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.17 1998/07/14 09:19:47 jkoshy 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 break; 151 152 case 'c': /* Tape is cart. not 9-track */ 153 cartridge = 1; 154 break; 155 156 case 'd': /* density, in bits per inch */ 157 density = numarg("density", 10L, 327670L) / 10; 158 if (density >= 625 && !bflag) 159 ntrec = HIGHDENSITYTREC; 160 break; 161 162 case 'f': /* output file */ 163 tape = 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 'T': /* time of last dump */ 185 spcl.c_ddate = unctime(optarg); 186 if (spcl.c_ddate < 0) { 187 (void)fprintf(stderr, "bad time \"%s\"\n", 188 optarg); 189 exit(X_STARTUP); 190 } 191 Tflag = 1; 192 lastlevel = '?'; 193 argc--; 194 argv++; 195 break; 196 197 case 'u': /* update /etc/dumpdates */ 198 uflag = 1; 199 break; 200 201 case 'W': /* what to do */ 202 case 'w': 203 lastdump(ch); 204 exit(X_FINOK); /* do nothing else */ 205 206 default: 207 usage(); 208 } 209 argc -= optind; 210 argv += optind; 211 212 if (argc < 1) { 213 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 214 exit(X_STARTUP); 215 } 216 disk = *argv++; 217 argc--; 218 if (argc >= 1) { 219 (void)fprintf(stderr, "Unknown arguments to dump:"); 220 while (argc--) 221 (void)fprintf(stderr, " %s", *argv++); 222 (void)fprintf(stderr, "\n"); 223 exit(X_STARTUP); 224 } 225 if (Tflag && uflag) { 226 (void)fprintf(stderr, 227 "You cannot use the T and u flags together.\n"); 228 exit(X_STARTUP); 229 } 230 if (strcmp(tape, "-") == 0) { 231 pipeout++; 232 tape = "standard output"; 233 } 234 235 if (blocksperfile) 236 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 237 else if (!unlimited) { 238 /* 239 * Determine how to default tape size and density 240 * 241 * density tape size 242 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 243 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 244 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 245 * (450*4 - slop) 246 * hilit19 hits again: " 247 */ 248 if (density == 0) 249 density = cartridge ? 100 : 160; 250 if (tsize == 0) 251 tsize = cartridge ? 1700L*120L : 2300L*120L; 252 } 253 254 if (strchr(tape, ':')) { 255 host = tape; 256 tape = strchr(host, ':'); 257 *tape++ = '\0'; 258 #ifdef RDUMP 259 if (index(tape, '\n')) { 260 (void)fprintf(stderr, "invalid characters in tape\n"); 261 exit(X_STARTUP); 262 } 263 if (rmthost(host) == 0) 264 exit(X_STARTUP); 265 #else 266 (void)fprintf(stderr, "remote dump not enabled\n"); 267 exit(X_STARTUP); 268 #endif 269 } 270 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 271 272 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 273 signal(SIGHUP, sig); 274 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 275 signal(SIGTRAP, sig); 276 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 277 signal(SIGFPE, sig); 278 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 279 signal(SIGBUS, sig); 280 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 281 signal(SIGSEGV, sig); 282 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 283 signal(SIGTERM, sig); 284 if (signal(SIGINT, interrupt) == SIG_IGN) 285 signal(SIGINT, SIG_IGN); 286 287 set_operators(); /* /etc/group snarfed */ 288 getfstab(); /* /etc/fstab snarfed */ 289 /* 290 * disk can be either the full special file name, 291 * the suffix of the special file name, 292 * the special name missing the leading '/', 293 * the file system name with or without the leading '/'. 294 */ 295 dt = fstabsearch(disk); 296 if (dt != NULL) { 297 disk = rawname(dt->fs_spec); 298 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 299 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 300 } else { 301 (void)strncpy(spcl.c_dev, disk, NAMELEN); 302 (void)strncpy(spcl.c_filesys, "an unlisted file system", 303 NAMELEN); 304 } 305 spcl.c_dev[NAMELEN-1]='\0'; 306 spcl.c_filesys[NAMELEN-1]='\0'; 307 (void)strcpy(spcl.c_label, "none"); 308 (void)gethostname(spcl.c_host, NAMELEN); 309 spcl.c_level = level - '0'; 310 spcl.c_type = TS_TAPE; 311 if (!Tflag) 312 getdumptime(); /* /etc/dumpdates snarfed */ 313 314 msg("Date of this level %c dump: %s", level, 315 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 316 msg("Date of last level %c dump: %s", lastlevel, 317 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 318 msg("Dumping %s ", disk); 319 if (dt != NULL) 320 msgtail("(%s) ", dt->fs_file); 321 if (host) 322 msgtail("to %s on host %s\n", tape, host); 323 else 324 msgtail("to %s\n", tape); 325 326 if ((diskfd = open(disk, O_RDONLY)) < 0) { 327 msg("Cannot open %s\n", disk); 328 exit(X_STARTUP); 329 } 330 sync(); 331 sblock = (struct fs *)sblock_buf; 332 bread(SBOFF, (char *) sblock, SBSIZE); 333 if (sblock->fs_magic != FS_MAGIC) 334 quit("bad sblock magic number\n"); 335 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 336 dev_bshift = ffs(dev_bsize) - 1; 337 if (dev_bsize != (1 << dev_bshift)) 338 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 339 tp_bshift = ffs(TP_BSIZE) - 1; 340 if (TP_BSIZE != (1 << tp_bshift)) 341 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 342 #ifdef FS_44INODEFMT 343 if (sblock->fs_inodefmt >= FS_44INODEFMT) 344 spcl.c_flags |= DR_NEWINODEFMT; 345 #endif 346 maxino = sblock->fs_ipg * sblock->fs_ncg; 347 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 348 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 349 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 350 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 351 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 352 353 nonodump = spcl.c_level < honorlevel; 354 355 msg("mapping (Pass I) [regular files]\n"); 356 anydirskipped = mapfiles(maxino, &tapesize); 357 358 msg("mapping (Pass II) [directories]\n"); 359 while (anydirskipped) { 360 anydirskipped = mapdirs(maxino, &tapesize); 361 } 362 363 if (pipeout || unlimited) { 364 tapesize += 10; /* 10 trailer blocks */ 365 msg("estimated %ld tape blocks.\n", tapesize); 366 } else { 367 double fetapes; 368 369 if (blocksperfile) 370 fetapes = (double) tapesize / blocksperfile; 371 else if (cartridge) { 372 /* Estimate number of tapes, assuming streaming stops at 373 the end of each block written, and not in mid-block. 374 Assume no erroneous blocks; this can be compensated 375 for with an artificially low tape size. */ 376 fetapes = 377 ( (double) tapesize /* blocks */ 378 * TP_BSIZE /* bytes/block */ 379 * (1.0/density) /* 0.1" / byte " */ 380 + 381 (double) tapesize /* blocks */ 382 * (1.0/ntrec) /* streaming-stops per block */ 383 * 15.48 /* 0.1" / streaming-stop " */ 384 ) * (1.0 / tsize ); /* tape / 0.1" " */ 385 } else { 386 /* Estimate number of tapes, for old fashioned 9-track 387 tape */ 388 int tenthsperirg = (density == 625) ? 3 : 7; 389 fetapes = 390 ( (double) tapesize /* blocks */ 391 * TP_BSIZE /* bytes / block */ 392 * (1.0/density) /* 0.1" / byte " */ 393 + 394 (double) tapesize /* blocks */ 395 * (1.0/ntrec) /* IRG's / block */ 396 * tenthsperirg /* 0.1" / IRG " */ 397 ) * (1.0 / tsize ); /* tape / 0.1" " */ 398 } 399 etapes = fetapes; /* truncating assignment */ 400 etapes++; 401 /* count the dumped inodes map on each additional tape */ 402 tapesize += (etapes - 1) * 403 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 404 tapesize += etapes + 10; /* headers + 10 trailer blks */ 405 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 406 tapesize, fetapes); 407 } 408 409 /* 410 * Allocate tape buffer. 411 */ 412 if (!alloctape()) 413 quit( 414 "can't allocate tape buffers - try a smaller blocking factor.\n"); 415 416 startnewtape(1); 417 (void)time((time_t *)&(tstart_writing)); 418 dumpmap(usedinomap, TS_CLRI, maxino - 1); 419 420 msg("dumping (Pass III) [directories]\n"); 421 dirty = 0; /* XXX just to get gcc to shut up */ 422 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 423 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 424 dirty = *map++; 425 else 426 dirty >>= 1; 427 if ((dirty & 1) == 0) 428 continue; 429 /* 430 * Skip directory inodes deleted and maybe reallocated 431 */ 432 dp = getino(ino); 433 if ((dp->di_mode & IFMT) != IFDIR) 434 continue; 435 (void)dumpino(dp, ino); 436 } 437 438 msg("dumping (Pass IV) [regular files]\n"); 439 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 440 int mode; 441 442 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 443 dirty = *map++; 444 else 445 dirty >>= 1; 446 if ((dirty & 1) == 0) 447 continue; 448 /* 449 * Skip inodes deleted and reallocated as directories. 450 */ 451 dp = getino(ino); 452 mode = dp->di_mode & IFMT; 453 if (mode == IFDIR) 454 continue; 455 (void)dumpino(dp, ino); 456 } 457 458 (void)time((time_t *)&(tend_writing)); 459 spcl.c_type = TS_END; 460 for (i = 0; i < ntrec; i++) 461 writeheader(maxino - 1); 462 if (pipeout) 463 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 464 else 465 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 466 spcl.c_tapea, spcl.c_volume); 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!\7\7\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 = strrchr(cp, '/'); 551 552 if (dp == NULL) 553 return (NULL); 554 *dp = '\0'; 555 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 556 rawbuf[MAXPATHLEN-1] = '\0'; 557 *dp = '/'; 558 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 559 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 560 return (rawbuf); 561 } 562 563 /* 564 * obsolete -- 565 * Change set of key letters and ordered arguments into something 566 * getopt(3) will like. 567 */ 568 static void 569 obsolete(argcp, argvp) 570 int *argcp; 571 char **argvp[]; 572 { 573 int argc, flags; 574 char *ap, **argv, *flagsp, **nargv, *p; 575 576 /* Setup. */ 577 argv = *argvp; 578 argc = *argcp; 579 580 /* Return if no arguments or first argument has leading dash. */ 581 ap = argv[1]; 582 if (argc == 1 || *ap == '-') 583 return; 584 585 /* Allocate space for new arguments. */ 586 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 587 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 588 err(1, NULL); 589 590 *nargv++ = *argv; 591 argv += 2; 592 593 for (flags = 0; *ap; ++ap) { 594 switch (*ap) { 595 case 'B': 596 case 'b': 597 case 'd': 598 case 'f': 599 case 'h': 600 case 's': 601 case 'T': 602 if (*argv == NULL) { 603 warnx("option requires an argument -- %c", *ap); 604 usage(); 605 } 606 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 607 err(1, NULL); 608 nargv[0][0] = '-'; 609 nargv[0][1] = *ap; 610 (void)strcpy(&nargv[0][2], *argv); 611 ++argv; 612 ++nargv; 613 break; 614 default: 615 if (!flags) { 616 *p++ = '-'; 617 flags = 1; 618 } 619 *p++ = *ap; 620 break; 621 } 622 } 623 624 /* Terminate flags. */ 625 if (flags) { 626 *p = '\0'; 627 *nargv++ = flagsp; 628 } 629 630 /* Copy remaining arguments. */ 631 while ((*nargv++ = *argv++)); 632 633 /* Update argument count. */ 634 *argcp = nargv - *argvp - 1; 635 } 636