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$ 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 $ 42 * $DragonFly: src/usr.sbin/vnconfig/vnconfig.c,v 1.15 2008/07/27 22:36:01 thomas Exp $ 43 */ 44 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> 61 #include <vfs/ufs/ufsmount.h> 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; 71 int64_t size; 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; 93 int listopt = 0; 94 u_long setopt = 0; 95 u_long resetopt = 0; 96 const char *configfile; 97 98 int config(struct vndisk *); 99 void getoptions(struct vndisk *, const char *); 100 int getinfo(const char *vname); 101 char *rawdevice(const char *); 102 void readconfig(int); 103 static void usage(void); 104 static int64_t getsize(const char *arg); 105 static void do_autolabel(const char *dev, const char *label); 106 int what_opt(const char *, u_long *); 107 108 int 109 main(int argc, char *argv[]) 110 { 111 int i, rv; 112 int flags = 0; 113 int64_t size = 0; 114 char *autolabel = NULL; 115 char *s; 116 117 configfile = _PATH_VNTAB; 118 while ((i = getopt(argc, argv, "acdef:glr:s:S:TZL:uv")) != -1) 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 163 case 'l': 164 listopt = 1; 165 break; 166 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 213 rv = 0; 214 if (listopt) { 215 if(argc > optind) 216 while(argc > optind) 217 rv += getinfo( argv[optind++]); 218 else { 219 rv = getinfo( NULL ); 220 } 221 exit(rv); 222 } 223 224 if (flags == 0) 225 flags = VN_CONFIG; 226 if (all) { 227 readconfig(flags); 228 } else { 229 vndisks = calloc(sizeof(struct vndisk), 1); 230 if (argc < optind + 1) 231 usage(); 232 vndisks[0].dev = argv[optind++]; 233 vndisks[0].file = argv[optind++]; /* may be NULL */ 234 vndisks[0].flags = flags; 235 vndisks[0].size = size; 236 vndisks[0].autolabel = autolabel; 237 if (optind < argc) 238 getoptions(&vndisks[0], argv[optind]); 239 nvndisks = 1; 240 } 241 rv = 0; 242 for (i = 0; i < nvndisks; i++) 243 rv += config(&vndisks[i]); 244 exit(rv); 245 } 246 247 int 248 what_opt(const char *str, u_long *p) 249 { 250 if (!strcmp(str,"reserve")) { *p |= VN_RESERVE; return 0; } 251 if (!strcmp(str,"labels")) { return 0; } /* deprecated */ 252 if (!strcmp(str,"follow")) { *p |= VN_FOLLOW; return 0; } 253 if (!strcmp(str,"debug")) { *p |= VN_DEBUG; return 0; } 254 if (!strcmp(str,"io")) { *p |= VN_IO; return 0; } 255 if (!strcmp(str,"all")) { *p |= ~0; return 0; } 256 if (!strcmp(str,"none")) { *p |= 0; return 0; } 257 return 1; 258 } 259 260 /* 261 * 262 * GETINFO 263 * 264 * Print vnode disk information to stdout for the device at 265 * path 'vname', or all existing 'vn' devices if none is given. 266 * Any 'vn' devices must exist under /dev in order to be queried. 267 * 268 * Todo: correctly use vm_secsize for swap-backed vn's .. 269 */ 270 271 int 272 getinfo( const char *vname ) 273 { 274 int i = 0, vd, printlim = 0; 275 char vnpath[PATH_MAX]; 276 const char *tmp; 277 278 struct vn_user vnu; 279 struct stat sb; 280 281 if (vname == NULL) { 282 printlim = 1024; 283 } else { 284 tmp = vname; 285 while (*tmp != 0) { 286 if(isdigit(*tmp)){ 287 i = atoi(tmp); 288 printlim = i + 1; 289 break; 290 } 291 tmp++; 292 } 293 if (*tmp == '\0') 294 errx(1, "unknown vn device: %s", vname); 295 } 296 297 snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", i); 298 299 vd = open(vnpath, O_RDONLY); 300 if (vd < 0) 301 err(1, "open: %s", vnpath); 302 303 for (; i<printlim; i++) { 304 305 bzero(&vnpath, sizeof(vnpath)); 306 bzero(&sb, sizeof(struct stat)); 307 bzero(&vnu, sizeof(struct vn_user)); 308 309 vnu.vnu_unit = i; 310 311 snprintf(vnpath, sizeof(vnpath), "/dev/vn%d", vnu.vnu_unit); 312 313 if(stat(vnpath, &sb) < 0) { 314 break; 315 } 316 else { 317 if (ioctl(vd, VNIOCGET, &vnu) == -1) { 318 warn("vn%d: ioctl", i); 319 continue; 320 } 321 322 fprintf(stdout, "vn%d: ", vnu.vnu_unit); 323 324 if (vnu.vnu_file[0] == 0) 325 fprintf(stdout, "not in use\n"); 326 else if ((strcmp(vnu.vnu_file, _VN_USER_SWAP)) == 0) 327 fprintf(stdout, 328 "consuming %lld VM pages\n", 329 vnu.vnu_size); 330 else 331 fprintf(stdout, 332 "covering %s on %s, inode %ju\n", 333 vnu.vnu_file, 334 devname(vnu.vnu_dev, S_IFBLK), 335 (uintmax_t)vnu.vnu_ino); 336 } 337 } 338 close(vd); 339 return 0; 340 } 341 342 int 343 config(struct vndisk *vnp) 344 { 345 char *dev, *file, *rdev, *oarg; 346 FILE *f; 347 struct vn_ioctl vnio; 348 int flags, pgsize, rv, status; 349 u_long l; 350 351 pgsize = getpagesize(); 352 353 status = rv = 0; 354 355 /* 356 * Prepend "/dev/" to the specified device name, if necessary. 357 * Operate on vnp->dev because it is used later. 358 */ 359 if (vnp->dev[0] != '/' && vnp->dev[0] != '.') 360 asprintf(&vnp->dev, "%s%s", _PATH_DEV, vnp->dev); 361 dev = vnp->dev; 362 file = vnp->file; 363 flags = vnp->flags; 364 oarg = vnp->oarg; 365 366 if (flags & VN_IGNORE) 367 return(0); 368 369 /* 370 * When a regular file has been specified, do any requested setup 371 * of the file. Truncation (also creates the file if necessary), 372 * sizing, and zeroing. 373 */ 374 375 if (file && vnp->size != 0 && (flags & VN_CONFIG)) { 376 int fd; 377 struct stat st; 378 379 if (flags & VN_TRUNCATE) 380 fd = open(file, O_RDWR|O_CREAT|O_TRUNC, 0600); 381 else 382 fd = open(file, O_RDWR); 383 if (fd >= 0 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { 384 if (st.st_size < vnp->size * pgsize) 385 ftruncate(fd, vnp->size * pgsize); 386 if (vnp->size != 0) 387 st.st_size = vnp->size * pgsize; 388 389 if (flags & VN_ZERO) { 390 char *buf = malloc(ZBUFSIZE); 391 bzero(buf, ZBUFSIZE); 392 while (st.st_size > 0) { 393 int n = (st.st_size > ZBUFSIZE) ? 394 ZBUFSIZE : (int)st.st_size; 395 if (write(fd, buf, n) != n) { 396 ftruncate(fd, 0); 397 printf("Unable to ZERO file %s\n", file); 398 return(0); 399 } 400 st.st_size -= (off_t)n; 401 } 402 } 403 close(fd); 404 } else { 405 printf("Unable to open file %s\n", file); 406 return(0); 407 } 408 } else if (file == NULL && vnp->size == 0 && (flags & VN_CONFIG)) { 409 warnx("specify regular filename or swap size"); 410 return (0); 411 } 412 413 rdev = rawdevice(dev); 414 f = fopen(rdev, "rw"); 415 if (f == NULL) { 416 warn("%s", dev); 417 return(1); 418 } 419 vnio.vn_file = file; 420 vnio.vn_size = vnp->size; /* non-zero only if swap backed */ 421 422 /* 423 * Disable the device 424 */ 425 if (flags & VN_DISABLE) { 426 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) { 427 rv = unmount(oarg, 0); 428 if (rv) { 429 status--; 430 if (errno == EBUSY) 431 flags &= ~VN_UNCONFIG; 432 if ((flags & VN_UNCONFIG) == 0) 433 warn("umount"); 434 } else if (verbose) 435 printf("%s: unmounted\n", dev); 436 } 437 } 438 /* 439 * Clear (un-configure) the device 440 */ 441 if (flags & VN_UNCONFIG) { 442 rv = ioctl(fileno(f), VNIOCDETACH, &vnio); 443 if (rv) { 444 if (errno == ENODEV) { 445 if (verbose) 446 printf("%s: not configured\n", dev); 447 rv = 0; 448 } else { 449 status--; 450 warn("VNIOCDETACH"); 451 } 452 } else if (verbose) 453 printf("%s: cleared\n", dev); 454 } 455 /* 456 * Set specified options 457 */ 458 if (flags & VN_SET) { 459 l = setopt; 460 if (global) 461 rv = ioctl(fileno(f), VNIOCGSET, &l); 462 else 463 rv = ioctl(fileno(f), VNIOCUSET, &l); 464 if (rv) { 465 status--; 466 warn("VNIO[GU]SET"); 467 } else if (verbose) 468 printf("%s: flags now=%08lx\n",dev,l); 469 } 470 /* 471 * Reset specified options 472 */ 473 if (flags & VN_RESET) { 474 l = resetopt; 475 if (global) 476 rv = ioctl(fileno(f), VNIOCGCLEAR, &l); 477 else 478 rv = ioctl(fileno(f), VNIOCUCLEAR, &l); 479 if (rv) { 480 status--; 481 warn("VNIO[GU]CLEAR"); 482 } else if (verbose) 483 printf("%s: flags now=%08lx\n",dev,l); 484 } 485 /* 486 * Configure the device 487 */ 488 if (flags & VN_CONFIG) { 489 rv = ioctl(fileno(f), VNIOCATTACH, &vnio); 490 if (rv) { 491 status--; 492 warn("VNIOCATTACH"); 493 flags &= ~VN_ENABLE; 494 } else { 495 if (verbose) { 496 printf("%s: %s, ", dev, file); 497 if (vnp->size != 0) { 498 printf("%lld bytes mapped\n", vnio.vn_size); 499 } else { 500 printf("complete file mapped\n"); 501 } 502 } 503 /* 504 * autolabel 505 */ 506 if (vnp->autolabel) { 507 do_autolabel(vnp->dev, vnp->autolabel); 508 } 509 } 510 } 511 /* 512 * Set an option 513 */ 514 if (flags & VN_SET) { 515 l = setopt; 516 if (global) 517 rv = ioctl(fileno(f), VNIOCGSET, &l); 518 else 519 rv = ioctl(fileno(f), VNIOCUSET, &l); 520 if (rv) { 521 status--; 522 warn("VNIO[GU]SET"); 523 } else if (verbose) 524 printf("%s: flags now=%08lx\n",dev,l); 525 } 526 /* 527 * Reset an option 528 */ 529 if (flags & VN_RESET) { 530 l = resetopt; 531 if (global) 532 rv = ioctl(fileno(f), VNIOCGCLEAR, &l); 533 else 534 rv = ioctl(fileno(f), VNIOCUCLEAR, &l); 535 if (rv) { 536 status--; 537 warn("VNIO[GU]CLEAR"); 538 } else if (verbose) 539 printf("%s: flags now=%08lx\n",dev,l); 540 } 541 542 /* 543 * Close the device now, as we may want to mount it. 544 */ 545 fclose(f); 546 547 /* 548 * Enable special functions on the device 549 */ 550 if (flags & VN_ENABLE) { 551 if (flags & VN_SWAP) { 552 rv = swapon(dev); 553 if (rv) { 554 status--; 555 warn("swapon"); 556 } 557 else if (verbose) 558 printf("%s: swapping enabled\n", dev); 559 } 560 if (flags & (VN_MOUNTRO|VN_MOUNTRW)) { 561 struct ufs_args args; 562 int mflags; 563 564 args.fspec = dev; 565 mflags = (flags & VN_MOUNTRO) ? MNT_RDONLY : 0; 566 rv = mount("ufs", oarg, mflags, &args); 567 if (rv) { 568 status--; 569 warn("mount"); 570 } 571 else if (verbose) 572 printf("%s: mounted on %s\n", dev, oarg); 573 } 574 } 575 /* done: */ 576 fflush(stdout); 577 return(status < 0); 578 } 579 580 #define EOL(c) ((c) == '\0' || (c) == '\n') 581 #define WHITE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') 582 583 void 584 readconfig(int flags) 585 { 586 char buf[LINESIZE]; 587 FILE *f; 588 char *cp, *sp; 589 int ix; 590 int ax; 591 592 f = fopen(configfile, "r"); 593 if (f == NULL) 594 err(1, "%s", configfile); 595 ix = 0; /* number of elements */ 596 ax = 0; /* allocated elements */ 597 while (fgets(buf, LINESIZE, f) != NULL) { 598 cp = buf; 599 if (*cp == '#') 600 continue; 601 while (!EOL(*cp) && WHITE(*cp)) 602 cp++; 603 if (EOL(*cp)) 604 continue; 605 sp = cp; 606 while (!EOL(*cp) && !WHITE(*cp)) 607 cp++; 608 if (EOL(*cp)) 609 continue; 610 *cp++ = '\0'; 611 612 if (ix == ax) { 613 ax = ax + 16; 614 vndisks = realloc(vndisks, ax * sizeof(struct vndisk)); 615 bzero(&vndisks[ix], (ax - ix) * sizeof(struct vndisk)); 616 } 617 vndisks[ix].dev = malloc(cp - sp); 618 strcpy(vndisks[ix].dev, sp); 619 while (!EOL(*cp) && WHITE(*cp)) 620 cp++; 621 if (EOL(*cp)) 622 continue; 623 sp = cp; 624 while (!EOL(*cp) && !WHITE(*cp)) 625 cp++; 626 *cp++ = '\0'; 627 628 if (*sp == '%' && strtol(sp + 1, NULL, 0) > 0) { 629 vndisks[ix].size = getsize(sp + 1); 630 } else { 631 vndisks[ix].file = malloc(cp - sp); 632 strcpy(vndisks[ix].file, sp); 633 } 634 635 while (!EOL(*cp) && WHITE(*cp)) 636 cp++; 637 vndisks[ix].flags = flags; 638 if (!EOL(*cp)) { 639 sp = cp; 640 while (!EOL(*cp) && !WHITE(*cp)) 641 cp++; 642 *cp++ = '\0'; 643 getoptions(&vndisks[ix], sp); 644 } 645 nvndisks++; 646 ix++; 647 } 648 } 649 650 void 651 getoptions(struct vndisk *vnp, const char *fstr) 652 { 653 int flags = 0; 654 const char *oarg = NULL; 655 656 if (strcmp(fstr, "swap") == 0) 657 flags |= VN_SWAP; 658 else if (strncmp(fstr, "mount=", 6) == 0) { 659 flags |= VN_MOUNTRW; 660 oarg = &fstr[6]; 661 } else if (strncmp(fstr, "mountrw=", 8) == 0) { 662 flags |= VN_MOUNTRW; 663 oarg = &fstr[8]; 664 } else if (strncmp(fstr, "mountro=", 8) == 0) { 665 flags |= VN_MOUNTRO; 666 oarg = &fstr[8]; 667 } else if (strcmp(fstr, "ignore") == 0) 668 flags |= VN_IGNORE; 669 vnp->flags |= flags; 670 if (oarg) { 671 vnp->oarg = malloc(strlen(oarg) + 1); 672 strcpy(vnp->oarg, oarg); 673 } else 674 vnp->oarg = NULL; 675 } 676 677 char * 678 rawdevice(const char *dev) 679 { 680 char *rawbuf, *dp, *ep; 681 struct stat sb; 682 int len; 683 684 len = strlen(dev); 685 rawbuf = malloc(len + 2); 686 strcpy(rawbuf, dev); 687 if (stat(rawbuf, &sb) != 0 || !S_ISCHR(sb.st_mode)) { 688 dp = strrchr(rawbuf, '/'); 689 if (dp) { 690 for (ep = &rawbuf[len]; ep > dp; --ep) 691 *(ep+1) = *ep; 692 *++ep = 'r'; 693 } 694 } 695 return (rawbuf); 696 } 697 698 static void 699 usage(void) 700 { 701 fprintf(stderr, "%s\n%s\n%s\n%s\n", 702 "usage: vnconfig [-cdeguvTZ] [-s options] [-r options]", 703 " [-S value] special_file [regular_file] [feature]", 704 " vnconfig -a [-cdeguv] [-s options] [-r options] [-f config_file]", 705 " vnconfig -l [special_file ...]"); 706 exit(1); 707 } 708 709 static int64_t 710 getsize(const char *arg) 711 { 712 char *ptr; 713 int pgsize = getpagesize(); 714 int64_t size = strtoq(arg, &ptr, 0); 715 716 switch(tolower(*ptr)) { 717 case 't': 718 /* 719 * GULP! Terabytes. It's actually possible to create 720 * a 7.9 TB VN device, though newfs can't handle any single 721 * filesystem larger then 1 TB. 722 */ 723 size *= 1024; 724 /* fall through */ 725 case 'g': 726 size *= 1024; 727 /* fall through */ 728 default: 729 case 'm': 730 size *= 1024; 731 /* fall through */ 732 case 'k': 733 size *= 1024; 734 /* fall through */ 735 case 'c': 736 break; 737 } 738 size = (size + pgsize - 1) / pgsize; 739 return(size); 740 } 741 742 /* 743 * DO_AUTOLABEL 744 * 745 * Automatically label the device. This will wipe any preexisting 746 * label. 747 */ 748 749 static void 750 do_autolabel(const char *dev __unused, const char *label __unused) 751 { 752 /* XXX not yet implemented */ 753 fprintf(stderr, "autolabel not yet implemented, sorry\n"); 754 exit(1); 755 } 756 757