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