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 "$Id: main.c,v 1.16 1998/06/15 06:58:10 charnier Exp $"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/time.h> 50 #ifdef sunos 51 #include <sys/vnode.h> 52 53 #include <ufs/inode.h> 54 #include <ufs/fs.h> 55 #else 56 #include <ufs/ufs/dinode.h> 57 #include <ufs/ffs/fs.h> 58 #endif 59 60 #include <protocols/dumprestore.h> 61 62 #include <ctype.h> 63 #include <err.h> 64 #include <fcntl.h> 65 #include <fstab.h> 66 #include <signal.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 72 #include "dump.h" 73 #include "pathnames.h" 74 75 #ifndef SBOFF 76 #define SBOFF (SBLOCK * DEV_BSIZE) 77 #endif 78 79 int notify = 0; /* notify operator flag */ 80 int blockswritten = 0; /* number of blocks written on current tape */ 81 int tapeno = 0; /* current tape number */ 82 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ 83 int ntrec = NTREC; /* # tape blocks in each tape record */ 84 int cartridge = 0; /* Assume non-cartridge tape */ 85 int dokerberos = 0; /* Use Kerberos authentication */ 86 long dev_bsize = 1; /* recalculated below */ 87 long blocksperfile; /* output blocks per file */ 88 char *host = NULL; /* remote host (if any) */ 89 90 static long numarg __P((char *, long, long)); 91 static void obsolete __P((int *, char **[])); 92 static void usage __P((void)); 93 94 int 95 main(argc, argv) 96 int argc; 97 char *argv[]; 98 { 99 register ino_t ino; 100 register int dirty; 101 register struct dinode *dp; 102 register struct fstab *dt; 103 register char *map; 104 register int ch; 105 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 106 ino_t maxino; 107 108 spcl.c_date = 0; 109 (void)time((time_t *)&spcl.c_date); 110 111 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 112 if ((tape = getenv("TAPE")) == NULL) 113 tape = _PATH_DEFTAPE; 114 dumpdates = _PATH_DUMPDATES; 115 temp = _PATH_DTMP; 116 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 117 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 118 level = '0'; 119 120 if (argc < 2) 121 usage(); 122 123 obsolete(&argc, &argv); 124 #ifdef KERBEROS 125 #define optstring "0123456789aB:b:cd:f:h:kns:T:uWw" 126 #else 127 #define optstring "0123456789aB:b:cd:f:h:ns:T:uWw" 128 #endif 129 while ((ch = getopt(argc, argv, optstring)) != -1) 130 #undef optstring 131 switch (ch) { 132 /* dump level */ 133 case '0': case '1': case '2': case '3': case '4': 134 case '5': case '6': case '7': case '8': case '9': 135 level = ch; 136 break; 137 138 case 'a': /* `auto-size', Write to EOM. */ 139 unlimited = 1; 140 break; 141 142 case 'B': /* blocks per output file */ 143 blocksperfile = numarg("number of blocks per file", 144 1L, 0L); 145 break; 146 147 case 'b': /* blocks per tape write */ 148 ntrec = numarg("number of blocks per write", 149 1L, 1000L); 150 /* 151 * XXX 152 * physio(9) currently slices all requests to 153 * 64 KB chunks. So now, if somebody entered 154 * e.g. 96 KB block size here, he would effectively 155 * yield one 64 KB and one 32 KB block, which 156 * restore cannot handle. 157 * Thus we currently enforce pyhsio(9)'s limit 158 * here, too. 159 */ 160 if ( ntrec > 64 ) { 161 msg("please choose a blocksize <= 64\n"); 162 exit(X_STARTUP); 163 } 164 break; 165 166 case 'c': /* Tape is cart. not 9-track */ 167 cartridge = 1; 168 break; 169 170 case 'd': /* density, in bits per inch */ 171 density = numarg("density", 10L, 327670L) / 10; 172 if (density >= 625 && !bflag) 173 ntrec = HIGHDENSITYTREC; 174 break; 175 176 case 'f': /* output file */ 177 tape = optarg; 178 break; 179 180 case 'h': 181 honorlevel = numarg("honor level", 0L, 10L); 182 break; 183 184 #ifdef KERBEROS 185 case 'k': 186 dokerberos = 1; 187 break; 188 #endif 189 190 case 'n': /* notify operators */ 191 notify = 1; 192 break; 193 194 case 's': /* tape size, feet */ 195 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 196 break; 197 198 case 'T': /* time of last dump */ 199 spcl.c_ddate = unctime(optarg); 200 if (spcl.c_ddate < 0) { 201 (void)fprintf(stderr, "bad time \"%s\"\n", 202 optarg); 203 exit(X_STARTUP); 204 } 205 Tflag = 1; 206 lastlevel = '?'; 207 argc--; 208 argv++; 209 break; 210 211 case 'u': /* update /etc/dumpdates */ 212 uflag = 1; 213 break; 214 215 case 'W': /* what to do */ 216 case 'w': 217 lastdump(ch); 218 exit(X_FINOK); /* do nothing else */ 219 220 default: 221 usage(); 222 } 223 argc -= optind; 224 argv += optind; 225 226 if (argc < 1) { 227 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 228 exit(X_STARTUP); 229 } 230 disk = *argv++; 231 argc--; 232 if (argc >= 1) { 233 (void)fprintf(stderr, "Unknown arguments to dump:"); 234 while (argc--) 235 (void)fprintf(stderr, " %s", *argv++); 236 (void)fprintf(stderr, "\n"); 237 exit(X_STARTUP); 238 } 239 if (Tflag && uflag) { 240 (void)fprintf(stderr, 241 "You cannot use the T and u flags together.\n"); 242 exit(X_STARTUP); 243 } 244 if (strcmp(tape, "-") == 0) { 245 pipeout++; 246 tape = "standard output"; 247 } 248 249 if (blocksperfile) 250 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 251 else if (!unlimited) { 252 /* 253 * Determine how to default tape size and density 254 * 255 * density tape size 256 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 257 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 258 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 259 * (450*4 - slop) 260 * hilit19 hits again: " 261 */ 262 if (density == 0) 263 density = cartridge ? 100 : 160; 264 if (tsize == 0) 265 tsize = cartridge ? 1700L*120L : 2300L*120L; 266 } 267 268 if (strchr(tape, ':')) { 269 host = tape; 270 tape = strchr(host, ':'); 271 *tape++ = '\0'; 272 #ifdef RDUMP 273 if (index(tape, '\n')) { 274 (void)fprintf(stderr, "invalid characters in tape\n"); 275 exit(X_STARTUP); 276 } 277 if (rmthost(host) == 0) 278 exit(X_STARTUP); 279 #else 280 (void)fprintf(stderr, "remote dump not enabled\n"); 281 exit(X_STARTUP); 282 #endif 283 } 284 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 285 286 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 287 signal(SIGHUP, sig); 288 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 289 signal(SIGTRAP, sig); 290 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 291 signal(SIGFPE, sig); 292 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 293 signal(SIGBUS, sig); 294 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 295 signal(SIGSEGV, sig); 296 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 297 signal(SIGTERM, sig); 298 if (signal(SIGINT, interrupt) == SIG_IGN) 299 signal(SIGINT, SIG_IGN); 300 301 set_operators(); /* /etc/group snarfed */ 302 getfstab(); /* /etc/fstab snarfed */ 303 /* 304 * disk can be either the full special file name, 305 * the suffix of the special file name, 306 * the special name missing the leading '/', 307 * the file system name with or without the leading '/'. 308 */ 309 dt = fstabsearch(disk); 310 if (dt != NULL) { 311 disk = rawname(dt->fs_spec); 312 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 313 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 314 } else { 315 (void)strncpy(spcl.c_dev, disk, NAMELEN); 316 (void)strncpy(spcl.c_filesys, "an unlisted file system", 317 NAMELEN); 318 } 319 spcl.c_dev[NAMELEN-1]='\0'; 320 spcl.c_filesys[NAMELEN-1]='\0'; 321 (void)strcpy(spcl.c_label, "none"); 322 (void)gethostname(spcl.c_host, NAMELEN); 323 spcl.c_level = level - '0'; 324 spcl.c_type = TS_TAPE; 325 if (!Tflag) 326 getdumptime(); /* /etc/dumpdates snarfed */ 327 328 msg("Date of this level %c dump: %s", level, 329 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 330 msg("Date of last level %c dump: %s", lastlevel, 331 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 332 msg("Dumping %s ", disk); 333 if (dt != NULL) 334 msgtail("(%s) ", dt->fs_file); 335 if (host) 336 msgtail("to %s on host %s\n", tape, host); 337 else 338 msgtail("to %s\n", tape); 339 340 if ((diskfd = open(disk, O_RDONLY)) < 0) { 341 msg("Cannot open %s\n", disk); 342 exit(X_STARTUP); 343 } 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 msg("mapping (Pass I) [regular files]\n"); 370 anydirskipped = mapfiles(maxino, &tapesize); 371 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 msg("dumping (Pass III) [directories]\n"); 435 dirty = 0; /* XXX just to get gcc to shut up */ 436 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 437 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 438 dirty = *map++; 439 else 440 dirty >>= 1; 441 if ((dirty & 1) == 0) 442 continue; 443 /* 444 * Skip directory inodes deleted and maybe reallocated 445 */ 446 dp = getino(ino); 447 if ((dp->di_mode & IFMT) != IFDIR) 448 continue; 449 (void)dumpino(dp, ino); 450 } 451 452 msg("dumping (Pass IV) [regular files]\n"); 453 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 454 int mode; 455 456 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 457 dirty = *map++; 458 else 459 dirty >>= 1; 460 if ((dirty & 1) == 0) 461 continue; 462 /* 463 * Skip inodes deleted and reallocated as directories. 464 */ 465 dp = getino(ino); 466 mode = dp->di_mode & IFMT; 467 if (mode == IFDIR) 468 continue; 469 (void)dumpino(dp, ino); 470 } 471 472 (void)time((time_t *)&(tend_writing)); 473 spcl.c_type = TS_END; 474 for (i = 0; i < ntrec; i++) 475 writeheader(maxino - 1); 476 if (pipeout) 477 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 478 else 479 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 480 spcl.c_tapea, spcl.c_volume); 481 482 /* report dump performance, avoid division through zero */ 483 if (tend_writing - tstart_writing == 0) 484 msg("finished in less than a second\n"); 485 else 486 msg("finished in %d seconds, throughput %d KBytes/sec\n", 487 tend_writing - tstart_writing, 488 spcl.c_tapea / (tend_writing - tstart_writing)); 489 490 putdumptime(); 491 trewind(); 492 broadcast("DUMP IS DONE!\7\7\n"); 493 msg("DUMP IS DONE\n"); 494 Exit(X_FINOK); 495 /* NOTREACHED */ 496 } 497 498 static void 499 usage() 500 { 501 fprintf(stderr, 502 "usage: dump [-0123456789ac" 503 #ifdef KERBEROS 504 "k" 505 #endif 506 "nu] [-B records] [-b blocksize] [-d density] [-f file]\n" 507 " [-h level] [-s feet] [-T date] filesystem\n" 508 " dump [-W | -w]\n"); 509 exit(X_STARTUP); 510 } 511 512 /* 513 * Pick up a numeric argument. It must be nonnegative and in the given 514 * range (except that a vmax of 0 means unlimited). 515 */ 516 static long 517 numarg(meaning, vmin, vmax) 518 char *meaning; 519 long vmin, vmax; 520 { 521 char *p; 522 long val; 523 524 val = strtol(optarg, &p, 10); 525 if (*p) 526 errx(1, "illegal %s -- %s", meaning, optarg); 527 if (val < vmin || (vmax && val > vmax)) 528 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 529 return (val); 530 } 531 532 void 533 sig(signo) 534 int signo; 535 { 536 switch(signo) { 537 case SIGALRM: 538 case SIGBUS: 539 case SIGFPE: 540 case SIGHUP: 541 case SIGTERM: 542 case SIGTRAP: 543 if (pipeout) 544 quit("Signal on pipe: cannot recover\n"); 545 msg("Rewriting attempted as response to unknown signal.\n"); 546 (void)fflush(stderr); 547 (void)fflush(stdout); 548 close_rewind(); 549 exit(X_REWRITE); 550 /* NOTREACHED */ 551 case SIGSEGV: 552 msg("SIGSEGV: ABORTING!\n"); 553 (void)signal(SIGSEGV, SIG_DFL); 554 (void)kill(0, SIGSEGV); 555 /* NOTREACHED */ 556 } 557 } 558 559 char * 560 rawname(cp) 561 char *cp; 562 { 563 static char rawbuf[MAXPATHLEN]; 564 char *dp = strrchr(cp, '/'); 565 566 if (dp == NULL) 567 return (NULL); 568 *dp = '\0'; 569 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 570 rawbuf[MAXPATHLEN-1] = '\0'; 571 *dp = '/'; 572 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 573 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 574 return (rawbuf); 575 } 576 577 /* 578 * obsolete -- 579 * Change set of key letters and ordered arguments into something 580 * getopt(3) will like. 581 */ 582 static void 583 obsolete(argcp, argvp) 584 int *argcp; 585 char **argvp[]; 586 { 587 int argc, flags; 588 char *ap, **argv, *flagsp, **nargv, *p; 589 590 /* Setup. */ 591 argv = *argvp; 592 argc = *argcp; 593 594 /* Return if no arguments or first argument has leading dash. */ 595 ap = argv[1]; 596 if (argc == 1 || *ap == '-') 597 return; 598 599 /* Allocate space for new arguments. */ 600 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 601 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 602 err(1, NULL); 603 604 *nargv++ = *argv; 605 argv += 2; 606 607 for (flags = 0; *ap; ++ap) { 608 switch (*ap) { 609 case 'B': 610 case 'b': 611 case 'd': 612 case 'f': 613 case 'h': 614 case 's': 615 case 'T': 616 if (*argv == NULL) { 617 warnx("option requires an argument -- %c", *ap); 618 usage(); 619 } 620 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 621 err(1, NULL); 622 nargv[0][0] = '-'; 623 nargv[0][1] = *ap; 624 (void)strcpy(&nargv[0][2], *argv); 625 ++argv; 626 ++nargv; 627 break; 628 default: 629 if (!flags) { 630 *p++ = '-'; 631 flags = 1; 632 } 633 *p++ = *ap; 634 break; 635 } 636 } 637 638 /* Terminate flags. */ 639 if (flags) { 640 *p = '\0'; 641 *nargv++ = flagsp; 642 } 643 644 /* Copy remaining arguments. */ 645 while ((*nargv++ = *argv++)); 646 647 /* Update argument count. */ 648 *argcp = nargv - *argvp - 1; 649 } 650