| Commit | Line | Data |
|---|---|---|
| 984263bc MD |
1 | /* |
| 2 | * Copyright (c) 1993 University of Utah. | |
| 3 | * Copyright (c) 1990, 1993 | |
| 4 | * The Regents of the University of California. All rights reserved. | |
| 5 | * | |
| 6 | * This code is derived from software contributed to Berkeley by | |
| 7 | * the Systems Programming Group of the University of Utah Computer | |
| 8 | * Science Department. | |
| 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 University of | |
| 21 | * California, Berkeley and its contributors. | |
| 22 | * 4. Neither the name of the University nor the names of its contributors | |
| 23 | * may be used to endorse or promote products derived from this software | |
| 24 | * without specific prior written permission. | |
| 25 | * | |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 36 | * SUCH DAMAGE. | |
| 37 | * | |
| 38 | * from: Utah $Hdr: vnconfig.c 1.1 93/12/15$ | |
| 1de703da MD |
39 | * |
| 40 | * @(#)vnconfig.c 8.1 (Berkeley) 12/15/93 | |
| 41 | * $FreeBSD: src/usr.sbin/vnconfig/vnconfig.c,v 1.13.2.7 2003/06/02 09:10:27 maxim Exp $ | |
| daa51328 | 42 | * $DragonFly: src/usr.sbin/vnconfig/vnconfig.c,v 1.15 2008/07/27 22:36:01 thomas Exp $ |
| 984263bc MD |
43 | */ |
| 44 | ||
| 984263bc MD |
45 | #include <ctype.h> |
| 46 | #include <err.h> | |
| 47 | #include <errno.h> | |
| 48 | #include <paths.h> | |
| 49 | #include <stdio.h> | |
| 50 | #include <stdlib.h> | |
| 51 | #include <fcntl.h> | |
| 52 | #include <string.h> | |
| 53 | #include <unistd.h> | |
| 54 | #include <sys/param.h> | |
| 55 | #include <sys/ioctl.h> | |
| 56 | #include <sys/linker.h> | |
| 57 | #include <sys/mount.h> | |
| 58 | #include <sys/module.h> | |
| 59 | #include <sys/stat.h> | |
| 60 | #include <sys/vnioctl.h> | |
| 38a690d7 | 61 | #include <vfs/ufs/ufsmount.h> |
| 984263bc MD |
62 | |
| 63 | #define LINESIZE 1024 | |
| 64 | #define ZBUFSIZE 32768 | |
| 65 | ||
| 66 | struct vndisk { | |
| 67 | char *dev; | |
| 68 | char *file; | |
| 69 | char *autolabel; | |
| 70 | int flags; | |
| 57fec106 | 71 | int64_t size; |
| 984263bc MD |
72 | char *oarg; |
| 73 | } *vndisks; | |
| 74 | ||
| 75 | #define VN_CONFIG 0x01 | |
| 76 | #define VN_UNCONFIG 0x02 | |
| 77 | #define VN_ENABLE 0x04 | |
| 78 | #define VN_DISABLE 0x08 | |
| 79 | #define VN_SWAP 0x10 | |
| 80 | #define VN_MOUNTRO 0x20 | |
| 81 | #define VN_MOUNTRW 0x40 | |
| 82 | #define VN_IGNORE 0x80 | |
| 83 | #define VN_SET 0x100 | |
| 84 | #define VN_RESET 0x200 | |
| 85 | #define VN_TRUNCATE 0x400 | |
| 86 | #define VN_ZERO 0x800 | |
| 87 | ||
| 88 | int nvndisks; | |
| 89 | ||
| 90 | int all = 0; | |
| 91 | int verbose = 0; | |
| 92 | int global = 0; | |
| 30c1fde0 | 93 | int listopt = 0; |
| 984263bc MD |
94 | u_long setopt = 0; |
| 95 | u_long resetopt = 0; | |
| 96 | char *configfile; | |
| 97 | ||
| 2d8a3be7 EN |
98 | int config(struct vndisk *); |
| 99 | void getoptions(struct vndisk *, char *); | |
| 30c1fde0 | 100 | int getinfo(const char *vname); |
| 2d8a3be7 EN |
101 | char *rawdevice(char *); |
| 102 | void readconfig(int); | |
| 103 | static void usage(void); | |
| 57fec106 | 104 | static int64_t getsize(const char *arg); |
| 984263bc | 105 | static void do_autolabel(const char *dev, const char *label); |
| 2d8a3be7 | 106 | int what_opt(char *, u_long *); |
| 984263bc MD |
107 | |
| 108 | int | |
| cf287f0a | 109 | main(int argc, char *argv[]) |
| 984263bc | 110 | { |
| cf287f0a | 111 | int i, rv; |
| 984263bc | 112 | int flags = 0; |
| 57fec106 | 113 | int64_t size = 0; |
| 984263bc MD |
114 | char *autolabel = NULL; |
| 115 | char *s; | |
| 116 | ||
| 117 | configfile = _PATH_VNTAB; | |
| 30c1fde0 | 118 | while ((i = getopt(argc, argv, "acdef:glr:s:S:TZL:uv")) != -1) |
| 984263bc MD |
119 | switch (i) { |
| 120 | ||
| 121 | /* all -- use config file */ | |
| 122 | case 'a': | |
| 123 | all++; | |
| 124 | break; | |
| 125 | ||
| 126 | /* configure */ | |
| 127 | case 'c': | |
| 128 | flags |= VN_CONFIG; | |
| 129 | flags &= ~VN_UNCONFIG; | |
| 130 | break; | |
| 131 | ||
| 132 | /* disable */ | |
| 133 | case 'd': | |
| 134 | flags |= VN_DISABLE; | |
| 135 | flags &= ~VN_ENABLE; | |
| 136 | break; | |
| 137 | ||
| 138 | /* enable */ | |
| 139 | case 'e': | |
| 140 | flags |= (VN_ENABLE|VN_CONFIG); | |
| 141 | flags &= ~(VN_DISABLE|VN_UNCONFIG); | |
| 142 | break; | |
| 143 | ||
| 144 | /* alternate config file */ | |
| 145 | case 'f': | |
| 146 | configfile = optarg; | |
| 147 | break; | |
| 148 | ||
| 149 | /* fiddle global options */ | |
| 150 | case 'g': | |
| 151 | global = 1 - global; | |
| 152 | break; | |
| 153 | ||
| 154 | /* reset options */ | |
| 155 | case 'r': | |
| 156 | for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) { | |
| 157 | if (what_opt(s, &resetopt)) | |
| 158 | errx(1, "invalid options '%s'", s); | |
| 159 | } | |
| 160 | flags |= VN_RESET; | |
| 161 | break; | |
| 162 | ||
| 30c1fde0 CT |
163 | case 'l': |
| 164 | listopt = 1; | |
| 165 | break; | |
| 166 | ||
| 984263bc MD |
167 | /* set options */ |
| 168 | case 's': | |
| 169 | for (s = strtok(optarg, ","); s; s = strtok(NULL, ",")) { | |
| 170 | if (what_opt(s, &setopt)) | |
| 171 | errx(1, "invalid options '%s'", s); | |
| 172 | } | |
| 173 | flags |= VN_SET; | |
| 174 | break; | |
| 175 | ||
| 176 | /* unconfigure */ | |
| 177 | case 'u': | |
| 178 | flags |= (VN_DISABLE|VN_UNCONFIG); | |
| 179 | flags &= ~(VN_ENABLE|VN_CONFIG); | |
| 180 | break; | |
| 181 | ||
| 182 | /* verbose */ | |
| 183 | case 'v': | |
| 184 | verbose++; | |
| 185 | break; | |
| 186 | ||
| 187 | case 'S': | |
| 188 | size = getsize(optarg); | |
| 189 | flags |= VN_CONFIG; | |
| 190 | flags &= ~VN_UNCONFIG; | |
| 191 | break; | |
| 192 | ||
| 193 | case 'T': | |
| 194 | flags |= VN_TRUNCATE; | |
| 195 | break; | |
| 196 | ||
| 197 | case 'Z': | |
| 198 | flags |= VN_ZERO; | |
| 199 | break; | |
| 200 | ||
| 201 | case 'L': | |
| 202 | autolabel = optarg; | |
| 203 | break; | |
| 204 | ||
| 205 | default: | |
| 206 | usage(); | |
| 207 | } | |
| 208 | ||
| 209 | if (modfind("vn") < 0) | |
| 210 | if (kldload("vn") < 0 || modfind("vn") < 0) | |
| 211 | warnx( "cannot find or load \"vn\" kernel module"); | |
| 212 | ||
| 30c1fde0 CT |
213 | if (listopt) { |
| 214 | if(argc > optind) | |
| 215 | while(argc > optind) | |
| 216 | rv += getinfo( argv[optind++]); | |
| 217 | else { | |
| 218 | rv = getinfo( NULL ); | |
| 219 | } | |
| 220 | exit(rv); | |
| 221 | } | |
| 222 | ||
| 984263bc MD |
223 | if (flags == 0) |
| 224 | flags = VN_CONFIG; | |
| 225 | if (all) { | |
| 226 | readconfig(flags); | |
| 227 | } else { | |
| 228 | vndisks = calloc(sizeof(struct vndisk), 1); | |
| 229 | if (argc < optind + 1) | |
| 230 | usage(); | |
| 231 | vndisks[0].dev = argv[optind++]; | |
| 232 | vndisks[0].file = argv[optind++]; /* may be NULL */ | |
| 233 | vndisks[0].flags = flags; | |
| 234 | vndisks[0].size = size; | |
| 235 | vndisks[0].autolabel = autolabel; | |
| 236 | if (optind < argc) | |
| 237 | getoptions(&vndisks[0], argv[optind]); | |
| 238 | nvndisks = 1; | |
| 239 | } | |
| 240 | rv = 0; | |
| 241 | for (i = 0; i < nvndisks; i++) | |
| 242 | rv += config(&vndisks[i]); | |
| 243 | exit(rv); | |
| 244 | } | |
| 245 | ||
| 246 | int | |
| cf287f0a | 247 | what_opt(char *str, u_long *p) |
| 984263bc MD |
248 | { |
| 249 | if (!strcmp(str,"reserve")) { *p |= VN_RESERVE; return 0; } | |
| 250 | if (!strcmp(str,"labels")) { *p |= VN_LABELS; return 0; } | |
| 251 | if (!strcmp(str,"follow")) { *p |= VN_FOLLOW; return 0; } | |
| 252 | if (!strcmp(str,"debug")) { *p |= VN_DEBUG; return 0; } | |
| 253 | if (!strcmp(str,"io")) { *p |= VN_IO; return 0; } | |
| 254 | if (!strcmp(str,"all")) { *p |= ~0; return 0; } | |
| 255 | if (!strcmp(str,"none")) { *p |= 0; return 0; } | |
| 256 | return 1; | |
| 257 | } | |
| 258 | ||
| 30c1fde0 CT |
259 | /* |
| 260 | * | |
| 261 | * GETINFO | |
| 262 | * | |
| 263 | * Print vnode disk information to stdout for the device at | |
| 264 | * path 'vname', or all existing 'vn' devices if none is given. | |
| 265 | * Any 'vn' devices must exist under /dev in order to be queried. | |
| 266 | * | |
| 267 | * Todo: correctly use vm_secsize for swap-backed vn's .. | |
| 268 | */ | |
| 269 | ||
| 270 | int | |
| 271 | getinfo( const char *vname ) | |
| 272 | { | |
| 273 | int i, vd, printlim = 0; | |
| 274 | char vnpath[PATH_MAX], *tmp; | |
| 275 | ||
| 276 | struct vn_user vnu; | |
| 277 | struct stat sb; | |
| 278 | ||
| 279 | if (vname == NULL) { | |
| 280 | i = 0; | |
| 281 | printlim = 1024; | |
| 282 | } | |
| 283 | else { | |
| 284 | tmp = (char *) vname; | |
| 201b3f37 | 285 | while (*tmp != 0) { |
| 30c1fde0 CT |
286 | if(isdigit(*tmp)){ |
| 287 | i = atoi(tmp); | |
| 288 | printlim = i + 1; | |
| 289 | break; | |
| 290 | } | |
| 291 | tmp++; | |
| 292 | } | |
| 293 | if (tmp == NULL) { | |
| 294 | printf("unknown vn device: %s", vname); | |
| 295 | return 1; | |
| 296 | } | |
| 297 | } | |
| 298 | ||
| 299 | snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", i); | |
| 300 | ||
| 301 | vd = open((char *)vnpath, O_RDONLY); | |
| 302 | if (vd < 0) { | |
| 303 | err(1, "open: %s", vnpath); | |
| 304 | return 1; | |
| 305 | } | |
| 306 | ||
| 307 | for (i; i<printlim; i++) { | |
| 308 | ||
| 309 | bzero((void *) &vnpath, sizeof(vnpath)); | |
| 310 | bzero((void *) &sb, sizeof(struct stat)); | |
| 311 | bzero((void *) &vnu, sizeof(struct vn_user)); | |
| 312 | ||
| 313 | vnu.vnu_unit = i; | |
| 314 | ||
| 315 | snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", vnu.vnu_unit); | |
| 316 | ||
| 317 | if(stat(vnpath, &sb) < 0) | |
| 318 | break; | |
| 319 | else { | |
| 320 | if (ioctl(vd, VNIOCGET, &vnu) == -1) { | |
| 321 | if (errno != ENXIO) { | |
| 322 | err(1, "ioctl: %s", vname); | |
| 323 | close(vd); | |
| 324 | return 1; | |
| 325 | } | |
| 326 | } | |
| 327 | ||
| 328 | fprintf(stdout, "vn%d: ", vnu.vnu_unit); | |
| 329 | ||
| 330 | if (vnu.vnu_file[0] == 0) | |
| 331 | fprintf(stdout, "not in use\n"); | |
| 332 | else if ((strcmp(vnu.vnu_file, _VN_USER_SWAP)) == 0) | |
| 333 | fprintf(stdout, | |
| 334 | "consuming %d VM pages\n", | |
| 335 | vnu.vnu_size); | |
| 336 | else | |
| 337 | fprintf(stdout, | |
| 0656dd4c | 338 | "covering %s on %s, inode %ju\n", |
| 30c1fde0 CT |
339 | vnu.vnu_file, |
| 340 | devname(vnu.vnu_dev, S_IFBLK), | |
| 0656dd4c | 341 | (uintmax_t)vnu.vnu_ino); |
| 30c1fde0 CT |
342 | } |
| 343 | } | |
| 344 | close(vd); | |
| 345 | return 0; | |
| 346 | } | |
| 347 | ||
| 984263bc | 348 | int |
| cf287f0a | 349 | config(struct vndisk *vnp) |
| 984263bc | 350 | { |
| 66950a15 | 351 | char *dev, *file, *rdev, *oarg; |
| 984263bc | 352 | FILE *f; |
| 66950a15 HP |
353 | struct vn_ioctl vnio; |
| 354 | int flags, pgsize, rv, status; | |
| 984263bc | 355 | u_long l; |
| 66950a15 HP |
356 | |
| 357 | pgsize = getpagesize(); | |
| 984263bc MD |
358 | |
| 359 | status = rv = 0; | |
| 360 | ||
| 361 | /* | |
| 362 | * Prepend "/dev/" to the specified device name, if necessary. | |
| 363 | * Operate on vnp->dev because it is used later. | |
| 364 | */ | |
| 365 | if (vnp->dev[0] != '/' && vnp->dev[0] != '.') | |
| 71126e33 | 366 | asprintf(&vnp->dev, "%s%s", _PATH_DEV, vnp->dev); |
| 984263bc MD |
367 | dev = vnp->dev; |
| 368 | file = vnp->file; | |
| 369 | flags = vnp->flags; | |
| 370 | oarg = vnp->oarg; | |
| 371 | ||
| 372 | if (flags & VN_IGNORE) | |
| 373 | return(0); | |
| 374 | ||
| 375 | /* | |
| 376 | * When a regular file has been specified, do any requested setup | |
| 377 | * of the file. Truncation (also creates the file if necessary), | |
| 378 | * sizing, and zeroing. | |
| 379 | */ | |
| 380 | ||
| 381 | if (file && vnp->size != 0 && (flags & VN_CONFIG)) { | |
| 382 | int fd; | |
| 383 | struct stat st; | |
| 384 | ||
| 385 | if (flags & VN_TRUNCATE) | |
| c7daadb1 | 386 | fd = open(file, O_RDWR|O_CREAT|O_TRUNC, 0600); |
| 984263bc MD |
387 | else |
| 388 | fd = open(file, O_RDWR); | |
| 389 | if (fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { | |
| 57fec106 MD |
390 | if (st.st_size < vnp->size * pgsize) |
| 391 | ftruncate(fd, vnp->size * pgsize); | |
| 984263bc | 392 | if (vnp->size != 0) |
| 57fec106 | 393 | st.st_size = vnp->size * pgsize; |
| 984263bc MD |
394 | |
| 395 | if (flags & VN_ZERO) { | |
| 396 | char *buf = malloc(ZBUFSIZE); | |
| 397 | bzero(buf, ZBUFSIZE); | |
| 398 | while (st.st_size > 0) { | |
| 399 | int n = (st.st_size > ZBUFSIZE) ? | |
| 400 | ZBUFSIZE : (int)st.st_size; | |
| 401 | if (write(fd, buf, n) != n) { | |
| 402 | ftruncate(fd, 0); | |
| 403 | printf("Unable to ZERO file %s\n", file); | |
| 404 | return(0); | |
| 405 | } | |
| 406 | st.st_size -= (off_t)n; | |
| 407 | } | |
| 408 | } | |
| 409 | close(fd); | |
| 410 | } else { | |
| 411 | printf("Unable to open file %s\n", file); | |
| 412 | return(0); | |
| 413 | } | |
| 414 | } | |
| 415 | ||
| 416 | rdev = rawdevice(dev); | |
| 417 | f = fopen(rdev, "rw"); | |
| 418 | if (f == NULL) { | |
| 419 | warn("%s", dev); | |
| 420 | return(1); | |
| 421 | } | |
| 422 | vnio.vn_file = file; | |
| 423 | vnio.vn_size = vnp->size; /* non-zero only if swap backed */ | |
| 424 | ||
| 425 | /* | |
| 426 | * Disable the device | |
| 427 | */ | |
| 428 | if (flags & VN_DISABLE) { | |
| 429 | if (flags & (VN_MOUNTRO|VN_MOUNTRW)) { | |
| 430 | rv = unmount(oarg, 0); | |
| 431 | if (rv) { | |
| 432 | status--; | |
| 433 | if (errno == EBUSY) | |
| 434 | flags &= ~VN_UNCONFIG; | |
| 435 | if ((flags & VN_UNCONFIG) == 0) | |
| 436 | warn("umount"); | |
| 437 | } else if (verbose) | |
| 438 | printf("%s: unmounted\n", dev); | |
| 439 | } | |
| 440 | } | |
| 441 | /* | |
| 442 | * Clear (un-configure) the device | |
| 443 | */ | |
| 444 | if (flags & VN_UNCONFIG) { | |
| 445 | rv = ioctl(fileno(f), VNIOCDETACH, &vnio); | |
| 446 | if (rv) { | |
| 447 | if (errno == ENODEV) { | |
| 448 | if (verbose) | |
| 449 | printf("%s: not configured\n", dev); | |
| 450 | rv = 0; | |
| 451 | } else { | |
| 452 | status--; | |
| 453 | warn("VNIOCDETACH"); | |
| 454 | } | |
| 455 | } else if (verbose) | |
| 456 | printf("%s: cleared\n", dev); | |
| 457 | } | |
| 458 | /* | |
| 459 | * Set specified options | |
| 460 | */ | |
| 461 | if (flags & VN_SET) { | |
| 462 | l = setopt; | |
| 463 | if (global) | |
| 464 | rv = ioctl(fileno(f), VNIOCGSET, &l); | |
| 465 | else | |
| 466 | rv = ioctl(fileno(f), VNIOCUSET, &l); | |
| 467 | if (rv) { | |
| 468 | status--; | |
| 469 | warn("VNIO[GU]SET"); | |
| 470 | } else if (verbose) | |
| 471 | printf("%s: flags now=%08x\n",dev,l); | |
| 472 | } | |
| 473 | /* | |
| 474 | * Reset specified options | |
| 475 | */ | |
| 476 | if (flags & VN_RESET) { | |
| 477 | l = resetopt; | |
| 478 | if (global) | |
| 479 | rv = ioctl(fileno(f), VNIOCGCLEAR, &l); | |
| 480 | else | |
| 481 | rv = ioctl(fileno(f), VNIOCUCLEAR, &l); | |
| 482 | if (rv) { | |
| 483 | status--; | |
| 484 | warn("VNIO[GU]CLEAR"); | |
| 485 | } else if (verbose) | |
| 486 | printf("%s: flags now=%08x\n",dev,l); | |
| 487 | } | |
| 488 | /* | |
| 489 | * Configure the device | |
| 490 | */ | |
| 491 | if (flags & VN_CONFIG) { | |
| 492 | rv = ioctl(fileno(f), VNIOCATTACH, &vnio); | |
| 493 | if (rv) { | |
| 494 | status--; | |
| 495 | warn("VNIOCATTACH"); | |
| 496 | flags &= ~VN_ENABLE; | |
| 497 | } else { | |
| 498 | if (verbose) { | |
| b3f9ca10 HP |
499 | printf("%s: %s, ", dev, file); |
| 500 | if (vnp->size != 0) { | |
| 57fec106 | 501 | printf("%lld bytes mapped\n", vnio.vn_size); |
| b3f9ca10 HP |
502 | } else { |
| 503 | printf("complete file mapped\n"); | |
| 504 | } | |
| 984263bc MD |
505 | } |
| 506 | /* | |
| 507 | * autolabel | |
| 508 | */ | |
| 509 | if (vnp->autolabel) { | |
| 510 | do_autolabel(vnp->dev, vnp->autolabel); | |
| 511 | } | |
| 512 | } | |
| 513 | } | |
| 514 | /* | |
| 515 | * Set an option | |
| 516 | */ | |
| 517 | if (flags & VN_SET) { | |
| 518 | l = setopt; | |
| 519 | if (global) | |
| 520 | rv = ioctl(fileno(f), VNIOCGSET, &l); | |
| 521 | else | |
| 522 | rv = ioctl(fileno(f), VNIOCUSET, &l); | |
| 523 | if (rv) { | |
| 524 | status--; | |
| 525 | warn("VNIO[GU]SET"); | |
| 526 | } else if (verbose) | |
| 527 | printf("%s: flags now=%08lx\n",dev,l); | |
| 528 | } | |
| 529 | /* | |
| 530 | * Reset an option | |
| 531 | */ | |
| 532 | if (flags & VN_RESET) { | |
| 533 | l = resetopt; | |
| 534 | if (global) | |
| 535 | rv = ioctl(fileno(f), VNIOCGCLEAR, &l); | |
| 536 | else | |
| 537 | rv = ioctl(fileno(f), VNIOCUCLEAR, &l); | |
| 538 | if (rv) { | |
| 539 | status--; | |
| 540 | warn("VNIO[GU]CLEAR"); | |
| 541 | } else if (verbose) | |
| 542 | printf("%s: flags now=%08lx\n",dev,l); | |
| 543 | } | |
| 544 | ||
| 545 | /* | |
| 546 | * Close the device now, as we may want to mount it. | |
| 547 | */ | |
| 548 | fclose(f); | |
| 549 | ||
| 550 | /* | |
| 551 | * Enable special functions on the device | |
| 552 | */ | |
| 553 | if (flags & VN_ENABLE) { | |
| 554 | if (flags & VN_SWAP) { | |
| 555 | rv = swapon(dev); | |
| 556 | if (rv) { | |
| 557 | status--; | |
| 558 | warn("swapon"); | |
| 559 | } | |
| 560 | else if (verbose) | |
| 561 | printf("%s: swapping enabled\n", dev); | |
| 562 | } | |
| 563 | if (flags & (VN_MOUNTRO|VN_MOUNTRW)) { | |
| 564 | struct ufs_args args; | |
| 565 | int mflags; | |
| 566 | ||
| 567 | args.fspec = dev; | |
| 568 | mflags = (flags & VN_MOUNTRO) ? MNT_RDONLY : 0; | |
| 569 | rv = mount("ufs", oarg, mflags, &args); | |
| 570 | if (rv) { | |
| 571 | status--; | |
| 572 | warn("mount"); | |
| 573 | } | |
| 574 | else if (verbose) | |
| 575 | printf("%s: mounted on %s\n", dev, oarg); | |
| 576 | } | |
| 577 | } | |
| 578 | /* done: */ | |
| 579 | fflush(stdout); | |
| 580 | return(status < 0); | |
| 581 | } | |
| 582 | ||
| 583 | #define EOL(c) ((c) == '\0' || (c) == '\n') | |
| 584 | #define WHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') | |
| 585 | ||
| 586 | void | |
| cf287f0a | 587 | readconfig(int flags) |
| 984263bc MD |
588 | { |
| 589 | char buf[LINESIZE]; | |
| 590 | FILE *f; | |
| cf287f0a MD |
591 | char *cp, *sp; |
| 592 | int ix; | |
| 984263bc MD |
593 | int ax; |
| 594 | ||
| 595 | f = fopen(configfile, "r"); | |
| 596 | if (f == NULL) | |
| 597 | err(1, "%s", configfile); | |
| 598 | ix = 0; /* number of elements */ | |
| 599 | ax = 0; /* allocated elements */ | |
| 600 | while (fgets(buf, LINESIZE, f) != NULL) { | |
| 601 | cp = buf; | |
| 602 | if (*cp == '#') | |
| 603 | continue; | |
| 604 | while (!EOL(*cp) && WHITE(*cp)) | |
| 605 | cp++; | |
| 606 | if (EOL(*cp)) | |
| 607 | continue; | |
| 608 | sp = cp; | |
| 609 | while (!EOL(*cp) && !WHITE(*cp)) | |
| 610 | cp++; | |
| 611 | if (EOL(*cp)) | |
| 612 | continue; | |
| 613 | *cp++ = '\0'; | |
| 614 | ||
| 615 | if (ix == ax) { | |
| 616 | ax = ax + 16; | |
| 617 | vndisks = realloc(vndisks, ax * sizeof(struct vndisk)); | |
| 618 | bzero(&vndisks[ix], (ax - ix) * sizeof(struct vndisk)); | |
| 619 | } | |
| 620 | vndisks[ix].dev = malloc(cp - sp); | |
| 621 | strcpy(vndisks[ix].dev, sp); | |
| 622 | while (!EOL(*cp) && WHITE(*cp)) | |
| 623 | cp++; | |
| 624 | if (EOL(*cp)) | |
| 625 | continue; | |
| 626 | sp = cp; | |
| 627 | while (!EOL(*cp) && !WHITE(*cp)) | |
| 628 | cp++; | |
| 629 | *cp++ = '\0'; | |
| 630 | ||
| 631 | if (*sp == '%' && strtol(sp + 1, NULL, 0) > 0) { | |
| 632 | vndisks[ix].size = getsize(sp + 1); | |
| 633 | } else { | |
| 634 | vndisks[ix].file = malloc(cp - sp); | |
| 635 | strcpy(vndisks[ix].file, sp); | |
| 636 | } | |
| 637 | ||
| 638 | while (!EOL(*cp) && WHITE(*cp)) | |
| 639 | cp++; | |
| 640 | vndisks[ix].flags = flags; | |
| 641 | if (!EOL(*cp)) { | |
| 642 | sp = cp; | |
| 643 | while (!EOL(*cp) && !WHITE(*cp)) | |
| 644 | cp++; | |
| 645 | *cp++ = '\0'; | |
| 646 | getoptions(&vndisks[ix], sp); | |
| 647 | } | |
| 648 | nvndisks++; | |
| 649 | ix++; | |
| 650 | } | |
| 651 | } | |
| 652 | ||
| 653 | void | |
| cf287f0a | 654 | getoptions(struct vndisk *vnp, char *fstr) |
| 984263bc MD |
655 | { |
| 656 | int flags = 0; | |
| 657 | char *oarg = NULL; | |
| 658 | ||
| 659 | if (strcmp(fstr, "swap") == 0) | |
| 660 | flags |= VN_SWAP; | |
| 661 | else if (strncmp(fstr, "mount=", 6) == 0) { | |
| 662 | flags |= VN_MOUNTRW; | |
| 663 | oarg = &fstr[6]; | |
| 664 | } else if (strncmp(fstr, "mountrw=", 8) == 0) { | |
| 665 | flags |= VN_MOUNTRW; | |
| 666 | oarg = &fstr[8]; | |
| 667 | } else if (strncmp(fstr, "mountro=", 8) == 0) { | |
| 668 | flags |= VN_MOUNTRO; | |
| 669 | oarg = &fstr[8]; | |
| 670 | } else if (strcmp(fstr, "ignore") == 0) | |
| 671 | flags |= VN_IGNORE; | |
| 672 | vnp->flags |= flags; | |
| 673 | if (oarg) { | |
| 674 | vnp->oarg = malloc(strlen(oarg) + 1); | |
| 675 | strcpy(vnp->oarg, oarg); | |
| 676 | } else | |
| 677 | vnp->oarg = NULL; | |
| 678 | } | |
| 679 | ||
| 680 | char * | |
| cf287f0a | 681 | rawdevice(char *dev) |
| 984263bc | 682 | { |
| cf287f0a | 683 | char *rawbuf, *dp, *ep; |
| 984263bc MD |
684 | struct stat sb; |
| 685 | int len; | |
| 686 | ||
| 687 | len = strlen(dev); | |
| 688 | rawbuf = malloc(len + 2); | |
| 689 | strcpy(rawbuf, dev); | |
| 690 | if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) { | |
| 17b61719 | 691 | dp = strrchr(rawbuf, '/'); |
| 984263bc MD |
692 | if (dp) { |
| 693 | for (ep = &rawbuf[len]; ep > dp; --ep) | |
| 694 | *(ep+1) = *ep; | |
| 695 | *++ep = 'r'; | |
| 696 | } | |
| 697 | } | |
| 698 | return (rawbuf); | |
| 699 | } | |
| 700 | ||
| 701 | static void | |
| 44d05a04 | 702 | usage(void) |
| 984263bc | 703 | { |
| daa51328 TN |
704 | fprintf(stderr, "%s\n%s\n%s\n%s\n", |
| 705 | "usage: vnconfig [-cdeguvTZ] [-s options] [-r options]", | |
| 706 | " [-S value] special_file [regular_file] [feature]", | |
| 707 | " vnconfig -a [-cdeguv] [-s options] [-r options] [-f config_file]", | |
| 708 | " vnconfig -l [special_file ...]"); | |
| 984263bc MD |
709 | exit(1); |
| 710 | } | |
| 711 | ||
| 57fec106 | 712 | static int64_t |
| 984263bc MD |
713 | getsize(const char *arg) |
| 714 | { | |
| 715 | char *ptr; | |
| 716 | int pgsize = getpagesize(); | |
| 57fec106 | 717 | int64_t size = strtoq(arg, &ptr, 0); |
| 984263bc MD |
718 | |
| 719 | switch(tolower(*ptr)) { | |
| 720 | case 't': | |
| 721 | /* | |
| 722 | * GULP! Terrabytes. It's actually possible to create | |
| 723 | * a 7.9 TB VN device, though newfs can't handle any single | |
| 724 | * filesystem larger then 1 TB. | |
| 725 | */ | |
| 726 | size *= 1024; | |
| 727 | /* fall through */ | |
| 728 | case 'g': | |
| 729 | size *= 1024; | |
| 730 | /* fall through */ | |
| 731 | default: | |
| 732 | case 'm': | |
| 733 | size *= 1024; | |
| 734 | /* fall through */ | |
| 735 | case 'k': | |
| 736 | size *= 1024; | |
| 737 | /* fall through */ | |
| 738 | case 'c': | |
| 739 | break; | |
| 740 | } | |
| 741 | size = (size + pgsize - 1) / pgsize; | |
| 57fec106 | 742 | return(size); |
| 984263bc MD |
743 | } |
| 744 | ||
| 745 | /* | |
| 746 | * DO_AUTOLABEL | |
| 747 | * | |
| 748 | * Automatically label the device. This will wipe any preexisting | |
| 749 | * label. | |
| 750 | */ | |
| 751 | ||
| 752 | static void | |
| 753 | do_autolabel(const char *dev, const char *label) | |
| 754 | { | |
| 755 | /* XXX not yet implemented */ | |
| 756 | fprintf(stderr, "autolabel not yet implemented, sorry\n"); | |
| 757 | exit(1); | |
| 758 | } | |
| 759 |