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 char *tmsg; 109 time_t t; 110 111 spcl.c_date = time_to_time32(time(NULL)); 112 113 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 114 if ((tape = getenv("TAPE")) == NULL) 115 tape = _PATH_DEFTAPE; 116 dumpdates = _PATH_DUMPDATES; 117 temp = _PATH_DTMP; 118 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 119 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 120 level = '0'; 121 122 if (argc < 2) 123 usage(); 124 125 obsolete(&argc, &argv); 126 #ifdef KERBEROS 127 #define optstring "0123456789aB:b:cd:f:h:kns:T:uWwD:" 128 #else 129 #define optstring "0123456789aB:b:cd:f:h:ns:T:uWwD:" 130 #endif 131 while ((ch = getopt(argc, argv, optstring)) != -1) 132 #undef optstring 133 switch (ch) { 134 /* dump level */ 135 case '0': case '1': case '2': case '3': case '4': 136 case '5': case '6': case '7': case '8': case '9': 137 level = ch; 138 break; 139 140 case 'a': /* `auto-size', Write to EOM. */ 141 unlimited = 1; 142 break; 143 144 case 'B': /* blocks per output file */ 145 blocksperfile = numarg("number of blocks per file", 146 1L, 0L); 147 break; 148 149 case 'b': /* blocks per tape write */ 150 ntrec = numarg("number of blocks per write", 151 1L, 1000L); 152 break; 153 154 case 'c': /* Tape is cart. not 9-track */ 155 cartridge = 1; 156 break; 157 158 case 'd': /* density, in bits per inch */ 159 density = numarg("density", 10L, 327670L) / 10; 160 if (density >= 625 && !bflag) 161 ntrec = HIGHDENSITYTREC; 162 break; 163 164 case 'f': /* output file */ 165 tape = optarg; 166 break; 167 168 case 'D': 169 dumpdates = optarg; 170 break; 171 172 case 'h': 173 honorlevel = numarg("honor level", 0L, 10L); 174 break; 175 176 #ifdef KERBEROS 177 case 'k': 178 dokerberos = 1; 179 break; 180 #endif 181 182 case 'n': /* notify operators */ 183 notify = 1; 184 break; 185 186 case 's': /* tape size, feet */ 187 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 188 break; 189 190 case 'T': /* time of last dump */ 191 spcl.c_ddate = unctime(optarg); 192 if (spcl.c_ddate < 0) { 193 (void)fprintf(stderr, "bad time \"%s\"\n", 194 optarg); 195 exit(X_STARTUP); 196 } 197 Tflag = 1; 198 lastlevel = '?'; 199 break; 200 201 case 'u': /* update /etc/dumpdates */ 202 uflag = 1; 203 break; 204 205 case 'W': /* what to do */ 206 case 'w': 207 lastdump(ch); 208 exit(X_FINOK); /* do nothing else */ 209 210 default: 211 usage(); 212 } 213 argc -= optind; 214 argv += optind; 215 216 if (argc < 1) { 217 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 218 exit(X_STARTUP); 219 } 220 disk = *argv++; 221 argc--; 222 if (argc >= 1) { 223 (void)fprintf(stderr, "Unknown arguments to dump:"); 224 while (argc--) 225 (void)fprintf(stderr, " %s", *argv++); 226 (void)fprintf(stderr, "\n"); 227 exit(X_STARTUP); 228 } 229 if (Tflag && uflag) { 230 (void)fprintf(stderr, 231 "You cannot use the T and u flags together.\n"); 232 exit(X_STARTUP); 233 } 234 if (strcmp(tape, "-") == 0) { 235 pipeout++; 236 tape = "standard output"; 237 } 238 239 if (blocksperfile) 240 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 241 else if (!unlimited) { 242 /* 243 * Determine how to default tape size and density 244 * 245 * density tape size 246 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 247 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 248 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 249 * (450*4 - slop) 250 * hilit19 hits again: " 251 */ 252 if (density == 0) 253 density = cartridge ? 100 : 160; 254 if (tsize == 0) 255 tsize = cartridge ? 1700L*120L : 2300L*120L; 256 } 257 258 if (strchr(tape, ':')) { 259 host = tape; 260 tape = strchr(host, ':'); 261 *tape++ = '\0'; 262 #ifdef RDUMP 263 if (index(tape, '\n')) { 264 (void)fprintf(stderr, "invalid characters in tape\n"); 265 exit(X_STARTUP); 266 } 267 if (rmthost(host) == 0) 268 exit(X_STARTUP); 269 #else 270 (void)fprintf(stderr, "remote dump not enabled\n"); 271 exit(X_STARTUP); 272 #endif 273 } 274 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 275 276 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 277 signal(SIGHUP, sig); 278 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 279 signal(SIGTRAP, sig); 280 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 281 signal(SIGFPE, sig); 282 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 283 signal(SIGBUS, sig); 284 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 285 signal(SIGSEGV, sig); 286 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 287 signal(SIGTERM, sig); 288 if (signal(SIGINT, interrupt) == SIG_IGN) 289 signal(SIGINT, SIG_IGN); 290 291 getfstab(); /* /etc/fstab snarfed */ 292 /* 293 * disk can be either the full special file name, 294 * the suffix of the special file name, 295 * the special name missing the leading '/', 296 * the file system name with or without the leading '/'. 297 */ 298 dt = fstabsearch(disk); 299 if (dt != NULL) { 300 disk = rawname(dt->fs_spec); 301 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 302 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 303 } else { 304 (void)strncpy(spcl.c_dev, disk, NAMELEN); 305 (void)strncpy(spcl.c_filesys, "an unlisted file system", 306 NAMELEN); 307 } 308 spcl.c_dev[NAMELEN-1]='\0'; 309 spcl.c_filesys[NAMELEN-1]='\0'; 310 (void)strcpy(spcl.c_label, "none"); 311 (void)gethostname(spcl.c_host, NAMELEN); 312 spcl.c_level = level - '0'; 313 spcl.c_type = TS_TAPE; 314 if (!Tflag) 315 getdumptime(); /* /etc/dumpdates snarfed */ 316 317 if (spcl.c_date == 0) { 318 tmsg = "the epoch\n"; 319 } else { 320 time_t t = time32_to_time(spcl.c_date); 321 tmsg = ctime(&t); 322 } 323 msg("Date of this level %c dump: %s", level, tmsg); 324 if (spcl.c_ddate == 0) { 325 tmsg = "the epoch\n"; 326 } else { 327 time_t t = time32_to_time(spcl.c_ddate); 328 tmsg = ctime(&t); 329 } 330 msg("Date of last level %c dump: %s", lastlevel, tmsg); 331 msg("Dumping %s ", disk); 332 if (dt != NULL) 333 msgtail("(%s) ", dt->fs_file); 334 if (host) 335 msgtail("to %s on host %s\n", tape, host); 336 else 337 msgtail("to %s\n", tape); 338 339 if ((diskfd = open(disk, O_RDONLY)) < 0) { 340 msg("Cannot open %s\n", disk); 341 exit(X_STARTUP); 342 } 343 sync(); 344 sblock = (struct fs *)sblock_buf; 345 bread(SBOFF, (char *) sblock, SBSIZE); 346 if (sblock->fs_magic != FS_MAGIC) 347 quit("bad sblock magic number\n"); 348 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 349 dev_bshift = ffs(dev_bsize) - 1; 350 if (dev_bsize != (1 << dev_bshift)) 351 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 352 tp_bshift = ffs(TP_BSIZE) - 1; 353 if (TP_BSIZE != (1 << tp_bshift)) 354 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 355 #ifdef FS_44INODEFMT 356 if (sblock->fs_inodefmt >= FS_44INODEFMT) 357 spcl.c_flags |= DR_NEWINODEFMT; 358 #endif 359 maxino = sblock->fs_ipg * sblock->fs_ncg; 360 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 361 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 362 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 363 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 364 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 365 366 nonodump = spcl.c_level < honorlevel; 367 368 msg("mapping (Pass I) [regular files]\n"); 369 anydirskipped = mapfiles(maxino, &tapesize); 370 371 msg("mapping (Pass II) [directories]\n"); 372 while (anydirskipped) { 373 anydirskipped = mapdirs(maxino, &tapesize); 374 } 375 376 if (pipeout || unlimited) { 377 tapesize += 10; /* 10 trailer blocks */ 378 msg("estimated %ld tape blocks.\n", tapesize); 379 } else { 380 double fetapes; 381 382 if (blocksperfile) 383 fetapes = (double) tapesize / blocksperfile; 384 else if (cartridge) { 385 /* Estimate number of tapes, assuming streaming stops at 386 the end of each block written, and not in mid-block. 387 Assume no erroneous blocks; this can be compensated 388 for with an artificially low tape size. */ 389 fetapes = 390 ( (double) tapesize /* blocks */ 391 * TP_BSIZE /* bytes/block */ 392 * (1.0/density) /* 0.1" / byte " */ 393 + 394 (double) tapesize /* blocks */ 395 * (1.0/ntrec) /* streaming-stops per block */ 396 * 15.48 /* 0.1" / streaming-stop " */ 397 ) * (1.0 / tsize ); /* tape / 0.1" " */ 398 } else { 399 /* Estimate number of tapes, for old fashioned 9-track 400 tape */ 401 int tenthsperirg = (density == 625) ? 3 : 7; 402 fetapes = 403 ( (double) tapesize /* blocks */ 404 * TP_BSIZE /* bytes / block */ 405 * (1.0/density) /* 0.1" / byte " */ 406 + 407 (double) tapesize /* blocks */ 408 * (1.0/ntrec) /* IRG's / block */ 409 * tenthsperirg /* 0.1" / IRG " */ 410 ) * (1.0 / tsize ); /* tape / 0.1" " */ 411 } 412 etapes = fetapes; /* truncating assignment */ 413 etapes++; 414 /* count the dumped inodes map on each additional tape */ 415 tapesize += (etapes - 1) * 416 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 417 tapesize += etapes + 10; /* headers + 10 trailer blks */ 418 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 419 tapesize, fetapes); 420 } 421 422 /* 423 * Allocate tape buffer. 424 */ 425 if (!alloctape()) 426 quit( 427 "can't allocate tape buffers - try a smaller blocking factor.\n"); 428 429 startnewtape(1); 430 (void)time((time_t *)&(tstart_writing)); 431 dumpmap(usedinomap, TS_CLRI, maxino - 1); 432 433 msg("dumping (Pass III) [directories]\n"); 434 dirty = 0; /* XXX just to get gcc to shut up */ 435 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 436 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 437 dirty = *map++; 438 else 439 dirty >>= 1; 440 if ((dirty & 1) == 0) 441 continue; 442 /* 443 * Skip directory inodes deleted and maybe reallocated 444 */ 445 dp = getino(ino); 446 if ((dp->di_mode & IFMT) != IFDIR) 447 continue; 448 (void)dumpino(dp, ino); 449 } 450 451 msg("dumping (Pass IV) [regular files]\n"); 452 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 453 int mode; 454 455 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 456 dirty = *map++; 457 else 458 dirty >>= 1; 459 if ((dirty & 1) == 0) 460 continue; 461 /* 462 * Skip inodes deleted and reallocated as directories. 463 */ 464 dp = getino(ino); 465 mode = dp->di_mode & IFMT; 466 if (mode == IFDIR) 467 continue; 468 (void)dumpino(dp, ino); 469 } 470 471 (void)time((time_t *)&(tend_writing)); 472 spcl.c_type = TS_END; 473 for (i = 0; i < ntrec; i++) 474 writeheader(maxino - 1); 475 if (pipeout) 476 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 477 else 478 msg("DUMP: %ld tape blocks on %d volume%s\n", 479 spcl.c_tapea, spcl.c_volume, 480 (spcl.c_volume == 1) ? "" : "s"); 481 482 /* report dump performance, avoid division through zero */ 483 if (tend_writing - tstart_writing == 0) 484 msg("finished in less than a second\n"); 485 else 486 msg("finished in %d seconds, throughput %d KBytes/sec\n", 487 tend_writing - tstart_writing, 488 spcl.c_tapea / (tend_writing - tstart_writing)); 489 490 putdumptime(); 491 trewind(); 492 broadcast("DUMP IS DONE!\a\a\n"); 493 msg("DUMP IS DONE\n"); 494 Exit(X_FINOK); 495 /* NOTREACHED */ 496 } 497 498 static void 499 usage() 500 { 501 fprintf(stderr, 502 "usage: dump [-0123456789ac" 503 #ifdef KERBEROS 504 "k" 505 #endif 506 "nu] [-B records] [-b blocksize] [-D dumpdates]\n" 507 " [-d density] [-f file ] [-h level] [-s feet] " 508 "[-T date] filesystem\n" 509 " dump [-W | -w]\n"); 510 exit(X_STARTUP); 511 } 512 513 /* 514 * Pick up a numeric argument. It must be nonnegative and in the given 515 * range (except that a vmax of 0 means unlimited). 516 */ 517 static long 518 numarg(meaning, vmin, vmax) 519 char *meaning; 520 long vmin, vmax; 521 { 522 char *p; 523 long val; 524 525 val = strtol(optarg, &p, 10); 526 if (*p) 527 errx(1, "illegal %s -- %s", meaning, optarg); 528 if (val < vmin || (vmax && val > vmax)) 529 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 530 return (val); 531 } 532 533 void 534 sig(signo) 535 int signo; 536 { 537 switch(signo) { 538 case SIGALRM: 539 case SIGBUS: 540 case SIGFPE: 541 case SIGHUP: 542 case SIGTERM: 543 case SIGTRAP: 544 if (pipeout) 545 quit("Signal on pipe: cannot recover\n"); 546 msg("Rewriting attempted as response to unknown signal.\n"); 547 (void)fflush(stderr); 548 (void)fflush(stdout); 549 close_rewind(); 550 exit(X_REWRITE); 551 /* NOTREACHED */ 552 case SIGSEGV: 553 msg("SIGSEGV: ABORTING!\n"); 554 (void)signal(SIGSEGV, SIG_DFL); 555 (void)kill(0, SIGSEGV); 556 /* NOTREACHED */ 557 } 558 } 559 560 char * 561 rawname(cp) 562 char *cp; 563 { 564 static char rawbuf[MAXPATHLEN]; 565 char *dp; 566 struct stat sb; 567 568 if (stat(cp, &sb) == 0) { 569 /* 570 * If the name already refers to a raw device, return 571 * it immediately without tampering. 572 */ 573 if ((sb.st_mode & S_IFMT) == S_IFCHR) 574 return (cp); 575 } 576 577 dp = strrchr(cp, '/'); 578 579 if (dp == NULL) 580 return (NULL); 581 *dp = '\0'; 582 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 583 rawbuf[MAXPATHLEN-1] = '\0'; 584 *dp = '/'; 585 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 586 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 587 return (rawbuf); 588 } 589 590 /* 591 * obsolete -- 592 * Change set of key letters and ordered arguments into something 593 * getopt(3) will like. 594 */ 595 static void 596 obsolete(argcp, argvp) 597 int *argcp; 598 char **argvp[]; 599 { 600 int argc, flags; 601 char *ap, **argv, *flagsp, **nargv, *p; 602 603 /* Setup. */ 604 argv = *argvp; 605 argc = *argcp; 606 607 /* Return if no arguments or first argument has leading dash. */ 608 ap = argv[1]; 609 if (argc == 1 || *ap == '-') 610 return; 611 612 /* Allocate space for new arguments. */ 613 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 614 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 615 err(1, NULL); 616 617 *nargv++ = *argv; 618 argv += 2; 619 620 for (flags = 0; *ap; ++ap) { 621 switch (*ap) { 622 case 'B': 623 case 'b': 624 case 'd': 625 case 'f': 626 case 'D': 627 case 'h': 628 case 's': 629 case 'T': 630 if (*argv == NULL) { 631 warnx("option requires an argument -- %c", *ap); 632 usage(); 633 } 634 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 635 err(1, NULL); 636 nargv[0][0] = '-'; 637 nargv[0][1] = *ap; 638 (void)strcpy(&nargv[0][2], *argv); 639 ++argv; 640 ++nargv; 641 break; 642 default: 643 if (!flags) { 644 *p++ = '-'; 645 flags = 1; 646 } 647 *p++ = *ap; 648 break; 649 } 650 } 651 652 /* Terminate flags. */ 653 if (flags) { 654 *p = '\0'; 655 *nargv++ = flagsp; 656 } 657 658 /* Copy remaining arguments. */ 659 while ((*nargv++ = *argv++)); 660 661 /* Update argument count. */ 662 *argcp = nargv - *argvp - 1; 663 } 664