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