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[] = "from: @(#)main.c 8.4 (Berkeley) 4/15/94";*/ 42 static char *rcsid = "$Id: main.c,v 1.3 1994/06/08 18:57:36 mycroft Exp $"; 43 #endif /* not lint */ 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 #ifdef sunos 48 #include <sys/vnode.h> 49 50 #include <ufs/inode.h> 51 #include <ufs/fs.h> 52 #else 53 #include <ufs/ffs/fs.h> 54 #include <ufs/ufs/dinode.h> 55 #endif 56 57 #include <protocols/dumprestore.h> 58 59 #include <ctype.h> 60 #include <errno.h> 61 #include <fcntl.h> 62 #include <fstab.h> 63 #include <signal.h> 64 #include <stdio.h> 65 #ifdef __STDC__ 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 #endif 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" */ 82 int ntrec = NTREC; /* # tape blocks in each tape record */ 83 int cartridge = 0; /* Assume non-cartridge tape */ 84 long dev_bsize = 1; /* recalculated below */ 85 long blocksperfile; /* output blocks per file */ 86 char *host = NULL; /* remote host (if any) */ 87 88 static long numarg __P((int, char *, long, long, int *, char ***)); 89 static __dead void missingarg __P((int, char *)); 90 91 int 92 main(argc, argv) 93 int argc; 94 char **argv; 95 { 96 register ino_t ino; 97 register int dirty; 98 register struct dinode *dp; 99 register struct fstab *dt; 100 register char *map; 101 register char *cp; 102 int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1; 103 ino_t maxino; 104 105 spcl.c_date = 0; 106 (void)time((time_t *)&spcl.c_date); 107 108 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 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 if (argc == 1) { 116 (void) fprintf(stderr, "Must specify a key.\n"); 117 Exit(X_ABORT); 118 } 119 argv++; 120 argc -= 2; 121 for (cp = *argv++; cp != NULL && *cp != '\0'; cp++) { 122 switch (*cp) { 123 case '-': 124 break; 125 126 case 'w': 127 lastdump('w'); /* tell us only what has to be done */ 128 exit(0); 129 130 case 'W': /* what to do */ 131 lastdump('W'); /* tell us state of what is done */ 132 exit(0); /* do nothing else */ 133 134 case 'f': /* output file */ 135 if (argc < 1) 136 missingarg('f', "output file"); 137 tape = *argv++; 138 argc--; 139 break; 140 141 case 'd': /* density, in bits per inch */ 142 density = numarg('d', "density", 143 10L, 327670L, &argc, &argv) / 10; 144 if (density >= 625 && !bflag) 145 ntrec = HIGHDENSITYTREC; 146 break; 147 148 case 's': /* tape size, feet */ 149 tsize = numarg('s', "size", 150 1L, 0L, &argc, &argv) * 12 * 10; 151 break; 152 153 case 'T': /* time of last dump */ 154 if (argc < 1) 155 missingarg('T', "time of last dump"); 156 spcl.c_ddate = unctime(*argv); 157 if (spcl.c_ddate < 0) { 158 (void)fprintf(stderr, "bad time \"%s\"\n", 159 *argv); 160 exit(X_ABORT); 161 } 162 Tflag = 1; 163 lastlevel = '?'; 164 argc--; 165 argv++; 166 break; 167 168 case 'b': /* blocks per tape write */ 169 ntrec = numarg('b', "number of blocks per write", 170 1L, 1000L, &argc, &argv); 171 break; 172 173 case 'B': /* blocks per output file */ 174 blocksperfile = numarg('B', "number of blocks per file", 175 1L, 0L, &argc, &argv); 176 break; 177 178 case 'c': /* Tape is cart. not 9-track */ 179 cartridge = 1; 180 break; 181 182 /* dump level */ 183 case '0': case '1': case '2': case '3': case '4': 184 case '5': case '6': case '7': case '8': case '9': 185 level = *cp; 186 break; 187 188 case 'u': /* update /etc/dumpdates */ 189 uflag = 1; 190 break; 191 192 case 'n': /* notify operators */ 193 notify = 1; 194 break; 195 196 case 'h': 197 honorlevel = numarg('h', "honor level", 198 0L, 10L, &argc, &argv); 199 break; 200 201 default: 202 (void)fprintf(stderr, "bad key '%c'\n", *cp); 203 exit(X_ABORT); 204 } 205 } 206 if (argc < 1) { 207 (void)fprintf(stderr, "Must specify disk or filesystem\n"); 208 exit(X_ABORT); 209 } 210 disk = *argv++; 211 argc--; 212 if (argc >= 1) { 213 (void)fprintf(stderr, "Unknown arguments to dump:"); 214 while (argc--) 215 (void)fprintf(stderr, " %s", *argv++); 216 (void)fprintf(stderr, "\n"); 217 exit(X_ABORT); 218 } 219 if (Tflag && uflag) { 220 (void)fprintf(stderr, 221 "You cannot use the T and u flags together.\n"); 222 exit(X_ABORT); 223 } 224 if (strcmp(tape, "-") == 0) { 225 pipeout++; 226 tape = "standard output"; 227 } 228 229 if (blocksperfile) 230 blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 231 else { 232 /* 233 * Determine how to default tape size and density 234 * 235 * density tape size 236 * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 237 * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 238 * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 239 * (450*4 - slop) 240 */ 241 if (density == 0) 242 density = cartridge ? 100 : 160; 243 if (tsize == 0) 244 tsize = cartridge ? 1700L*120L : 2300L*120L; 245 } 246 247 if (index(tape, ':')) { 248 host = tape; 249 tape = index(host, ':'); 250 *tape++ = '\0'; 251 #ifdef RDUMP 252 if (rmthost(host) == 0) 253 exit(X_ABORT); 254 #else 255 (void)fprintf(stderr, "remote dump not enabled\n"); 256 exit(X_ABORT); 257 #endif 258 } 259 (void)setuid(getuid()); /* rmthost() is the only reason to be setuid */ 260 261 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 262 signal(SIGHUP, sig); 263 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 264 signal(SIGTRAP, sig); 265 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 266 signal(SIGFPE, sig); 267 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 268 signal(SIGBUS, sig); 269 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 270 signal(SIGSEGV, sig); 271 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 272 signal(SIGTERM, sig); 273 if (signal(SIGINT, interrupt) == SIG_IGN) 274 signal(SIGINT, SIG_IGN); 275 276 set_operators(); /* /etc/group snarfed */ 277 getfstab(); /* /etc/fstab snarfed */ 278 /* 279 * disk can be either the full special file name, 280 * the suffix of the special file name, 281 * the special name missing the leading '/', 282 * the file system name with or without the leading '/'. 283 */ 284 dt = fstabsearch(disk); 285 if (dt != NULL) { 286 disk = rawname(dt->fs_spec); 287 (void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 288 (void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 289 } else { 290 (void)strncpy(spcl.c_dev, disk, NAMELEN); 291 (void)strncpy(spcl.c_filesys, "an unlisted file system", 292 NAMELEN); 293 } 294 (void)strcpy(spcl.c_label, "none"); 295 (void)gethostname(spcl.c_host, NAMELEN); 296 spcl.c_level = level - '0'; 297 spcl.c_type = TS_TAPE; 298 if (!Tflag) 299 getdumptime(); /* /etc/dumpdates snarfed */ 300 301 msg("Date of this level %c dump: %s", level, 302 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 303 msg("Date of last level %c dump: %s", lastlevel, 304 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 305 msg("Dumping %s ", disk); 306 if (dt != NULL) 307 msgtail("(%s) ", dt->fs_file); 308 if (host) 309 msgtail("to %s on host %s\n", tape, host); 310 else 311 msgtail("to %s\n", tape); 312 313 if ((diskfd = open(disk, O_RDONLY)) < 0) { 314 msg("Cannot open %s\n", disk); 315 exit(X_ABORT); 316 } 317 sync(); 318 sblock = (struct fs *)sblock_buf; 319 bread(SBOFF, (char *) sblock, SBSIZE); 320 if (sblock->fs_magic != FS_MAGIC) 321 quit("bad sblock magic number\n"); 322 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 323 dev_bshift = ffs(dev_bsize) - 1; 324 if (dev_bsize != (1 << dev_bshift)) 325 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 326 tp_bshift = ffs(TP_BSIZE) - 1; 327 if (TP_BSIZE != (1 << tp_bshift)) 328 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 329 #ifdef FS_44INODEFMT 330 if (sblock->fs_inodefmt >= FS_44INODEFMT) 331 spcl.c_flags |= DR_NEWINODEFMT; 332 #endif 333 maxino = sblock->fs_ipg * sblock->fs_ncg; 334 mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE); 335 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 336 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 337 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 338 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 339 340 nonodump = spcl.c_level < honorlevel; 341 342 msg("mapping (Pass I) [regular files]\n"); 343 anydirskipped = mapfiles(maxino, &tapesize); 344 345 msg("mapping (Pass II) [directories]\n"); 346 while (anydirskipped) { 347 anydirskipped = mapdirs(maxino, &tapesize); 348 } 349 350 if (pipeout) { 351 tapesize += 10; /* 10 trailer blocks */ 352 msg("estimated %ld tape blocks.\n", tapesize); 353 } else { 354 double fetapes; 355 356 if (blocksperfile) 357 fetapes = (double) tapesize / blocksperfile; 358 else if (cartridge) { 359 /* Estimate number of tapes, assuming streaming stops at 360 the end of each block written, and not in mid-block. 361 Assume no erroneous blocks; this can be compensated 362 for with an artificially low tape size. */ 363 fetapes = 364 ( tapesize /* blocks */ 365 * TP_BSIZE /* bytes/block */ 366 * (1.0/density) /* 0.1" / byte */ 367 + 368 tapesize /* blocks */ 369 * (1.0/ntrec) /* streaming-stops per block */ 370 * 15.48 /* 0.1" / streaming-stop */ 371 ) * (1.0 / tsize ); /* tape / 0.1" */ 372 } else { 373 /* Estimate number of tapes, for old fashioned 9-track 374 tape */ 375 int tenthsperirg = (density == 625) ? 3 : 7; 376 fetapes = 377 ( tapesize /* blocks */ 378 * TP_BSIZE /* bytes / block */ 379 * (1.0/density) /* 0.1" / byte */ 380 + 381 tapesize /* blocks */ 382 * (1.0/ntrec) /* IRG's / block */ 383 * tenthsperirg /* 0.1" / IRG */ 384 ) * (1.0 / tsize ); /* tape / 0.1" */ 385 } 386 etapes = fetapes; /* truncating assignment */ 387 etapes++; 388 /* count the dumped inodes map on each additional tape */ 389 tapesize += (etapes - 1) * 390 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 391 tapesize += etapes + 10; /* headers + 10 trailer blks */ 392 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 393 tapesize, fetapes); 394 } 395 396 /* 397 * Allocate tape buffer. 398 */ 399 if (!alloctape()) 400 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 401 402 startnewtape(1); 403 (void)time((time_t *)&(tstart_writing)); 404 dumpmap(usedinomap, TS_CLRI, maxino - 1); 405 406 msg("dumping (Pass III) [directories]\n"); 407 dirty = 0; /* XXX just to get gcc to shut up */ 408 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 409 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 410 dirty = *map++; 411 else 412 dirty >>= 1; 413 if ((dirty & 1) == 0) 414 continue; 415 /* 416 * Skip directory inodes deleted and maybe reallocated 417 */ 418 dp = getino(ino); 419 if ((dp->di_mode & IFMT) != IFDIR) 420 continue; 421 (void)dumpino(dp, ino); 422 } 423 424 msg("dumping (Pass IV) [regular files]\n"); 425 for (map = dumpinomap, ino = 1; ino < maxino; ino++) { 426 int mode; 427 428 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 429 dirty = *map++; 430 else 431 dirty >>= 1; 432 if ((dirty & 1) == 0) 433 continue; 434 /* 435 * Skip inodes deleted and reallocated as directories. 436 */ 437 dp = getino(ino); 438 mode = dp->di_mode & IFMT; 439 if (mode == IFDIR) 440 continue; 441 (void)dumpino(dp, ino); 442 } 443 444 spcl.c_type = TS_END; 445 for (i = 0; i < ntrec; i++) 446 writeheader(maxino - 1); 447 if (pipeout) 448 msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 449 else 450 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 451 spcl.c_tapea, spcl.c_volume); 452 putdumptime(); 453 trewind(); 454 broadcast("DUMP IS DONE!\7\7\n"); 455 msg("DUMP IS DONE\n"); 456 Exit(X_FINOK); 457 /* NOTREACHED */ 458 } 459 460 /* 461 * Pick up a numeric argument. It must be nonnegative and in the given 462 * range (except that a vmax of 0 means unlimited). 463 */ 464 static long 465 numarg(letter, meaning, vmin, vmax, pargc, pargv) 466 int letter; 467 char *meaning; 468 long vmin, vmax; 469 int *pargc; 470 char ***pargv; 471 { 472 register char *p; 473 long val; 474 char *str; 475 476 if (--*pargc < 0) 477 missingarg(letter, meaning); 478 str = *(*pargv)++; 479 for (p = str; *p; p++) 480 if (!isdigit(*p)) 481 goto bad; 482 val = atol(str); 483 if (val < vmin || (vmax && val > vmax)) 484 goto bad; 485 return (val); 486 487 bad: 488 (void)fprintf(stderr, "bad '%c' (%s) value \"%s\"\n", 489 letter, meaning, str); 490 exit(X_ABORT); 491 } 492 493 static __dead void 494 missingarg(letter, meaning) 495 int letter; 496 char *meaning; 497 { 498 499 (void)fprintf(stderr, "The '%c' flag (%s) requires an argument\n", 500 letter, meaning); 501 exit(X_ABORT); 502 } 503 504 void 505 sig(signo) 506 int signo; 507 { 508 switch(signo) { 509 case SIGALRM: 510 case SIGBUS: 511 case SIGFPE: 512 case SIGHUP: 513 case SIGTERM: 514 case SIGTRAP: 515 if (pipeout) 516 quit("Signal on pipe: cannot recover\n"); 517 msg("Rewriting attempted as response to unknown signal.\n"); 518 (void)fflush(stderr); 519 (void)fflush(stdout); 520 close_rewind(); 521 exit(X_REWRITE); 522 /* NOTREACHED */ 523 case SIGSEGV: 524 msg("SIGSEGV: ABORTING!\n"); 525 (void)signal(SIGSEGV, SIG_DFL); 526 (void)kill(0, SIGSEGV); 527 /* NOTREACHED */ 528 } 529 } 530 531 char * 532 rawname(cp) 533 char *cp; 534 { 535 static char rawbuf[MAXPATHLEN]; 536 char *dp = rindex(cp, '/'); 537 538 if (dp == NULL) 539 return (NULL); 540 *dp = '\0'; 541 (void)strcpy(rawbuf, cp); 542 *dp = '/'; 543 (void)strcat(rawbuf, "/r"); 544 (void)strcat(rawbuf, dp + 1); 545 return (rawbuf); 546 } 547 548 #ifdef sunos 549 const char * 550 strerror(errnum) 551 int errnum; 552 { 553 extern int sys_nerr; 554 extern const char *const sys_errlist[]; 555 556 if (errnum < sys_nerr) 557 return (sys_errlist[errnum]); 558 else 559 return ("bogus errno in strerror"); 560 } 561 #endif 562