147082Smckusick /*- 247082Smckusick * Copyright (c) 1980, 1991 The Regents of the University of California. 347082Smckusick * All rights reserved. 447082Smckusick * 547082Smckusick * %sccs.include.redist.c% 622037Sdist */ 722037Sdist 822037Sdist #ifndef lint 947082Smckusick char copyright[] = 1047082Smckusick "@(#) Copyright (c) 1980, 1991 The Regents of the University of California.\n\ 1147082Smckusick All rights reserved.\n"; 1246586Storek #endif /* not lint */ 1322037Sdist 1447082Smckusick #ifndef lint 15*48621Skarels static char sccsid[] = "@(#)main.c 5.16 (Berkeley) 04/24/91"; 1647082Smckusick #endif /* not lint */ 1747082Smckusick 1846795Sbostic #include <sys/param.h> 1946795Sbostic #include <ufs/dir.h> 2046795Sbostic #include <ufs/dinode.h> 2146795Sbostic #include <ufs/fs.h> 2246795Sbostic #include <protocols/dumprestore.h> 2346795Sbostic #include <signal.h> 2446795Sbostic #ifdef __STDC__ 2546795Sbostic #include <time.h> 2646795Sbostic #endif 2746795Sbostic #include <fcntl.h> 2846795Sbostic #include <fstab.h> 2946795Sbostic #include <stdio.h> 3046795Sbostic #ifdef __STDC__ 3146795Sbostic #include <unistd.h> 3246795Sbostic #include <stdlib.h> 3346795Sbostic #include <string.h> 3446795Sbostic #endif 351423Sroot #include "dump.h" 3637266Sbostic #include "pathnames.h" 371423Sroot 381423Sroot int notify = 0; /* notify operator flag */ 391423Sroot int blockswritten = 0; /* number of blocks written on current tape */ 401423Sroot int tapeno = 0; /* current tape number */ 4110910Ssam int density = 0; /* density in bytes/0.1" */ 4210910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */ 4310910Ssam int cartridge = 0; /* Assume non-cartridge tape */ 4430560Smckusick long dev_bsize = 1; /* recalculated below */ 4546614Smckusick long blocksperfile; /* output blocks per file */ 466882Ssam #ifdef RDUMP 476882Ssam char *host; 4846586Storek int rmthost(); 496882Ssam #endif 501423Sroot 511423Sroot main(argc, argv) 5246794Smckusick int argc; 5346795Sbostic char **argv; 541423Sroot { 5546794Smckusick register ino_t ino; 5647058Smckusick register int dirty; 5746794Smckusick register struct dinode *dp; 5846794Smckusick register struct fstab *dt; 5946794Smckusick register char *map; 6046794Smckusick register char *cp; 6146794Smckusick int i, anydirskipped, bflag = 0; 6246794Smckusick float fetapes; 6346794Smckusick ino_t maxino; 641423Sroot 651423Sroot time(&(spcl.c_date)); 661423Sroot 6710910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */ 6837946Sbostic tape = _PATH_DEFTAPE; 6946794Smckusick dumpdates = _PATH_DUMPDATES; 7037266Sbostic temp = _PATH_DTMP; 7146586Storek if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 7246586Storek quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 7346794Smckusick level = '0'; 7446794Smckusick argv++; 7546794Smckusick argc -= 2; 7646794Smckusick for (cp = *argv++; *cp; cp++) { 7746794Smckusick switch (*cp) { 7846794Smckusick case '-': 7946794Smckusick continue; 801423Sroot 8146794Smckusick case 'w': 8246794Smckusick lastdump('w'); /* tell us only what has to be done */ 8346794Smckusick exit(0); 8446794Smckusick 8546794Smckusick case 'W': /* what to do */ 8646794Smckusick lastdump('W'); /* tell us state of what is done */ 8746794Smckusick exit(0); /* do nothing else */ 8846794Smckusick 8946794Smckusick case 'f': /* output file */ 9046794Smckusick if (argc < 1) 9146794Smckusick break; 9246794Smckusick tape = *argv++; 931423Sroot argc--; 9446794Smckusick continue; 951423Sroot 9646794Smckusick case 'd': /* density, in bits per inch */ 9746794Smckusick if (argc < 1) 9846794Smckusick break; 9946794Smckusick density = atoi(*argv) / 10; 10046794Smckusick if (density < 1) { 10146794Smckusick fprintf(stderr, "bad density \"%s\"\n", *argv); 10246794Smckusick Exit(X_ABORT); 10346794Smckusick } 10446794Smckusick argc--; 1051423Sroot argv++; 10618494Smckusick if (density >= 625 && !bflag) 10718494Smckusick ntrec = HIGHDENSITYTREC; 10846794Smckusick continue; 1091423Sroot 11046794Smckusick case 's': /* tape size, feet */ 11146794Smckusick if (argc < 1) 11246794Smckusick break; 11346794Smckusick tsize = atol(*argv); 11446794Smckusick if (tsize < 1) { 11546794Smckusick fprintf(stderr, "bad size \"%s\"\n", *argv); 11646794Smckusick Exit(X_ABORT); 11746794Smckusick } 11846794Smckusick argc--; 1191423Sroot argv++; 12046794Smckusick tsize *= 12 * 10; 12146794Smckusick continue; 1221423Sroot 12346794Smckusick case 'b': /* blocks per tape write */ 12446794Smckusick if (argc < 1) 12546794Smckusick break; 12618494Smckusick bflag++; 12747058Smckusick ntrec = atoi(*argv); 12846794Smckusick if (ntrec < 1) { 12946794Smckusick fprintf(stderr, "%s \"%s\"\n", 13046794Smckusick "bad number of blocks per write ", *argv); 13146614Smckusick Exit(X_ABORT); 13246614Smckusick } 13346794Smckusick argc--; 13446794Smckusick argv++; 13546794Smckusick continue; 13610910Ssam 13746794Smckusick case 'B': /* blocks per output file */ 13846794Smckusick if (argc < 1) 13946794Smckusick break; 14046794Smckusick blocksperfile = atol(*argv); 14146794Smckusick if (blocksperfile < 1) { 14246794Smckusick fprintf(stderr, "%s \"%s\"\n", 14346794Smckusick "bad number of blocks per file ", *argv); 14446794Smckusick Exit(X_ABORT); 14546794Smckusick } 14646794Smckusick argc--; 14746614Smckusick argv++; 14846794Smckusick continue; 14946614Smckusick 15046794Smckusick case 'c': /* Tape is cart. not 9-track */ 15146794Smckusick cartridge++; 15246794Smckusick continue; 15310910Ssam 15446794Smckusick case '0': /* dump level */ 15546794Smckusick case '1': 15646794Smckusick case '2': 15746794Smckusick case '3': 15846794Smckusick case '4': 15946794Smckusick case '5': 16046794Smckusick case '6': 16146794Smckusick case '7': 16246794Smckusick case '8': 16346794Smckusick case '9': 16446794Smckusick level = *cp; 16546794Smckusick continue; 1661423Sroot 16746794Smckusick case 'u': /* update /etc/dumpdates */ 16846794Smckusick uflag++; 16946794Smckusick continue; 1701423Sroot 17146794Smckusick case 'n': /* notify operators */ 17246794Smckusick notify++; 17346794Smckusick continue; 1741423Sroot 17546794Smckusick default: 17646794Smckusick fprintf(stderr, "bad key '%c'\n", *cp); 17746794Smckusick Exit(X_ABORT); 17846794Smckusick } 17946794Smckusick fprintf(stderr, "missing argument to '%c'\n", *cp); 1801423Sroot Exit(X_ABORT); 1811423Sroot } 18246794Smckusick if (argc < 1) { 18346794Smckusick fprintf(stderr, "Must specify disk or filesystem\n"); 18446794Smckusick Exit(X_ABORT); 18546794Smckusick } else { 18646794Smckusick disk = *argv++; 1871423Sroot argc--; 1881423Sroot } 18946794Smckusick if (argc >= 1) { 19046794Smckusick fprintf(stderr, "Unknown arguments to dump:"); 19146794Smckusick while (argc--) 19246794Smckusick fprintf(stderr, " %s", *argv++); 19346794Smckusick fprintf(stderr, "\n"); 19446794Smckusick Exit(X_ABORT); 19546794Smckusick } 19612331Smckusick if (strcmp(tape, "-") == 0) { 19712331Smckusick pipeout++; 19812331Smckusick tape = "standard output"; 19912331Smckusick } 20010910Ssam 20146614Smckusick if (blocksperfile) 20246614Smckusick blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 20346614Smckusick else { 20446614Smckusick /* 20546614Smckusick * Determine how to default tape size and density 20646614Smckusick * 20746614Smckusick * density tape size 20846614Smckusick * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 20946614Smckusick * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 21046614Smckusick * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 21146614Smckusick * (450*4 - slop) 21246614Smckusick */ 21346614Smckusick if (density == 0) 21446614Smckusick density = cartridge ? 100 : 160; 21546614Smckusick if (tsize == 0) 21646614Smckusick tsize = cartridge ? 1700L*120L : 2300L*120L; 21746614Smckusick } 21810910Ssam 2196886Ssam #ifdef RDUMP 2206886Ssam { char *index(); 2216886Ssam host = tape; 2226886Ssam tape = index(host, ':'); 2236886Ssam if (tape == 0) { 22428642Smckusick msg("need keyletter ``f'' and device ``host:tape''\n"); 2256886Ssam exit(1); 2266886Ssam } 2276886Ssam *tape++ = 0; 2286886Ssam if (rmthost(host) == 0) 2296886Ssam exit(X_ABORT); 2306886Ssam } 23128860Smckusick setuid(getuid()); /* rmthost() is the only reason to be setuid */ 2326886Ssam #endif 23346614Smckusick if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 23446614Smckusick signal(SIGHUP, sighup); 23546614Smckusick if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 23646614Smckusick signal(SIGTRAP, sigtrap); 23746614Smckusick if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 23846614Smckusick signal(SIGFPE, sigfpe); 23946614Smckusick if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 24046614Smckusick signal(SIGBUS, sigbus); 24146614Smckusick if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 24246614Smckusick signal(SIGSEGV, sigsegv); 24346614Smckusick if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 24446614Smckusick signal(SIGTERM, sigterm); 2451423Sroot 2461423Sroot 2471423Sroot if (signal(SIGINT, interrupt) == SIG_IGN) 2481423Sroot signal(SIGINT, SIG_IGN); 2491423Sroot 2501423Sroot set_operators(); /* /etc/group snarfed */ 2511423Sroot getfstab(); /* /etc/fstab snarfed */ 2521423Sroot /* 2531423Sroot * disk can be either the full special file name, 2541423Sroot * the suffix of the special file name, 2551423Sroot * the special name missing the leading '/', 2561423Sroot * the file system name with or without the leading '/'. 2571423Sroot */ 2581423Sroot dt = fstabsearch(disk); 25929900Smckusick if (dt != 0) { 2601423Sroot disk = rawname(dt->fs_spec); 26129900Smckusick strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 26229900Smckusick strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 26329900Smckusick } else { 26429900Smckusick strncpy(spcl.c_dev, disk, NAMELEN); 26529900Smckusick strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN); 26629900Smckusick } 26729900Smckusick strcpy(spcl.c_label, "none"); 26829900Smckusick gethostname(spcl.c_host, NAMELEN); 26946794Smckusick spcl.c_level = level - '0'; 27029900Smckusick spcl.c_type = TS_TAPE; 27146794Smckusick getdumptime(); /* /etc/dumpdates snarfed */ 2721423Sroot 27346794Smckusick msg("Date of this level %c dump: %s", level, 27446794Smckusick spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 27546794Smckusick msg("Date of last level %c dump: %s", lastlevel, 27646794Smckusick spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 2771423Sroot msg("Dumping %s ", disk); 2781423Sroot if (dt != 0) 2791423Sroot msgtail("(%s) ", dt->fs_file); 2808506Smckusick #ifdef RDUMP 2818506Smckusick msgtail("to %s on host %s\n", tape, host); 2828506Smckusick #else 2838371Smckusick msgtail("to %s\n", tape); 2846882Ssam #endif 2851423Sroot 28646794Smckusick if ((diskfd = open(disk, O_RDONLY)) < 0) { 2871423Sroot msg("Cannot open %s\n", disk); 2881423Sroot Exit(X_ABORT); 2891423Sroot } 29046794Smckusick sync(); 2915328Smckusic sblock = (struct fs *)buf; 29230560Smckusick bread(SBOFF, sblock, SBSIZE); 29346586Storek if (sblock->fs_magic != FS_MAGIC) 29446586Storek quit("bad sblock magic number\n"); 29530560Smckusick dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 29646586Storek dev_bshift = ffs(dev_bsize) - 1; 29746586Storek if (dev_bsize != (1 << dev_bshift)) 29846586Storek quit("dev_bsize (%d) is not a power of 2", dev_bsize); 29946586Storek tp_bshift = ffs(TP_BSIZE) - 1; 30046586Storek if (TP_BSIZE != (1 << tp_bshift)) 30146586Storek quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 30246794Smckusick maxino = sblock->fs_ipg * sblock->fs_ncg - 1; 30346794Smckusick mapsize = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 3045328Smckusic TP_BSIZE); 30546794Smckusick usedinomap = (char *)calloc(mapsize, sizeof(char)); 30646794Smckusick dumpdirmap = (char *)calloc(mapsize, sizeof(char)); 30746794Smckusick dumpinomap = (char *)calloc(mapsize, sizeof(char)); 30846794Smckusick tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 3091423Sroot 3101423Sroot msg("mapping (Pass I) [regular files]\n"); 31146794Smckusick anydirskipped = mapfiles(maxino, &tapesize); 3121423Sroot 31346794Smckusick msg("mapping (Pass II) [directories]\n"); 31446794Smckusick while (anydirskipped) { 31546794Smckusick anydirskipped = mapdirs(maxino, &tapesize); 31646794Smckusick } 3171423Sroot 31846614Smckusick if (pipeout) 31946794Smckusick tapesize += 10; /* 10 trailer blocks */ 32046614Smckusick else { 32146614Smckusick if (blocksperfile) 322*48621Skarels fetapes = (float) tapesize / blocksperfile; 32346614Smckusick else if (cartridge) { 32446614Smckusick /* Estimate number of tapes, assuming streaming stops at 32546614Smckusick the end of each block written, and not in mid-block. 32646614Smckusick Assume no erroneous blocks; this can be compensated 32746614Smckusick for with an artificially low tape size. */ 32846614Smckusick fetapes = 32946794Smckusick ( tapesize /* blocks */ 33046614Smckusick * TP_BSIZE /* bytes/block */ 33146614Smckusick * (1.0/density) /* 0.1" / byte */ 33246614Smckusick + 33346794Smckusick tapesize /* blocks */ 33446614Smckusick * (1.0/ntrec) /* streaming-stops per block */ 33546614Smckusick * 15.48 /* 0.1" / streaming-stop */ 33646614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 33746614Smckusick } else { 33846614Smckusick /* Estimate number of tapes, for old fashioned 9-track 33946614Smckusick tape */ 34046614Smckusick int tenthsperirg = (density == 625) ? 3 : 7; 34146614Smckusick fetapes = 34246794Smckusick ( tapesize /* blocks */ 34346614Smckusick * TP_BSIZE /* bytes / block */ 34446614Smckusick * (1.0/density) /* 0.1" / byte */ 34546614Smckusick + 34646794Smckusick tapesize /* blocks */ 34746614Smckusick * (1.0/ntrec) /* IRG's / block */ 34846614Smckusick * tenthsperirg /* 0.1" / IRG */ 34946614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 35046614Smckusick } 35146614Smckusick etapes = fetapes; /* truncating assignment */ 35246614Smckusick etapes++; 35346794Smckusick /* count the dumped inodes map on each additional tape */ 35446794Smckusick tapesize += (etapes - 1) * 35546794Smckusick (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 35646794Smckusick tapesize += etapes + 10; /* headers + 10 trailer blks */ 35710910Ssam } 35846614Smckusick if (pipeout) 35946794Smckusick msg("estimated %ld tape blocks.\n", tapesize); 36046614Smckusick else 36146614Smckusick msg("estimated %ld tape blocks on %3.2f tape(s).\n", 36246794Smckusick tapesize, fetapes); 3631423Sroot 36410910Ssam alloctape(); /* Allocate tape buffer */ 36510910Ssam 36646794Smckusick startnewtape(); 3671423Sroot time(&(tstart_writing)); 36846794Smckusick dumpmap(usedinomap, TS_CLRI, maxino); 3691423Sroot 3701423Sroot msg("dumping (Pass III) [directories]\n"); 37146794Smckusick for (map = dumpdirmap, ino = 0; ino < maxino; ) { 37246794Smckusick if ((ino % NBBY) == 0) 37347058Smckusick dirty = *map++; 37446794Smckusick else 37547058Smckusick dirty >>= 1; 37646794Smckusick ino++; 37747058Smckusick if ((dirty & 1) == 0) 37846794Smckusick continue; 37946794Smckusick /* 38046794Smckusick * Skip directory inodes deleted and maybe reallocated 38146794Smckusick */ 38246794Smckusick dp = getino(ino); 38346794Smckusick if ((dp->di_mode & IFMT) != IFDIR) 38446794Smckusick continue; 38546794Smckusick dumpino(dp, ino); 38646794Smckusick } 3871423Sroot 3881423Sroot msg("dumping (Pass IV) [regular files]\n"); 38946794Smckusick for (map = dumpinomap, ino = 0; ino < maxino; ) { 39046794Smckusick if ((ino % NBBY) == 0) 39147058Smckusick dirty = *map++; 39246794Smckusick else 39347058Smckusick dirty >>= 1; 39446794Smckusick ino++; 39547058Smckusick if ((dirty & 1) == 0) 39646794Smckusick continue; 39746794Smckusick /* 39846794Smckusick * Skip inodes deleted and reallocated as directories. 39946794Smckusick */ 40046794Smckusick dp = getino(ino); 40146794Smckusick if ((dp->di_mode & IFMT) == IFDIR) 40246794Smckusick continue; 40346794Smckusick dumpino(dp, ino); 40446794Smckusick } 4051423Sroot 4061423Sroot spcl.c_type = TS_END; 4076882Ssam #ifndef RDUMP 40846794Smckusick for (i = 0; i < ntrec; i++) 40946794Smckusick writeheader(maxino); 4106882Ssam #endif 41146614Smckusick if (pipeout) 41246614Smckusick msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 41346614Smckusick else 41446614Smckusick msg("DUMP: %ld tape blocks on %d volumes(s)\n", 41546614Smckusick spcl.c_tapea, spcl.c_volume); 4161423Sroot msg("DUMP IS DONE\n"); 4171423Sroot 41846794Smckusick putdumptime(); 4196882Ssam #ifndef RDUMP 42012331Smckusick if (!pipeout) { 42146794Smckusick close(tapefd); 42246239Storek trewind(); 42312331Smckusick } 4246882Ssam #else 42546794Smckusick for (i = 0; i < ntrec; i++) 42646794Smckusick writeheader(curino); 42746239Storek trewind(); 4286882Ssam #endif 4291423Sroot broadcast("DUMP IS DONE!\7\7\n"); 4301423Sroot Exit(X_FINOK); 43146586Storek /* NOTREACHED */ 4321423Sroot } 4331423Sroot 43446586Storek void 4351423Sroot sigAbort() 4361423Sroot { 43746586Storek if (pipeout) 43846586Storek quit("Unknown signal, cannot recover\n"); 4391423Sroot msg("Rewriting attempted as response to unknown signal.\n"); 4401423Sroot fflush(stderr); 4411423Sroot fflush(stdout); 4421423Sroot close_rewind(); 4431423Sroot exit(X_REWRITE); 4441423Sroot } 4451423Sroot 44646586Storek void sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();} 44746586Storek void sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();} 44846586Storek void sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();} 44946586Storek void sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();} 45046586Storek void sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();} 45146586Storek void sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();} 45246586Storek void sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();} 45346586Storek 45446586Storek char * 45546586Storek rawname(cp) 4561423Sroot char *cp; 4571423Sroot { 4581423Sroot static char rawbuf[32]; 4594608Smckusic char *rindex(); 4601423Sroot char *dp = rindex(cp, '/'); 4611423Sroot 4621423Sroot if (dp == 0) 4631423Sroot return (0); 4641423Sroot *dp = 0; 4651423Sroot strcpy(rawbuf, cp); 4661423Sroot *dp = '/'; 4671423Sroot strcat(rawbuf, "/r"); 4681423Sroot strcat(rawbuf, dp+1); 4691423Sroot return (rawbuf); 4701423Sroot } 471