1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/stat.h> 50 #include <sys/time.h> 51 #ifdef sunos 52 #include <sys/vnode.h> 53 54 #include <ufs/inode.h> 55 #include <ufs/fs.h> 56 #else 57 #include <ufs/ufs/dinode.h> 58 #include <ufs/ffs/fs.h> 59 #endif 60 61 #include <protocols/dumprestore.h> 62 63 #include <ctype.h> 64 #include <err.h> 65 #include <fcntl.h> 66 #include <fstab.h> 67 #include <signal.h> 68 #include <stdio.h> 69 #include <stdlib.h> 70 #include <string.h> 71 #include <unistd.h> 72 73 #include "dump.h" 74 #include "pathnames.h" 75 76 #ifndef SBOFF 77 #define SBOFF (SBLOCK * DEV_BSIZE) 78 #endif 79 80 int notify = 0; /* notify operator flag */ 81 int blockswritten = 0; /* number of blocks written on current tape */ 82 int tapeno = 0; /* current tape number */ 83 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ 84 int ntrec = NTREC; /* # tape blocks in each tape record */ 85 int cartridge = 0; /* Assume non-cartridge tape */ 86 int dokerberos = 0; /* Use Kerberos authentication */ 87 long dev_bsize = 1; /* recalculated below */ 88 long blocksperfile; /* output blocks per file */ 89 char *host = NULL; /* remote host (if any) */ 90 91 static long numarg __P((char *, long, long)); 92 static void obsolete __P((int *, char **[])); 93 static void usage __P((void)); 94 95 int 96 main(argc, argv) 97 int argc; 98 char *argv[]; 99 { 100 register ino_t ino; 101 register int dirty; 102 register struct dinode *dp; 103 register struct fstab *dt; 104 register char *map; 105 register int ch; 106 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 107 ino_t maxino; 108 109 spcl.c_date = 0; 110 (void)time((time_t *)&spcl.c_date); 111 112 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 113 if ((tape = getenv("TAPE")) == NULL) 114 tape = _PATH_DEFTAPE; 115 dumpdates = _PATH_DUMPDATES; 116 temp = _PATH_DTMP; 117 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 118 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 119 level = '0'; 120 121 if (argc < 2) 122 usage(); 123 124 obsolete(&argc, &argv); 125 #ifdef KERBEROS 126 #define optstring "0123456789aB:b:cd:f:h:kns:T:uWw" 127 #else 128 #define optstring "0123456789aB:b:cd:f:h:ns:T:uWw" 129 #endif 130 while ((ch = getopt(argc, argv, optstring)) != -1) 131 #undef optstring 132 switch (ch) { 133 /* dump level */ 134 case '0': case '1': case '2': case '3': case '4': 135 case '5': case '6': case '7': case '8': case '9': 136 level = ch; 137 break; 138 139 case 'a': /* `auto-size', Write to EOM. */ 140 unlimited = 1; 141 break; 142 143 case 'B': /* blocks per output file */ 144 blocksperfile = numarg("number of blocks per file", 145 1L, 0L); 146 break; 147 148 case 'b': /* blocks per tape write */ 149 ntrec = numarg("number of blocks per write", 150 1L, 1000L); 151 break; 152 153 case 'c': /* Tape is cart. not 9-track */ 154 cartridge = 1; 155 break; 156 157 case 'd': /* density, in bits per inch */ 158 density = numarg("density", 10L, 327670L) / 10; 159 if (density >= 625 && !bflag) 160 ntrec = HIGHDENSITYTREC; 161 break; 162 163 case 'f': /* output file */ 164 tape = optarg; 165 break; 166 167 case 'h': 168 honorlevel = numarg("honor level", 0L, 10L); 169 break; 170 171 #ifdef KERBEROS 172 case 'k': 173 dokerberos = 1; 174 break; 175 #endif 176 177 case 'n': /* notify operators */ 178 notify = 1; 179 break; 180 181 case 's': /* tape size, feet */ 182 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 183 break; 184 185 case 'T': /* time of last dump */ 186 spcl.c_ddate = unctime(optarg); 187 if (spcl.c_ddate < 0) { 188 (void)fprintf(stderr, "bad time \"%s\"\n", 189 optarg); 190 exit(X_STARTUP); 191 } 192 Tflag = 1; 193 lastlevel = '?'; 194 argc--; 195 argv++; 196 break; 197 198 case 'u': /* update /etc/dumpdates */ 199 uflag = 1; 200 break; 201 202 case 'W': /* what to do */ 203 case 'w': 204 lastdump(ch); 205 exit(X_FINOK); /* do nothing else */ 206 207 default: 208 usage(); 209 } 210 argc -= optind; 211 argv += optind; 212 213 if (argc < 1) { 214 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 215 exit(X_STARTUP); 216 } 217 disk = *argv++; 218 argc--; 219 if (argc >= 1) { 220 (void)fprintf(stderr, "Unknown arguments to dump:"); 221 while (argc--) 222 (void)fprintf(stderr, " %s", *argv++); 223 (void)fprintf(stderr, "\n"); 224 exit(X_STARTUP); 225 } 226 if (Tflag && uflag) { 227 (void)fprintf(stderr, 228 "You cannot use the T and u flags together.\n"); 229 exit(X_STARTUP); 230 } 231 if (strcmp(tape, "-") == 0) { 232 pipeout++; 233 tape = "standard output"; 234 } 235 236 if (blocksperfile) 237 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 238 else if (!unlimited) { 239 /* 240 * Determine how to default tape size and density 241 * 242 * density tape size 243 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 244 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 245 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 246 * (450*4 - slop) 247 * hilit19 hits again: " 248 */ 249 if (density == 0) 250 density = cartridge ? 100 : 160; 251 if (tsize == 0) 252 tsize = cartridge ? 1700L*120L : 2300L*120L; 253 } 254 255 if (strchr(tape, ':')) { 256 host = tape; 257 tape = strchr(host, ':'); 258 *tape++ = '\0'; 259 #ifdef RDUMP 260 if (index(tape, '\n')) { 261 (void)fprintf(stderr, "invalid characters in tape\n"); 262 exit(X_STARTUP); 263 } 264 if (rmthost(host) == 0) 265 exit(X_STARTUP); 266 #else 267 (void)fprintf(stderr, "remote dump not enabled\n"); 268 exit(X_STARTUP); 269 #endif 270 } 271 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 272 273 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 274 signal(SIGHUP, sig); 275 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 276 signal(SIGTRAP, sig); 277 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 278 signal(SIGFPE, sig); 279 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 280 signal(SIGBUS, sig); 281 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 282 signal(SIGSEGV, sig); 283 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 284 signal(SIGTERM, sig); 285 if (signal(SIGINT, interrupt) == SIG_IGN) 286 signal(SIGINT, SIG_IGN); 287 288 set_operators(); /* /etc/group snarfed */ 289 getfstab(); /* /etc/fstab snarfed */ 290 /* 291 * disk can be either the full special file name, 292 * the suffix of the special file name, 293 * the special name missing the leading '/', 294 * the file system name with or without the leading '/'. 295 */ 296 dt = fstabsearch(disk); 297 if (dt != NULL) { 298 disk = rawname(dt->fs_spec); 299 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 300 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 301 } else { 302 (void)strncpy(spcl.c_dev, disk, NAMELEN); 303 (void)strncpy(spcl.c_filesys, "an unlisted file system", 304 NAMELEN); 305 } 306 spcl.c_dev[NAMELEN-1]='\0'; 307 spcl.c_filesys[NAMELEN-1]='\0'; 308 (void)strcpy(spcl.c_label, "none"); 309 (void)gethostname(spcl.c_host, NAMELEN); 310 spcl.c_level = level - '0'; 311 spcl.c_type = TS_TAPE; 312 if (!Tflag) 313 getdumptime(); /* /etc/dumpdates snarfed */ 314 315 msg("Date of this level %c dump: %s", level, 316 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 317 msg("Date of last level %c dump: %s", lastlevel, 318 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 319 msg("Dumping %s ", disk); 320 if (dt != NULL) 321 msgtail("(%s) ", dt->fs_file); 322 if (host) 323 msgtail("to %s on host %s\n", tape, host); 324 else 325 msgtail("to %s\n", tape); 326 327 if ((diskfd = open(disk, O_RDONLY)) < 0) { 328 msg("Cannot open %s\n", disk); 329 exit(X_STARTUP); 330 } 331 sync(); 332 sblock = (struct fs *)sblock_buf; 333 bread(SBOFF, (char *) sblock, SBSIZE); 334 if (sblock->fs_magic != FS_MAGIC) 335 quit("bad sblock magic number\n"); 336 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 337 dev_bshift = ffs(dev_bsize) - 1; 338 if (dev_bsize != (1 << dev_bshift)) 339 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 340 tp_bshift = ffs(TP_BSIZE) - 1; 341 if (TP_BSIZE != (1 << tp_bshift)) 342 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 343 #ifdef FS_44INODEFMT 344 if (sblock->fs_inodefmt >= FS_44INODEFMT) 345 spcl.c_flags |= DR_NEWINODEFMT; 346 #endif 347 maxino = sblock->fs_ipg * sblock->fs_ncg; 348 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 349 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 350 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 351 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 352 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 353 354 nonodump = spcl.c_level < honorlevel; 355 356 msg("mapping (Pass I) [regular files]\n"); 357 anydirskipped = mapfiles(maxino, &tapesize); 358 359 msg("mapping (Pass II) [directories]\n"); 360 while (anydirskipped) { 361 anydirskipped = mapdirs(maxino, &tapesize); 362 } 363 364 if (pipeout || unlimited) { 365 tapesize += 10; /* 10 trailer blocks */ 366 msg("estimated %ld tape blocks.\n", tapesize); 367 } else { 368 double fetapes; 369 370 if (blocksperfile) 371 fetapes = (double) tapesize / blocksperfile; 372 else if (cartridge) { 373 /* Estimate number of tapes, assuming streaming stops at 374 the end of each block written, and not in mid-block. 375 Assume no erroneous blocks; this can be compensated 376 for with an artificially low tape size. */ 377 fetapes = 378 ( (double) tapesize /* blocks */ 379 * TP_BSIZE /* bytes/block */ 380 * (1.0/density) /* 0.1" / byte " */ 381 + 382 (double) tapesize /* blocks */ 383 * (1.0/ntrec) /* streaming-stops per block */ 384 * 15.48 /* 0.1" / streaming-stop " */ 385 ) * (1.0 / tsize ); /* tape / 0.1" " */ 386 } else { 387 /* Estimate number of tapes, for old fashioned 9-track 388 tape */ 389 int tenthsperirg = (density == 625) ? 3 : 7; 390 fetapes = 391 ( (double) tapesize /* blocks */ 392 * TP_BSIZE /* bytes / block */ 393 * (1.0/density) /* 0.1" / byte " */ 394 + 395 (double) tapesize /* blocks */ 396 * (1.0/ntrec) /* IRG's / block */ 397 * tenthsperirg /* 0.1" / IRG " */ 398 ) * (1.0 / tsize ); /* tape / 0.1" " */ 399 } 400 etapes = fetapes; /* truncating assignment */ 401 etapes++; 402 /* count the dumped inodes map on each additional tape */ 403 tapesize += (etapes - 1) * 404 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 405 tapesize += etapes + 10; /* headers + 10 trailer blks */ 406 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 407 tapesize, fetapes); 408 } 409 410 /* 411 * Allocate tape buffer. 412 */ 413 if (!alloctape()) 414 quit( 415 "can't allocate tape buffers - try a smaller blocking factor.\n"); 416 417 startnewtape(1); 418 (void)time((time_t *)&(tstart_writing)); 419 dumpmap(usedinomap, TS_CLRI, maxino - 1); 420 421 msg("dumping (Pass III) [directories]\n"); 422 dirty = 0; /* XXX just to get gcc to shut up */ 423 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 424 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 425 dirty = *map++; 426 else 427 dirty >>= 1; 428 if ((dirty & 1) == 0) 429 continue; 430 /* 431 * Skip directory inodes deleted and maybe reallocated 432 */ 433 dp = getino(ino); 434 if ((dp->di_mode & IFMT) != IFDIR) 435 continue; 436 (void)dumpino(dp, ino); 437 } 438 439 msg("dumping (Pass IV) [regular files]\n"); 440 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 441 int mode; 442 443 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 444 dirty = *map++; 445 else 446 dirty >>= 1; 447 if ((dirty & 1) == 0) 448 continue; 449 /* 450 * Skip inodes deleted and reallocated as directories. 451 */ 452 dp = getino(ino); 453 mode = dp->di_mode & IFMT; 454 if (mode == IFDIR) 455 continue; 456 (void)dumpino(dp, ino); 457 } 458 459 (void)time((time_t *)&(tend_writing)); 460 spcl.c_type = TS_END; 461 for (i = 0; i < ntrec; i++) 462 writeheader(maxino - 1); 463 if (pipeout) 464 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 465 else 466 msg("DUMP: %ld tape blocks on %d volume%s\n", 467 spcl.c_tapea, spcl.c_volume, 468 (spcl.c_volume == 1) ? "" : "s"); 469 470 /* report dump performance, avoid division through zero */ 471 if (tend_writing - tstart_writing == 0) 472 msg("finished in less than a second\n"); 473 else 474 msg("finished in %d seconds, throughput %d KBytes/sec\n", 475 tend_writing - tstart_writing, 476 spcl.c_tapea / (tend_writing - tstart_writing)); 477 478 putdumptime(); 479 trewind(); 480 broadcast("DUMP IS DONE!\7\7\n"); 481 msg("DUMP IS DONE\n"); 482 Exit(X_FINOK); 483 /* NOTREACHED */ 484 } 485 486 static void 487 usage() 488 { 489 fprintf(stderr, 490 "usage: dump [-0123456789ac" 491 #ifdef KERBEROS 492 "k" 493 #endif 494 "nu] [-B records] [-b blocksize] [-d density] [-f file]\n" 495 " [-h level] [-s feet] [-T date] filesystem\n" 496 " dump [-W | -w]\n"); 497 exit(X_STARTUP); 498 } 499 500 /* 501 * Pick up a numeric argument. It must be nonnegative and in the given 502 * range (except that a vmax of 0 means unlimited). 503 */ 504 static long 505 numarg(meaning, vmin, vmax) 506 char *meaning; 507 long vmin, vmax; 508 { 509 char *p; 510 long val; 511 512 val = strtol(optarg, &p, 10); 513 if (*p) 514 errx(1, "illegal %s -- %s", meaning, optarg); 515 if (val < vmin || (vmax && val > vmax)) 516 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 517 return (val); 518 } 519 520 void 521 sig(signo) 522 int signo; 523 { 524 switch(signo) { 525 case SIGALRM: 526 case SIGBUS: 527 case SIGFPE: 528 case SIGHUP: 529 case SIGTERM: 530 case SIGTRAP: 531 if (pipeout) 532 quit("Signal on pipe: cannot recover\n"); 533 msg("Rewriting attempted as response to unknown signal.\n"); 534 (void)fflush(stderr); 535 (void)fflush(stdout); 536 close_rewind(); 537 exit(X_REWRITE); 538 /* NOTREACHED */ 539 case SIGSEGV: 540 msg("SIGSEGV: ABORTING!\n"); 541 (void)signal(SIGSEGV, SIG_DFL); 542 (void)kill(0, SIGSEGV); 543 /* NOTREACHED */ 544 } 545 } 546 547 char * 548 rawname(cp) 549 char *cp; 550 { 551 static char rawbuf[MAXPATHLEN]; 552 char *dp; 553 struct stat sb; 554 555 if (stat(cp, &sb) == 0) { 556 /* 557 * If the name already refers to a raw device, return 558 * it immediately without tampering. 559 */ 560 if ((sb.st_mode & S_IFMT) == S_IFCHR) 561 return (cp); 562 } 563 564 dp = strrchr(cp, '/'); 565 566 if (dp == NULL) 567 return (NULL); 568 *dp = '\0'; 569 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 570 rawbuf[MAXPATHLEN-1] = '\0'; 571 *dp = '/'; 572 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 573 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 574 return (rawbuf); 575 } 576 577 /* 578 * obsolete -- 579 * Change set of key letters and ordered arguments into something 580 * getopt(3) will like. 581 */ 582 static void 583 obsolete(argcp, argvp) 584 int *argcp; 585 char **argvp[]; 586 { 587 int argc, flags; 588 char *ap, **argv, *flagsp, **nargv, *p; 589 590 /* Setup. */ 591 argv = *argvp; 592 argc = *argcp; 593 594 /* Return if no arguments or first argument has leading dash. */ 595 ap = argv[1]; 596 if (argc == 1 || *ap == '-') 597 return; 598 599 /* Allocate space for new arguments. */ 600 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 601 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 602 err(1, NULL); 603 604 *nargv++ = *argv; 605 argv += 2; 606 607 for (flags = 0; *ap; ++ap) { 608 switch (*ap) { 609 case 'B': 610 case 'b': 611 case 'd': 612 case 'f': 613 case 'h': 614 case 's': 615 case 'T': 616 if (*argv == NULL) { 617 warnx("option requires an argument -- %c", *ap); 618 usage(); 619 } 620 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 621 err(1, NULL); 622 nargv[0][0] = '-'; 623 nargv[0][1] = *ap; 624 (void)strcpy(&nargv[0][2], *argv); 625 ++argv; 626 ++nargv; 627 break; 628 default: 629 if (!flags) { 630 *p++ = '-'; 631 flags = 1; 632 } 633 *p++ = *ap; 634 break; 635 } 636 } 637 638 /* Terminate flags. */ 639 if (flags) { 640 *p = '\0'; 641 *nargv++ = flagsp; 642 } 643 644 /* Copy remaining arguments. */ 645 while ((*nargv++ = *argv++)); 646 647 /* Update argument count. */ 648 *argcp = nargv - *argvp - 1; 649 } 650