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