1 /*- 2 * Copyright (c) 1980, 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)main.c 5.24 (Berkeley) 07/16/92"; 16 #endif /* not lint */ 17 18 #ifdef sunos 19 #include <stdio.h> 20 #include <errno.h> 21 #include <ctype.h> 22 #include <sys/param.h> 23 #include <sys/stat.h> 24 #include <ufs/fs.h> 25 #else 26 #include <sys/param.h> 27 #include <sys/time.h> 28 #include <ufs/ffs/fs.h> 29 #endif 30 #include <ufs/ufs/dinode.h> 31 #include <protocols/dumprestore.h> 32 #include <signal.h> 33 #ifdef __STDC__ 34 #include <time.h> 35 #endif 36 #include <fcntl.h> 37 #include <fstab.h> 38 #include <stdio.h> 39 #ifdef __STDC__ 40 #include <unistd.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #endif 44 #include "dump.h" 45 #include "pathnames.h" 46 47 int notify = 0; /* notify operator flag */ 48 int blockswritten = 0; /* number of blocks written on current tape */ 49 int tapeno = 0; /* current tape number */ 50 int density = 0; /* density in bytes/0.1" */ 51 int ntrec = NTREC; /* # tape blocks in each tape record */ 52 int cartridge = 0; /* Assume non-cartridge tape */ 53 long dev_bsize = 1; /* recalculated below */ 54 long blocksperfile; /* output blocks per file */ 55 char *host = NULL; /* remote host (if any) */ 56 #ifdef RDUMP 57 int rmthost(); 58 #endif 59 60 main(argc, argv) 61 int argc; 62 char **argv; 63 { 64 register ino_t ino; 65 register int dirty; 66 register struct dinode *dp; 67 register struct fstab *dt; 68 register char *map; 69 register char *cp; 70 int i, anydirskipped, bflag = 0, Tflag = 0; 71 float fetapes; 72 ino_t maxino; 73 74 spcl.c_date = 0; 75 (void) time((time_t *) &(spcl.c_date)); 76 77 tsize = 0; /* Default later, based on 'c' option for cart tapes */ 78 tape = _PATH_DEFTAPE; 79 dumpdates = _PATH_DUMPDATES; 80 temp = _PATH_DTMP; 81 if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 82 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 83 level = '0'; 84 argv++; 85 argc -= 2; 86 for (cp = *argv++; *cp; cp++) { 87 switch (*cp) { 88 case '-': 89 continue; 90 91 case 'w': 92 lastdump('w'); /* tell us only what has to be done */ 93 (void) exit(0); 94 95 case 'W': /* what to do */ 96 lastdump('W'); /* tell us state of what is done */ 97 (void) exit(0); /* do nothing else */ 98 99 case 'f': /* output file */ 100 if (argc < 1) 101 break; 102 tape = *argv++; 103 argc--; 104 continue; 105 106 case 'd': /* density, in bits per inch */ 107 if (argc < 1) 108 break; 109 density = atoi(*argv) / 10; 110 if (density < 1) { 111 (void) fprintf(stderr, "bad density \"%s\"\n", 112 *argv); 113 Exit(X_ABORT); 114 } 115 argc--; 116 argv++; 117 if (density >= 625 && !bflag) 118 ntrec = HIGHDENSITYTREC; 119 continue; 120 121 case 's': /* tape size, feet */ 122 if (argc < 1) 123 break; 124 tsize = atol(*argv); 125 if (tsize < 1) { 126 (void) fprintf(stderr, "bad size \"%s\"\n", 127 *argv); 128 Exit(X_ABORT); 129 } 130 argc--; 131 argv++; 132 tsize *= 12 * 10; 133 continue; 134 135 case 'T': /* time of last dump */ 136 if (argc < 1) 137 break; 138 spcl.c_ddate = unctime(*argv); 139 if (spcl.c_ddate < 0) { 140 (void) fprintf(stderr, "bad time \"%s\"\n", 141 *argv); 142 Exit(X_ABORT); 143 } 144 Tflag++; 145 lastlevel = '?'; 146 argc--; 147 argv++; 148 continue; 149 150 case 'b': /* blocks per tape write */ 151 if (argc < 1) 152 break; 153 bflag++; 154 ntrec = atoi(*argv); 155 if (ntrec < 1) { 156 (void) fprintf(stderr, "%s \"%s\"\n", 157 "bad number of blocks per write ", *argv); 158 Exit(X_ABORT); 159 } 160 argc--; 161 argv++; 162 continue; 163 164 case 'B': /* blocks per output file */ 165 if (argc < 1) 166 break; 167 blocksperfile = atol(*argv); 168 if (blocksperfile < 1) { 169 (void) fprintf(stderr, "%s \"%s\"\n", 170 "bad number of blocks per file ", *argv); 171 Exit(X_ABORT); 172 } 173 argc--; 174 argv++; 175 continue; 176 177 case 'c': /* Tape is cart. not 9-track */ 178 cartridge++; 179 continue; 180 181 case '0': /* dump level */ 182 case '1': 183 case '2': 184 case '3': 185 case '4': 186 case '5': 187 case '6': 188 case '7': 189 case '8': 190 case '9': 191 level = *cp; 192 continue; 193 194 case 'u': /* update /etc/dumpdates */ 195 uflag++; 196 continue; 197 198 case 'n': /* notify operators */ 199 notify++; 200 continue; 201 202 default: 203 (void) fprintf(stderr, "bad key '%c'\n", *cp); 204 Exit(X_ABORT); 205 } 206 (void) fprintf(stderr, "missing argument to '%c'\n", *cp); 207 Exit(X_ABORT); 208 } 209 if (argc < 1) { 210 (void) fprintf(stderr, "Must specify disk or filesystem\n"); 211 Exit(X_ABORT); 212 } else { 213 disk = *argv++; 214 argc--; 215 } 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 { 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 */ 245 if (density == 0) 246 density = cartridge ? 100 : 160; 247 if (tsize == 0) 248 tsize = cartridge ? 1700L*120L : 2300L*120L; 249 } 250 251 if (index(tape, ':')) { 252 host = tape; 253 tape = index(host, ':'); 254 *tape++ = 0; 255 #ifdef RDUMP 256 if (rmthost(host) == 0) 257 (void) exit(X_ABORT); 258 #else 259 (void) fprintf(stderr, "remote dump not enabled\n"); 260 (void) exit(X_ABORT); 261 #endif 262 } 263 (void) setuid(getuid()); /* rmthost() is the only reason to be setuid */ 264 265 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 266 signal(SIGHUP, sig); 267 if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 268 signal(SIGTRAP, sig); 269 if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 270 signal(SIGFPE, sig); 271 if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 272 signal(SIGBUS, sig); 273 if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 274 signal(SIGSEGV, sig); 275 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 276 signal(SIGTERM, sig); 277 if (signal(SIGINT, interrupt) == SIG_IGN) 278 signal(SIGINT, SIG_IGN); 279 280 set_operators(); /* /etc/group snarfed */ 281 getfstab(); /* /etc/fstab snarfed */ 282 /* 283 * disk can be either the full special file name, 284 * the suffix of the special file name, 285 * the special name missing the leading '/', 286 * the file system name with or without the leading '/'. 287 */ 288 dt = fstabsearch(disk); 289 if (dt != 0) { 290 disk = rawname(dt->fs_spec); 291 (void) strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 292 (void) strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 293 } else { 294 (void) strncpy(spcl.c_dev, disk, NAMELEN); 295 (void) strncpy(spcl.c_filesys, "an unlisted file system", 296 NAMELEN); 297 } 298 (void) strcpy(spcl.c_label, "none"); 299 (void) gethostname(spcl.c_host, NAMELEN); 300 spcl.c_level = level - '0'; 301 spcl.c_type = TS_TAPE; 302 if (!Tflag) 303 getdumptime(); /* /etc/dumpdates snarfed */ 304 305 msg("Date of this level %c dump: %s", level, 306 spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 307 msg("Date of last level %c dump: %s", lastlevel, 308 spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 309 msg("Dumping %s ", disk); 310 if (dt != 0) 311 msgtail("(%s) ", dt->fs_file); 312 if (host) 313 msgtail("to %s on host %s\n", tape, host); 314 else 315 msgtail("to %s\n", tape); 316 317 if ((diskfd = open(disk, O_RDONLY)) < 0) { 318 msg("Cannot open %s\n", disk); 319 Exit(X_ABORT); 320 } 321 sync(); 322 sblock = (struct fs *)sblock_buf; 323 bread(SBOFF, (char *) sblock, SBSIZE); 324 if (sblock->fs_magic != FS_MAGIC) 325 quit("bad sblock magic number\n"); 326 dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 327 dev_bshift = ffs(dev_bsize) - 1; 328 if (dev_bsize != (1 << dev_bshift)) 329 quit("dev_bsize (%d) is not a power of 2", dev_bsize); 330 tp_bshift = ffs(TP_BSIZE) - 1; 331 if (TP_BSIZE != (1 << tp_bshift)) 332 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 333 if (sblock->fs_inodefmt >= FS_44INODEFMT) 334 spcl.c_flags |= DR_NEWINODEFMT; 335 maxino = sblock->fs_ipg * sblock->fs_ncg - 1; 336 mapsize = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 337 TP_BSIZE); 338 usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 339 dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char)); 340 dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char)); 341 tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 342 343 msg("mapping (Pass I) [regular files]\n"); 344 anydirskipped = mapfiles(maxino, &tapesize); 345 346 msg("mapping (Pass II) [directories]\n"); 347 while (anydirskipped) { 348 anydirskipped = mapdirs(maxino, &tapesize); 349 } 350 351 if (pipeout) 352 tapesize += 10; /* 10 trailer blocks */ 353 else { 354 if (blocksperfile) 355 fetapes = (float) tapesize / blocksperfile; 356 else if (cartridge) { 357 /* Estimate number of tapes, assuming streaming stops at 358 the end of each block written, and not in mid-block. 359 Assume no erroneous blocks; this can be compensated 360 for with an artificially low tape size. */ 361 fetapes = 362 ( tapesize /* blocks */ 363 * TP_BSIZE /* bytes/block */ 364 * (1.0/density) /* 0.1" / byte */ 365 + 366 tapesize /* blocks */ 367 * (1.0/ntrec) /* streaming-stops per block */ 368 * 15.48 /* 0.1" / streaming-stop */ 369 ) * (1.0 / tsize ); /* tape / 0.1" */ 370 } else { 371 /* Estimate number of tapes, for old fashioned 9-track 372 tape */ 373 int tenthsperirg = (density == 625) ? 3 : 7; 374 fetapes = 375 ( tapesize /* blocks */ 376 * TP_BSIZE /* bytes / block */ 377 * (1.0/density) /* 0.1" / byte */ 378 + 379 tapesize /* blocks */ 380 * (1.0/ntrec) /* IRG's / block */ 381 * tenthsperirg /* 0.1" / IRG */ 382 ) * (1.0 / tsize ); /* tape / 0.1" */ 383 } 384 etapes = fetapes; /* truncating assignment */ 385 etapes++; 386 /* count the dumped inodes map on each additional tape */ 387 tapesize += (etapes - 1) * 388 (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 389 tapesize += etapes + 10; /* headers + 10 trailer blks */ 390 } 391 if (pipeout) 392 msg("estimated %ld tape blocks.\n", tapesize); 393 else 394 msg("estimated %ld tape blocks on %3.2f tape(s).\n", 395 tapesize, fetapes); 396 397 /* 398 * Allocate tape buffer 399 */ 400 if (!alloctape()) 401 quit("can't allocate tape buffers - try a smaller blocking factor.\n"); 402 403 startnewtape(1); 404 (void) time((time_t *)&(tstart_writing)); 405 dumpmap(usedinomap, TS_CLRI, maxino); 406 407 msg("dumping (Pass III) [directories]\n"); 408 for (map = dumpdirmap, ino = 0; ino < maxino; ) { 409 if ((ino % NBBY) == 0) 410 dirty = *map++; 411 else 412 dirty >>= 1; 413 ino++; 414 if ((dirty & 1) == 0) 415 continue; 416 /* 417 * Skip directory inodes deleted and maybe reallocated 418 */ 419 dp = getino(ino); 420 if ((dp->di_mode & IFMT) != IFDIR) 421 continue; 422 (void) dumpino(dp, ino); 423 } 424 425 msg("dumping (Pass IV) [regular files]\n"); 426 for (map = dumpinomap, ino = 0; ino < maxino; ) { 427 if ((ino % NBBY) == 0) 428 dirty = *map++; 429 else 430 dirty >>= 1; 431 ino++; 432 if ((dirty & 1) == 0) 433 continue; 434 /* 435 * Skip inodes deleted and reallocated as directories. 436 */ 437 dp = getino(ino); 438 if ((dp->di_mode & IFMT) == IFDIR) 439 continue; 440 (void) dumpino(dp, ino); 441 } 442 443 spcl.c_type = TS_END; 444 for (i = 0; i < ntrec; i++) 445 writeheader(maxino); 446 if (pipeout) 447 msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 448 else 449 msg("DUMP: %ld tape blocks on %d volumes(s)\n", 450 spcl.c_tapea, spcl.c_volume); 451 putdumptime(); 452 trewind(); 453 broadcast("DUMP IS DONE!\7\7\n"); 454 msg("DUMP IS DONE\n"); 455 Exit(X_FINOK); 456 /* NOTREACHED */ 457 } 458 459 void 460 sig(signo) 461 int signo; 462 { 463 switch(signo) { 464 case SIGALRM: 465 case SIGBUS: 466 case SIGFPE: 467 case SIGHUP: 468 case SIGTERM: 469 case SIGTRAP: 470 if (pipeout) 471 quit("Signal on pipe: cannot recover\n"); 472 msg("Rewriting attempted as response to unknown signal.\n"); 473 (void) fflush(stderr); 474 (void) fflush(stdout); 475 close_rewind(); 476 (void) exit(X_REWRITE); 477 /* NOTREACHED */ 478 case SIGSEGV: 479 msg("SIGSEGV: ABORTING!\n"); 480 (void) signal(SIGSEGV, SIG_DFL); 481 (void) kill(0, SIGSEGV); 482 /* NOTREACHED */ 483 } 484 } 485 486 char * 487 rawname(cp) 488 char *cp; 489 { 490 static char rawbuf[32]; 491 char *rindex(); 492 char *dp = rindex(cp, '/'); 493 494 if (dp == 0) 495 return (0); 496 *dp = 0; 497 (void) strcpy(rawbuf, cp); 498 *dp = '/'; 499 (void) strcat(rawbuf, "/r"); 500 (void) strcat(rawbuf, dp+1); 501 return (rawbuf); 502 } 503 504 #ifdef sunos 505 char * 506 strerror(errnum) 507 int errnum; 508 { 509 extern int sys_nerr; 510 extern char *sys_errlist[]; 511 512 if (errnum < sys_nerr) { 513 return(sys_errlist[errnum]); 514 } else { 515 return("bogus errno in strerror"); 516 } 517 } 518 #endif 519