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