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 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 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/1/95"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #ifdef sunos 47 #include <sys/vnode.h> 48 49 #include <ufs/inode.h> 50 #include <ufs/fs.h> 51 #else 52 #include <ufs/ufs/dinode.h> 53 #include <ufs/ffs/fs.h> 54 #endif 55 56 #include <protocols/dumprestore.h> 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <fstab.h> 63 #include <signal.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include "dump.h" 70 #include "pathnames.h" 71 72 #ifndef SBOFF 73 #define SBOFF (SBLOCK * DEV_BSIZE) 74 #endif 75 76 int notify = 0; /* notify operator flag */ 77 int blockswritten = 0; /* number of blocks written on current tape */ 78 int tapeno = 0; /* current tape number */ 79 int density = 0; /* density in bytes/0.1" " <- this is for hilit19 */ 80 int ntrec = NTREC; /* # tape blocks in each tape record */ 81 int cartridge = 0; /* Assume non-cartridge tape */ 82 long dev_bsize = 1; /* recalculated below */ 83 long blocksperfile; /* output blocks per file */ 84 char *host = NULL; /* remote host (if any) */ 85 86 static long numarg __P((char *, long, long)); 87 static void obsolete __P((int *, char **[])); 88 static void usage __P((void)); 89 90 int 91 main(argc, argv) 92 int argc; 93 char *argv[]; 94 { 95 register ino_t ino; 96 register int dirty; 97 register struct dinode *dp; 98 register struct fstab *dt; 99 register char *map; 100 register int ch; 101 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 102 ino_t maxino; 103 104 spcl.c_date = 0; 105 (void)time((time_t *)&spcl.c_date); 106 107 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 108 if ((tape = getenv("TAPE")) == NULL) 109 tape = _PATH_DEFTAPE; 110 dumpdates = _PATH_DUMPDATES; 111 temp = _PATH_DTMP; 112 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 113 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 114 level = '0'; 115 116 if (argc < 2) 117 usage(); 118 119 obsolete(&argc, &argv); 120 while ((ch = getopt(argc, argv, "0123456789aB:b:cd:f:h:ns:T:uWw")) != -1) 121 switch (ch) { 122 /* dump level */ 123 case '0': case '1': case '2': case '3': case '4': 124 case '5': case '6': case '7': case '8': case '9': 125 level = ch; 126 break; 127 128 case 'a': /* `auto-size', Write to EOM. */ 129 unlimited = 1; 130 break; 131 132 case 'B': /* blocks per output file */ 133 blocksperfile = numarg("number of blocks per file", 134 1L, 0L); 135 break; 136 137 case 'b': /* blocks per tape write */ 138 ntrec = numarg("number of blocks per write", 139 1L, 1000L); 140 /* 141 * XXX 142 * physio(9) currently slices all requests to 143 * 64 KB chunks. So now, if somebody entered 144 * e.g. 96 KB block size here, he would effectively 145 * yield one 64 KB and one 32 KB block, which 146 * restore cannot handle. 147 * Thus we currently enforce pyhsio(9)'s limit 148 * here, too. 149 */ 150 if ( ntrec > 64 ) { 151 msg("please choose a blocksize <= 64\n"); 152 exit(X_ABORT); 153 } 154 break; 155 156 case 'c': /* Tape is cart. not 9-track */ 157 cartridge = 1; 158 break; 159 160 case 'd': /* density, in bits per inch */ 161 density = numarg("density", 10L, 327670L) / 10; 162 if (density >= 625 && !bflag) 163 ntrec = HIGHDENSITYTREC; 164 break; 165 166 case 'f': /* output file */ 167 tape = optarg; 168 break; 169 170 case 'h': 171 honorlevel = numarg("honor level", 0L, 10L); 172 break; 173 174 case 'n': /* notify operators */ 175 notify = 1; 176 break; 177 178 case 's': /* tape size, feet */ 179 tsize = numarg("tape size", 1L, 0L) * 12 * 10; 180 break; 181 182 case 'T': /* time of last dump */ 183 spcl.c_ddate = unctime(optarg); 184 if (spcl.c_ddate < 0) { 185 (void)fprintf(stderr, "bad time \"%s\"\n", 186 optarg); 187 exit(X_ABORT); 188 } 189 Tflag = 1; 190 lastlevel = '?'; 191 argc--; 192 argv++; 193 break; 194 195 case 'u': /* update /etc/dumpdates */ 196 uflag = 1; 197 break; 198 199 case 'W': /* what to do */ 200 case 'w': 201 lastdump(ch); 202 exit(0); /* do nothing else */ 203 204 default: 205 usage(); 206 } 207 argc -= optind; 208 argv += optind; 209 210 if (argc < 1) { 211 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 212 exit(X_ABORT); 213 } 214 disk = *argv++; 215 argc--; 216 if (argc >= 1) { 217 (void)fprintf(stderr, "Unknown arguments to dump:"); 218 while (argc--) 219 (void)fprintf(stderr, " %s", *argv++); 220 (void)fprintf(stderr, "\n"); 221 exit(X_ABORT); 222 } 223 if (Tflag && uflag) { 224 (void)fprintf(stderr, 225 "You cannot use the T and u flags together.\n"); 226 exit(X_ABORT); 227 } 228 if (strcmp(tape, "-") == 0) { 229 pipeout++; 230 tape = "standard output"; 231 } 232 233 if (blocksperfile) 234 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 235 else if (!unlimited) { 236 /* 237 * Determine how to default tape size and density 238 * 239 * density tape size 240 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 241 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 242 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 243 * (450*4 - slop) 244 * hilit19 hits again: " 245 */ 246 if (density == 0) 247 density = cartridge ? 100 : 160; 248 if (tsize == 0) 249 tsize = cartridge ? 1700L*120L : 2300L*120L; 250 } 251 252 if (strchr(tape, ':')) { 253 host = tape; 254 tape = strchr(host, ':'); 255 *tape++ = '\0'; 256 #ifdef RDUMP 257 if (index(tape, '\n')) { 258 (void)fprintf(stderr, "invalid characters in tape\n"); 259 exit(X_ABORT); 260 } 261 if (rmthost(host) == 0) 262 exit(X_ABORT); 263 #else 264 (void)fprintf(stderr, "remote dump not enabled\n"); 265 exit(X_ABORT); 266 #endif 267 } 268 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 269 270 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 271 signal(SIGHUP, sig); 272 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 273 signal(SIGTRAP, sig); 274 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 275 signal(SIGFPE, sig); 276 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 277 signal(SIGBUS, sig); 278 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 279 signal(SIGSEGV, sig); 280 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 281 signal(SIGTERM, sig); 282 if (signal(SIGINT, interrupt) == SIG_IGN) 283 signal(SIGINT, SIG_IGN); 284 285 set_operators(); /* /etc/group snarfed */ 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 msg("Date of this level %c dump: %s", level, 313 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 314 msg("Date of last level %c dump: %s", lastlevel, 315 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 316 msg("Dumping %s ", disk); 317 if (dt != NULL) 318 msgtail("(%s) ", dt->fs_file); 319 if (host) 320 msgtail("to %s on host %s\n", tape, host); 321 else 322 msgtail("to %s\n", tape); 323 324 if ((diskfd = open(disk, O_RDONLY)) < 0) { 325 msg("Cannot open %s\n", disk); 326 exit(X_ABORT); 327 } 328 sync(); 329 sblock = (struct fs *)sblock_buf; 330 bread(SBOFF, (char *) sblock, SBSIZE); 331 if (sblock->fs_magic != FS_MAGIC) 332 quit("bad sblock magic number\n"); 333 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 334 dev_bshift = ffs(dev_bsize) - 1; 335 if (dev_bsize != (1 << dev_bshift)) 336 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 337 tp_bshift = ffs(TP_BSIZE) - 1; 338 if (TP_BSIZE != (1 << tp_bshift)) 339 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 340 #ifdef FS_44INODEFMT 341 if (sblock->fs_inodefmt >= FS_44INODEFMT) 342 spcl.c_flags |= DR_NEWINODEFMT; 343 #endif 344 maxino = sblock->fs_ipg * sblock->fs_ncg; 345 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 346 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 347 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 348 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 349 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 350 351 nonodump = spcl.c_level < honorlevel; 352 353 msg("mapping (Pass I) [regular files]\n"); 354 anydirskipped = mapfiles(maxino, &tapesize); 355 356 msg("mapping (Pass II) [directories]\n"); 357 while (anydirskipped) { 358 anydirskipped = mapdirs(maxino, &tapesize); 359 } 360 361 if (pipeout || unlimited) { 362 tapesize += 10; /* 10 trailer blocks */ 363 msg("estimated %ld tape blocks.\n", tapesize); 364 } else { 365 double fetapes; 366 367 if (blocksperfile) 368 fetapes = (double) tapesize / blocksperfile; 369 else if (cartridge) { 370 /* Estimate number of tapes, assuming streaming stops at 371 the end of each block written, and not in mid-block. 372 Assume no erroneous blocks; this can be compensated 373 for with an artificially low tape size. */ 374 fetapes = 375 ( (double) tapesize /* blocks */ 376 * TP_BSIZE /* bytes/block */ 377 * (1.0/density) /* 0.1" / byte " */ 378 + 379 (double) tapesize /* blocks */ 380 * (1.0/ntrec) /* streaming-stops per block */ 381 * 15.48 /* 0.1" / streaming-stop " */ 382 ) * (1.0 / tsize ); /* tape / 0.1" " */ 383 } else { 384 /* Estimate number of tapes, for old fashioned 9-track 385 tape */ 386 int tenthsperirg = (density == 625) ? 3 : 7; 387 fetapes = 388 ( (double) tapesize /* blocks */ 389 * TP_BSIZE /* bytes / block */ 390 * (1.0/density) /* 0.1" / byte " */ 391 + 392 (double) tapesize /* blocks */ 393 * (1.0/ntrec) /* IRG's / block */ 394 * tenthsperirg /* 0.1" / IRG " */ 395 ) * (1.0 / tsize ); /* tape / 0.1" " */ 396 } 397 etapes = fetapes; /* truncating assignment */ 398 etapes++; 399 /* count the dumped inodes map on each additional tape */ 400 tapesize += (etapes - 1) * 401 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 402 tapesize += etapes + 10; /* headers + 10 trailer blks */ 403 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 404 tapesize, fetapes); 405 } 406 407 /* 408 * Allocate tape buffer. 409 */ 410 if (!alloctape()) 411 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 412 413 startnewtape(1); 414 (void)time((time_t *)&(tstart_writing)); 415 dumpmap(usedinomap, TS_CLRI, maxino - 1); 416 417 msg("dumping (Pass III) [directories]\n"); 418 dirty = 0; /* XXX just to get gcc to shut up */ 419 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 420 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 421 dirty = *map++; 422 else 423 dirty >>= 1; 424 if ((dirty & 1) == 0) 425 continue; 426 /* 427 * Skip directory inodes deleted and maybe reallocated 428 */ 429 dp = getino(ino); 430 if ((dp->di_mode & IFMT) != IFDIR) 431 continue; 432 (void)dumpino(dp, ino); 433 } 434 435 msg("dumping (Pass IV) [regular files]\n"); 436 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 437 int mode; 438 439 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 440 dirty = *map++; 441 else 442 dirty >>= 1; 443 if ((dirty & 1) == 0) 444 continue; 445 /* 446 * Skip inodes deleted and reallocated as directories. 447 */ 448 dp = getino(ino); 449 mode = dp->di_mode & IFMT; 450 if (mode == IFDIR) 451 continue; 452 (void)dumpino(dp, ino); 453 } 454 455 (void)time((time_t *)&(tend_writing)); 456 spcl.c_type = TS_END; 457 for (i = 0; i < ntrec; i++) 458 writeheader(maxino - 1); 459 if (pipeout) 460 msg("DUMP: %ld tape blocks\n", spcl.c_tapea); 461 else 462 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 463 spcl.c_tapea, spcl.c_volume); 464 465 /* report dump performance, avoid division through zero */ 466 if (tend_writing - tstart_writing == 0) 467 msg("finished in less than a second\n"); 468 else 469 msg("finished in %d seconds, throughput %d KBytes/sec\n", 470 tend_writing - tstart_writing, 471 spcl.c_tapea / (tend_writing - tstart_writing)); 472 473 putdumptime(); 474 trewind(); 475 broadcast("DUMP IS DONE!\7\7\n"); 476 msg("DUMP IS DONE\n"); 477 Exit(X_FINOK); 478 /* NOTREACHED */ 479 } 480 481 static void 482 usage() 483 { 484 485 (void)fprintf(stderr, "usage: dump [-0123456789acnu] [-B records] [-b blocksize] [-d density] [-f file]\n [-h level] [-s feet] [-T date] filesystem\n"); 486 (void)fprintf(stderr, " dump [-W | -w]\n"); 487 exit(1); 488 } 489 490 /* 491 * Pick up a numeric argument. It must be nonnegative and in the given 492 * range (except that a vmax of 0 means unlimited). 493 */ 494 static long 495 numarg(meaning, vmin, vmax) 496 char *meaning; 497 long vmin, vmax; 498 { 499 char *p; 500 long val; 501 502 val = strtol(optarg, &p, 10); 503 if (*p) 504 errx(1, "illegal %s -- %s", meaning, optarg); 505 if (val < vmin || (vmax && val > vmax)) 506 errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax); 507 return (val); 508 } 509 510 void 511 sig(signo) 512 int signo; 513 { 514 switch(signo) { 515 case SIGALRM: 516 case SIGBUS: 517 case SIGFPE: 518 case SIGHUP: 519 case SIGTERM: 520 case SIGTRAP: 521 if (pipeout) 522 quit("Signal on pipe: cannot recover\n"); 523 msg("Rewriting attempted as response to unknown signal.\n"); 524 (void)fflush(stderr); 525 (void)fflush(stdout); 526 close_rewind(); 527 exit(X_REWRITE); 528 /* NOTREACHED */ 529 case SIGSEGV: 530 msg("SIGSEGV: ABORTING!\n"); 531 (void)signal(SIGSEGV, SIG_DFL); 532 (void)kill(0, SIGSEGV); 533 /* NOTREACHED */ 534 } 535 } 536 537 char * 538 rawname(cp) 539 char *cp; 540 { 541 static char rawbuf[MAXPATHLEN]; 542 char *dp = strrchr(cp, '/'); 543 544 if (dp == NULL) 545 return (NULL); 546 *dp = '\0'; 547 (void)strncpy(rawbuf, cp, MAXPATHLEN - 1); 548 rawbuf[MAXPATHLEN-1] = '\0'; 549 *dp = '/'; 550 (void)strncat(rawbuf, "/r", MAXPATHLEN - 1 - strlen(rawbuf)); 551 (void)strncat(rawbuf, dp + 1, MAXPATHLEN - 1 - strlen(rawbuf)); 552 return (rawbuf); 553 } 554 555 /* 556 * obsolete -- 557 * Change set of key letters and ordered arguments into something 558 * getopt(3) will like. 559 */ 560 static void 561 obsolete(argcp, argvp) 562 int *argcp; 563 char **argvp[]; 564 { 565 int argc, flags; 566 char *ap, **argv, *flagsp, **nargv, *p; 567 568 /* Setup. */ 569 argv = *argvp; 570 argc = *argcp; 571 572 /* Return if no arguments or first argument has leading dash. */ 573 ap = argv[1]; 574 if (argc == 1 || *ap == '-') 575 return; 576 577 /* Allocate space for new arguments. */ 578 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 579 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 580 err(1, NULL); 581 582 *nargv++ = *argv; 583 argv += 2; 584 585 for (flags = 0; *ap; ++ap) { 586 switch (*ap) { 587 case 'B': 588 case 'b': 589 case 'd': 590 case 'f': 591 case 'h': 592 case 's': 593 case 'T': 594 if (*argv == NULL) { 595 warnx("option requires an argument -- %c", *ap); 596 usage(); 597 } 598 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 599 err(1, NULL); 600 nargv[0][0] = '-'; 601 nargv[0][1] = *ap; 602 (void)strcpy(&nargv[0][2], *argv); 603 ++argv; 604 ++nargv; 605 break; 606 default: 607 if (!flags) { 608 *p++ = '-'; 609 flags = 1; 610 } 611 *p++ = *ap; 612 break; 613 } 614 } 615 616 /* Terminate flags. */ 617 if (flags) { 618 *p = '\0'; 619 *nargv++ = flagsp; 620 } 621 622 /* Copy remaining arguments. */ 623 while (*nargv++ = *argv++); 624 625 /* Update argument count. */ 626 *argcp = nargv - *argvp - 1; 627 } 628