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