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