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 * $Id$ 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/time.h> 48 #ifdef sunos 49 #include <sys/vnode.h> 50 51 #include <ufs/inode.h> 52 #include <ufs/fs.h> 53 #else 54 #include <ufs/ufs/dinode.h> 55 #include <ufs/ffs/fs.h> 56 #endif 57 58 #include <protocols/dumprestore.h> 59 60 #include <ctype.h> 61 #include <err.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <fstab.h> 65 #include <signal.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 71 #include "dump.h" 72 #include "pathnames.h" 73 74 #ifndef SBOFF 75 #define SBOFF (SBLOCK * DEV_BSIZE) 76 #endif 77 78 int notify = 0; /* notify operator flag */ 79 int blockswritten = 0; /* number of blocks written on current tape */ 80 int tapeno = 0; /* current tape number */ 81 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ 82 int ntrec = NTREC; /* # tape blocks in each tape record */ 83 int cartridge = 0; /* Assume non-cartridge tape */ 84 int dokerberos = 0; /* Use Kerberos authentication */ 85 long dev_bsize = 1; /* recalculated below */ 86 long blocksperfile; /* output blocks per file */ 87 char *host = NULL; /* remote host (if any) */ 88 89 static long numarg __P((char *, long, long)); 90 static void obsolete __P((int *, char **[])); 91 static void usage __P((void)); 92 93 int 94 main(argc, argv) 95 int argc; 96 char *argv[]; 97 { 98 register ino_t ino; 99 register int dirty; 100 register struct dinode *dp; 101 register struct fstab *dt; 102 register char *map; 103 register int ch; 104 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 105 ino_t maxino; 106 107 spcl.c_date = 0; 108 (void)time((time_t *)&spcl.c_date); 109 110 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 111 if ((tape = getenv("TAPE")) == NULL) 112 tape = _PATH_DEFTAPE; 113 dumpdates = _PATH_DUMPDATES; 114 temp = _PATH_DTMP; 115 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 116 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 117 level = '0'; 118 119 if (argc < 2) 120 usage(); 121 122 obsolete(&argc, &argv); 123 #ifdef KERBEROS 124 #define optstring "0123456789aB:b:cd:f:h:kns:T:uWw" 125 #else 126 #define optstring "0123456789aB:b:cd:f:h:ns:T:uWw" 127 #endif 128 while ((ch = getopt(argc, argv, optstring)) != -1) 129 #undef optstring 130 switch (ch) { 131 /* dump level */ 132 case '0': case '1': case '2': case '3': case '4': 133 case '5': case '6': case '7': case '8': case '9': 134 level = ch; 135 break; 136 137 case 'a': /* `auto-size', Write to EOM. */ 138 unlimited = 1; 139 break; 140 141 case 'B': /* blocks per output file */ 142 blocksperfile = numarg("number of blocks per file", 143 1L, 0L); 144 break; 145 146 case 'b': /* blocks per tape write */ 147 ntrec = numarg("number of blocks per write", 148 1L, 1000L); 149 /* 150 * XXX 151 * physio(9) currently slices all requests to 152 * 64 KB chunks. So now, if somebody entered 153 * e.g. 96 KB block size here, he would effectively 154 * yield one 64 KB and one 32 KB block, which 155 * restore cannot handle. 156 * Thus we currently enforce pyhsio(9)'s limit 157 * here, too. 158 */ 159 if ( ntrec > 64 ) { 160 msg("please choose a blocksize <= 64\n"); 161 exit(X_ABORT); 162 } 163 break; 164 165 case 'c': /* Tape is cart. not 9-track */ 166 cartridge = 1; 167 break; 168 169 case 'd': /* density, in bits per inch */ 170 density = numarg("density", 10L, 327670L) / 10; 171 if (density >= 625 && !bflag) 172 ntrec = HIGHDENSITYTREC; 173 break; 174 175 case 'f': /* output file */ 176 tape = optarg; 177 break; 178 179 case 'h': 180 honorlevel = numarg("honor level", 0L, 10L); 181 break; 182 183 #ifdef KERBEROS 184 case 'k': 185 dokerberos = 1; 186 break; 187 #endif 188 189 case 'n': /* notify operators */ 190 notify = 1; 191 break; 192 193 case 's': /* tape size, feet */ 194 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 195 break; 196 197 case 'T': /* time of last dump */ 198 spcl.c_ddate = unctime(optarg); 199 if (spcl.c_ddate < 0) { 200 (void)fprintf(stderr, "bad time \"%s\"\n", 201 optarg); 202 exit(X_ABORT); 203 } 204 Tflag = 1; 205 lastlevel = '?'; 206 argc--; 207 argv++; 208 break; 209 210 case 'u': /* update /etc/dumpdates */ 211 uflag = 1; 212 break; 213 214 case 'W': /* what to do */ 215 case 'w': 216 lastdump(ch); 217 exit(0); /* do nothing else */ 218 219 default: 220 usage(); 221 } 222 argc -= optind; 223 argv += optind; 224 225 if (argc < 1) { 226 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 227 exit(X_ABORT); 228 } 229 disk = *argv++; 230 argc--; 231 if (argc >= 1) { 232 (void)fprintf(stderr, "Unknown arguments to dump:"); 233 while (argc--) 234 (void)fprintf(stderr, " %s", *argv++); 235 (void)fprintf(stderr, "\n"); 236 exit(X_ABORT); 237 } 238 if (Tflag && uflag) { 239 (void)fprintf(stderr, 240 "You cannot use the T and u flags together.\n"); 241 exit(X_ABORT); 242 } 243 if (strcmp(tape, "-") == 0) { 244 pipeout++; 245 tape = "standard output"; 246 } 247 248 if (blocksperfile) 249 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 250 else if (!unlimited) { 251 /* 252 * Determine how to default tape size and density 253 * 254 * density tape size 255 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 256 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 257 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 258 * (450*4 - slop) 259 * hilit19 hits again: " 260 */ 261 if (density == 0) 262 density = cartridge ? 100 : 160; 263 if (tsize == 0) 264 tsize = cartridge ? 1700L*120L : 2300L*120L; 265 } 266 267 if (strchr(tape, ':')) { 268 host = tape; 269 tape = strchr(host, ':'); 270 *tape++ = '\0'; 271 #ifdef RDUMP 272 if (index(tape, '\n')) { 273 (void)fprintf(stderr, "invalid characters in tape\n"); 274 exit(X_ABORT); 275 } 276 if (rmthost(host) == 0) 277 exit(X_ABORT); 278 #else 279 (void)fprintf(stderr, "remote dump not enabled\n"); 280 exit(X_ABORT); 281 #endif 282 } 283 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 284 285 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 286 signal(SIGHUP, sig); 287 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 288 signal(SIGTRAP, sig); 289 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 290 signal(SIGFPE, sig); 291 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 292 signal(SIGBUS, sig); 293 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 294 signal(SIGSEGV, sig); 295 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 296 signal(SIGTERM, sig); 297 if (signal(SIGINT, interrupt) == SIG_IGN) 298 signal(SIGINT, SIG_IGN); 299 300 set_operators(); /* /etc/group snarfed */ 301 getfstab(); /* /etc/fstab snarfed */ 302 /* 303 * disk can be either the full special file name, 304 * the suffix of the special file name, 305 * the special name missing the leading '/', 306 * the file system name with or without the leading '/'. 307 */ 308 dt = fstabsearch(disk); 309 if (dt != NULL) { 310 disk = rawname(dt->fs_spec); 311 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 312 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 313 } else { 314 (void)strncpy(spcl.c_dev, disk, NAMELEN); 315 (void)strncpy(spcl.c_filesys, "an unlisted file system", 316 NAMELEN); 317 } 318 spcl.c_dev[NAMELEN-1]='\0'; 319 spcl.c_filesys[NAMELEN-1]='\0'; 320 (void)strcpy(spcl.c_label, "none"); 321 (void)gethostname(spcl.c_host, NAMELEN); 322 spcl.c_level = level - '0'; 323 spcl.c_type = TS_TAPE; 324 if (!Tflag) 325 getdumptime(); /* /etc/dumpdates snarfed */ 326 327 msg("Date of this level %c dump: %s", level, 328 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 329 msg("Date of last level %c dump: %s", lastlevel, 330 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 331 msg("Dumping %s ", disk); 332 if (dt != NULL) 333 msgtail("(%s) ", dt->fs_file); 334 if (host) 335 msgtail("to %s on host %s\n", tape, host); 336 else 337 msgtail("to %s\n", tape); 338 339 if ((diskfd = open(disk, O_RDONLY)) < 0) { 340 msg("Cannot open %s\n", disk); 341 exit(X_ABORT); 342 } 343 sync(); 344 sblock = (struct fs *)sblock_buf; 345 bread(SBOFF, (char *) sblock, SBSIZE); 346 if (sblock->fs_magic != FS_MAGIC) 347 quit("bad sblock magic number\n"); 348 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 349 dev_bshift = ffs(dev_bsize) - 1; 350 if (dev_bsize != (1 << dev_bshift)) 351 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 352 tp_bshift = ffs(TP_BSIZE) - 1; 353 if (TP_BSIZE != (1 << tp_bshift)) 354 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 355 #ifdef FS_44INODEFMT 356 if (sblock->fs_inodefmt >= FS_44INODEFMT) 357 spcl.c_flags |= DR_NEWINODEFMT; 358 #endif 359 maxino = sblock->fs_ipg * sblock->fs_ncg; 360 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 361 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 362 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 363 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 364 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 365 366 nonodump = spcl.c_level < honorlevel; 367 368 msg("mapping (Pass I) [regular files]\n"); 369 anydirskipped = mapfiles(maxino, &tapesize); 370 371 msg("mapping (Pass II) [directories]\n"); 372 while (anydirskipped) { 373 anydirskipped = mapdirs(maxino, &tapesize); 374 } 375 376 if (pipeout || unlimited) { 377 tapesize += 10; /* 10 trailer blocks */ 378 msg("estimated %ld tape blocks.\n", tapesize); 379 } else { 380 double fetapes; 381 382 if (blocksperfile) 383 fetapes = (double) tapesize / blocksperfile; 384 else if (cartridge) { 385 /* Estimate number of tapes, assuming streaming stops at 386 the end of each block written, and not in mid-block. 387 Assume no erroneous blocks; this can be compensated 388 for with an artificially low tape size. */ 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) /* streaming-stops per block */ 396 * 15.48 /* 0.1" / streaming-stop " */ 397 ) * (1.0 / tsize ); /* tape / 0.1" " */ 398 } else { 399 /* Estimate number of tapes, for old fashioned 9-track 400 tape */ 401 int tenthsperirg = (density == 625) ? 3 : 7; 402 fetapes = 403 ( (double) tapesize /* blocks */ 404 * TP_BSIZE /* bytes / block */ 405 * (1.0/density) /* 0.1" / byte " */ 406 + 407 (double) tapesize /* blocks */ 408 * (1.0/ntrec) /* IRG's / block */ 409 * tenthsperirg /* 0.1" / IRG " */ 410 ) * (1.0 / tsize ); /* tape / 0.1" " */ 411 } 412 etapes = fetapes; /* truncating assignment */ 413 etapes++; 414 /* count the dumped inodes map on each additional tape */ 415 tapesize += (etapes - 1) * 416 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 417 tapesize += etapes + 10; /* headers + 10 trailer blks */ 418 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 419 tapesize, fetapes); 420 } 421 422 /* 423 * Allocate tape buffer. 424 */ 425 if (!alloctape()) 426 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 427 428 startnewtape(1); 429 (void)time((time_t *)&(tstart_writing)); 430 dumpmap(usedinomap, TS_CLRI, maxino - 1); 431 432 msg("dumping (Pass III) [directories]\n"); 433 dirty = 0; /* XXX just to get gcc to shut up */ 434 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 435 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 436 dirty = *map++; 437 else 438 dirty >>= 1; 439 if ((dirty & 1) == 0) 440 continue; 441 /* 442 * Skip directory inodes deleted and maybe reallocated 443 */ 444 dp = getino(ino); 445 if ((dp->di_mode & IFMT) != IFDIR) 446 continue; 447 (void)dumpino(dp, ino); 448 } 449 450 msg("dumping (Pass IV) [regular files]\n"); 451 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 452 int mode; 453 454 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 455 dirty = *map++; 456 else 457 dirty >>= 1; 458 if ((dirty & 1) == 0) 459 continue; 460 /* 461 * Skip inodes deleted and reallocated as directories. 462 */ 463 dp = getino(ino); 464 mode = dp->di_mode & IFMT; 465 if (mode == IFDIR) 466 continue; 467 (void)dumpino(dp, ino); 468 } 469 470 (void)time((time_t *)&(tend_writing)); 471 spcl.c_type = TS_END; 472 for (i = 0; i < ntrec; i++) 473 writeheader(maxino - 1); 474 if (pipeout) 475 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 476 else 477 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 478 spcl.c_tapea, spcl.c_volume); 479 480 /* report dump performance, avoid division through zero */ 481 if (tend_writing - tstart_writing == 0) 482 msg("finished in less than a second\n"); 483 else 484 msg("finished in %d seconds, throughput %d KBytes/sec\n", 485 tend_writing - tstart_writing, 486 spcl.c_tapea / (tend_writing - tstart_writing)); 487 488 putdumptime(); 489 trewind(); 490 broadcast("DUMP IS DONE!\7\7\n"); 491 msg("DUMP IS DONE\n"); 492 Exit(X_FINOK); 493 /* NOTREACHED */ 494 } 495 496 static void 497 usage() 498 { 499 fprintf(stderr, 500 "usage: dump [-0123456789ac" 501 #ifdef KERBEROS 502 "k" 503 #endif 504 "nu] [-B records] [-b blocksize] [-d density] [-f file]\n" 505 " [-h level] [-s feet] [-T date] filesystem\n" 506 " dump [-W | -w]\n"); 507 exit(1); 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 = 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