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 msg("mapping (Pass I) [regular files]\n"); 366 anydirskipped = mapfiles(maxino, &tapesize); 367 368 msg("mapping (Pass II) [directories]\n"); 369 while (anydirskipped) { 370 anydirskipped = mapdirs(maxino, &tapesize); 371 } 372 373 if (pipeout || unlimited) { 374 tapesize += 10; /* 10 trailer blocks */ 375 msg("estimated %ld tape blocks.\n", tapesize); 376 } else { 377 double fetapes; 378 379 if (blocksperfile) 380 fetapes = (double) tapesize / blocksperfile; 381 else if (cartridge) { 382 /* Estimate number of tapes, assuming streaming stops at 383 the end of each block written, and not in mid-block. 384 Assume no erroneous blocks; this can be compensated 385 for with an artificially low tape size. */ 386 fetapes = 387 ( (double) tapesize /* blocks */ 388 * TP_BSIZE /* bytes/block */ 389 * (1.0/density) /* 0.1" / byte " */ 390 + 391 (double) tapesize /* blocks */ 392 * (1.0/ntrec) /* streaming-stops per block */ 393 * 15.48 /* 0.1" / streaming-stop " */ 394 ) * (1.0 / tsize ); /* tape / 0.1" " */ 395 } else { 396 /* Estimate number of tapes, for old fashioned 9-track 397 tape */ 398 int tenthsperirg = (density == 625) ? 3 : 7; 399 fetapes = 400 ( (double) tapesize /* blocks */ 401 * TP_BSIZE /* bytes / block */ 402 * (1.0/density) /* 0.1" / byte " */ 403 + 404 (double) tapesize /* blocks */ 405 * (1.0/ntrec) /* IRG's / block */ 406 * tenthsperirg /* 0.1" / IRG " */ 407 ) * (1.0 / tsize ); /* tape / 0.1" " */ 408 } 409 etapes = fetapes; /* truncating assignment */ 410 etapes++; 411 /* count the dumped inodes map on each additional tape */ 412 tapesize += (etapes - 1) * 413 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 414 tapesize += etapes + 10; /* headers + 10 trailer blks */ 415 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 416 tapesize, fetapes); 417 } 418 419 /* 420 * Allocate tape buffer. 421 */ 422 if (!alloctape()) 423 quit( 424 "can't allocate tape buffers - try a smaller blocking factor.\n"); 425 426 startnewtape(1); 427 (void)time((time_t *)&(tstart_writing)); 428 dumpmap(usedinomap, TS_CLRI, maxino - 1); 429 430 msg("dumping (Pass III) [directories]\n"); 431 dirty = 0; /* XXX just to get gcc to shut up */ 432 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 433 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 434 dirty = *map++; 435 else 436 dirty >>= 1; 437 if ((dirty & 1) == 0) 438 continue; 439 /* 440 * Skip directory inodes deleted and maybe reallocated 441 */ 442 dp = getino(ino); 443 if ((dp->di_mode & IFMT) != IFDIR) 444 continue; 445 (void)dumpino(dp, ino); 446 } 447 448 msg("dumping (Pass IV) [regular files]\n"); 449 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 450 int mode; 451 452 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 453 dirty = *map++; 454 else 455 dirty >>= 1; 456 if ((dirty & 1) == 0) 457 continue; 458 /* 459 * Skip inodes deleted and reallocated as directories. 460 */ 461 dp = getino(ino); 462 mode = dp->di_mode & IFMT; 463 if (mode == IFDIR) 464 continue; 465 (void)dumpino(dp, ino); 466 } 467 468 (void)time((time_t *)&(tend_writing)); 469 spcl.c_type = TS_END; 470 for (i = 0; i < ntrec; i++) 471 writeheader(maxino - 1); 472 if (pipeout) 473 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 474 else 475 msg("DUMP: %ld tape blocks on %d volume%s\n", 476 spcl.c_tapea, spcl.c_volume, 477 (spcl.c_volume == 1) ? "" : "s"); 478 479 /* report dump performance, avoid division through zero */ 480 if (tend_writing - tstart_writing == 0) 481 msg("finished in less than a second\n"); 482 else 483 msg("finished in %d seconds, throughput %d KBytes/sec\n", 484 tend_writing - tstart_writing, 485 spcl.c_tapea / (tend_writing - tstart_writing)); 486 487 putdumptime(); 488 trewind(); 489 broadcast("DUMP IS DONE!\a\a\n"); 490 msg("DUMP IS DONE\n"); 491 Exit(X_FINOK); 492 /* NOTREACHED */ 493 } 494 495 static void 496 usage() 497 { 498 fprintf(stderr, 499 "usage: dump [-0123456789ac" 500 #ifdef KERBEROS 501 "k" 502 #endif 503 "nu] [-B records] [-b blocksize] [-D dumpdates]\n" 504 " [-d density] [-f file ] [-h level] [-s feet] " 505 "[-T date] filesystem\n" 506 " dump [-W | -w]\n"); 507 exit(X_STARTUP); 508 } 509 510 /* 511 * Pick up a numeric argument. It must be nonnegative and in the given 512 * range (except that a vmax of 0 means unlimited). 513 */ 514 static long 515 numarg(meaning, vmin, vmax) 516 char *meaning; 517 long vmin, vmax; 518 { 519 char *p; 520 long val; 521 522 val = strtol(optarg, &p, 10); 523 if (*p) 524 errx(1, "illegal %s -- %s", meaning, optarg); 525 if (val < vmin || (vmax && val > vmax)) 526 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 527 return (val); 528 } 529 530 void 531 sig(signo) 532 int signo; 533 { 534 switch(signo) { 535 case SIGALRM: 536 case SIGBUS: 537 case SIGFPE: 538 case SIGHUP: 539 case SIGTERM: 540 case SIGTRAP: 541 if (pipeout) 542 quit("Signal on pipe: cannot recover\n"); 543 msg("Rewriting attempted as response to unknown signal.\n"); 544 (void)fflush(stderr); 545 (void)fflush(stdout); 546 close_rewind(); 547 exit(X_REWRITE); 548 /* NOTREACHED */ 549 case SIGSEGV: 550 msg("SIGSEGV: ABORTING!\n"); 551 (void)signal(SIGSEGV, SIG_DFL); 552 (void)kill(0, SIGSEGV); 553 /* NOTREACHED */ 554 } 555 } 556 557 char * 558 rawname(cp) 559 char *cp; 560 { 561 static char rawbuf[MAXPATHLEN]; 562 char *dp; 563 struct stat sb; 564 565 if (stat(cp, &sb) == 0) { 566 /* 567 * If the name already refers to a raw device, return 568 * it immediately without tampering. 569 */ 570 if ((sb.st_mode & S_IFMT) == S_IFCHR) 571 return (cp); 572 } 573 574 dp = strrchr(cp, '/'); 575 576 if (dp == NULL) 577 return (NULL); 578 *dp = '\0'; 579 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 580 rawbuf[MAXPATHLEN-1] = '\0'; 581 *dp = '/'; 582 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 583 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 584 return (rawbuf); 585 } 586 587 /* 588 * obsolete -- 589 * Change set of key letters and ordered arguments into something 590 * getopt(3) will like. 591 */ 592 static void 593 obsolete(argcp, argvp) 594 int *argcp; 595 char **argvp[]; 596 { 597 int argc, flags; 598 char *ap, **argv, *flagsp, **nargv, *p; 599 600 /* Setup. */ 601 argv = *argvp; 602 argc = *argcp; 603 604 /* Return if no arguments or first argument has leading dash. */ 605 ap = argv[1]; 606 if (argc == 1 || *ap == '-') 607 return; 608 609 /* Allocate space for new arguments. */ 610 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 611 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 612 err(1, NULL); 613 614 *nargv++ = *argv; 615 argv += 2; 616 617 for (flags = 0; *ap; ++ap) { 618 switch (*ap) { 619 case 'B': 620 case 'b': 621 case 'd': 622 case 'f': 623 case 'D': 624 case 'h': 625 case 's': 626 case 'T': 627 if (*argv == NULL) { 628 warnx("option requires an argument -- %c", *ap); 629 usage(); 630 } 631 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 632 err(1, NULL); 633 nargv[0][0] = '-'; 634 nargv[0][1] = *ap; 635 (void)strcpy(&nargv[0][2], *argv); 636 ++argv; 637 ++nargv; 638 break; 639 default: 640 if (!flags) { 641 *p++ = '-'; 642 flags = 1; 643 } 644 *p++ = *ap; 645 break; 646 } 647 } 648 649 /* Terminate flags. */ 650 if (flags) { 651 *p = '\0'; 652 *nargv++ = flagsp; 653 } 654 655 /* Copy remaining arguments. */ 656 while ((*nargv++ = *argv++)); 657 658 /* Update argument count. */ 659 *argcp = nargv - *argvp - 1; 660 } 661