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