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*51605Sbostic static char sccsid[] = "@(#)main.c 5.20 (Berkeley) 11/10/91"; 1647082Smckusick #endif /* not lint */ 1747082Smckusick 1850496Smckusick #ifdef sunos 1950496Smckusick #include <stdio.h> 2050496Smckusick #include <errno.h> 2150496Smckusick #include <ctype.h> 2246795Sbostic #include <sys/param.h> 2350496Smckusick #include <sys/stat.h> 2450496Smckusick #include <sys/time.h> 2550496Smckusick #include <sys/dir.h> 2650496Smckusick #include <sys/vnode.h> 2750496Smckusick #include <ufs/inode.h> 2850496Smckusick #else 2950496Smckusick #include <sys/param.h> 30*51605Sbostic #include <ufs/ufs/dinode.h> 3150496Smckusick #endif 32*51605Sbostic #include <ufs/ffs/fs.h> 3346795Sbostic #include <protocols/dumprestore.h> 3446795Sbostic #include <signal.h> 3546795Sbostic #ifdef __STDC__ 3646795Sbostic #include <time.h> 3746795Sbostic #endif 3846795Sbostic #include <fcntl.h> 3946795Sbostic #include <fstab.h> 4046795Sbostic #include <stdio.h> 4146795Sbostic #ifdef __STDC__ 4246795Sbostic #include <unistd.h> 4346795Sbostic #include <stdlib.h> 4446795Sbostic #include <string.h> 4546795Sbostic #endif 461423Sroot #include "dump.h" 4737266Sbostic #include "pathnames.h" 481423Sroot 491423Sroot int notify = 0; /* notify operator flag */ 501423Sroot int blockswritten = 0; /* number of blocks written on current tape */ 511423Sroot int tapeno = 0; /* current tape number */ 5210910Ssam int density = 0; /* density in bytes/0.1" */ 5310910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */ 5410910Ssam int cartridge = 0; /* Assume non-cartridge tape */ 5530560Smckusick long dev_bsize = 1; /* recalculated below */ 5646614Smckusick long blocksperfile; /* output blocks per file */ 5750495Smckusick char *host = NULL; /* remote host (if any) */ 586882Ssam #ifdef RDUMP 5946586Storek int rmthost(); 606882Ssam #endif 611423Sroot 621423Sroot main(argc, argv) 6346794Smckusick int argc; 6446795Sbostic char **argv; 651423Sroot { 6646794Smckusick register ino_t ino; 6747058Smckusick register int dirty; 6846794Smckusick register struct dinode *dp; 6946794Smckusick register struct fstab *dt; 7046794Smckusick register char *map; 7146794Smckusick register char *cp; 7246794Smckusick int i, anydirskipped, bflag = 0; 7346794Smckusick float fetapes; 7446794Smckusick ino_t maxino; 751423Sroot 7650495Smckusick spcl.c_date = 0; 771423Sroot time(&(spcl.c_date)); 781423Sroot 7910910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */ 8037946Sbostic tape = _PATH_DEFTAPE; 8146794Smckusick dumpdates = _PATH_DUMPDATES; 8237266Sbostic temp = _PATH_DTMP; 8346586Storek if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 8446586Storek quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 8546794Smckusick level = '0'; 8646794Smckusick argv++; 8746794Smckusick argc -= 2; 8846794Smckusick for (cp = *argv++; *cp; cp++) { 8946794Smckusick switch (*cp) { 9046794Smckusick case '-': 9146794Smckusick continue; 921423Sroot 9346794Smckusick case 'w': 9446794Smckusick lastdump('w'); /* tell us only what has to be done */ 9546794Smckusick exit(0); 9646794Smckusick 9746794Smckusick case 'W': /* what to do */ 9846794Smckusick lastdump('W'); /* tell us state of what is done */ 9946794Smckusick exit(0); /* do nothing else */ 10046794Smckusick 10146794Smckusick case 'f': /* output file */ 10246794Smckusick if (argc < 1) 10346794Smckusick break; 10446794Smckusick tape = *argv++; 1051423Sroot argc--; 10646794Smckusick continue; 1071423Sroot 10846794Smckusick case 'd': /* density, in bits per inch */ 10946794Smckusick if (argc < 1) 11046794Smckusick break; 11146794Smckusick density = atoi(*argv) / 10; 11246794Smckusick if (density < 1) { 11346794Smckusick fprintf(stderr, "bad density \"%s\"\n", *argv); 11446794Smckusick Exit(X_ABORT); 11546794Smckusick } 11646794Smckusick argc--; 1171423Sroot argv++; 11818494Smckusick if (density >= 625 && !bflag) 11918494Smckusick ntrec = HIGHDENSITYTREC; 12046794Smckusick continue; 1211423Sroot 12246794Smckusick case 's': /* tape size, feet */ 12346794Smckusick if (argc < 1) 12446794Smckusick break; 12546794Smckusick tsize = atol(*argv); 12646794Smckusick if (tsize < 1) { 12746794Smckusick fprintf(stderr, "bad size \"%s\"\n", *argv); 12846794Smckusick Exit(X_ABORT); 12946794Smckusick } 13046794Smckusick argc--; 1311423Sroot argv++; 13246794Smckusick tsize *= 12 * 10; 13346794Smckusick continue; 1341423Sroot 13546794Smckusick case 'b': /* blocks per tape write */ 13646794Smckusick if (argc < 1) 13746794Smckusick break; 13818494Smckusick bflag++; 13947058Smckusick ntrec = atoi(*argv); 14046794Smckusick if (ntrec < 1) { 14146794Smckusick fprintf(stderr, "%s \"%s\"\n", 14246794Smckusick "bad number of blocks per write ", *argv); 14346614Smckusick Exit(X_ABORT); 14446614Smckusick } 14546794Smckusick argc--; 14646794Smckusick argv++; 14746794Smckusick continue; 14810910Ssam 14946794Smckusick case 'B': /* blocks per output file */ 15046794Smckusick if (argc < 1) 15146794Smckusick break; 15246794Smckusick blocksperfile = atol(*argv); 15346794Smckusick if (blocksperfile < 1) { 15446794Smckusick fprintf(stderr, "%s \"%s\"\n", 15546794Smckusick "bad number of blocks per file ", *argv); 15646794Smckusick Exit(X_ABORT); 15746794Smckusick } 15846794Smckusick argc--; 15946614Smckusick argv++; 16046794Smckusick continue; 16146614Smckusick 16246794Smckusick case 'c': /* Tape is cart. not 9-track */ 16346794Smckusick cartridge++; 16446794Smckusick continue; 16510910Ssam 16646794Smckusick case '0': /* dump level */ 16746794Smckusick case '1': 16846794Smckusick case '2': 16946794Smckusick case '3': 17046794Smckusick case '4': 17146794Smckusick case '5': 17246794Smckusick case '6': 17346794Smckusick case '7': 17446794Smckusick case '8': 17546794Smckusick case '9': 17646794Smckusick level = *cp; 17746794Smckusick continue; 1781423Sroot 17946794Smckusick case 'u': /* update /etc/dumpdates */ 18046794Smckusick uflag++; 18146794Smckusick continue; 1821423Sroot 18346794Smckusick case 'n': /* notify operators */ 18446794Smckusick notify++; 18546794Smckusick continue; 1861423Sroot 18746794Smckusick default: 18846794Smckusick fprintf(stderr, "bad key '%c'\n", *cp); 18946794Smckusick Exit(X_ABORT); 19046794Smckusick } 19146794Smckusick fprintf(stderr, "missing argument to '%c'\n", *cp); 1921423Sroot Exit(X_ABORT); 1931423Sroot } 19446794Smckusick if (argc < 1) { 19546794Smckusick fprintf(stderr, "Must specify disk or filesystem\n"); 19646794Smckusick Exit(X_ABORT); 19746794Smckusick } else { 19846794Smckusick disk = *argv++; 1991423Sroot argc--; 2001423Sroot } 20146794Smckusick if (argc >= 1) { 20246794Smckusick fprintf(stderr, "Unknown arguments to dump:"); 20346794Smckusick while (argc--) 20446794Smckusick fprintf(stderr, " %s", *argv++); 20546794Smckusick fprintf(stderr, "\n"); 20646794Smckusick Exit(X_ABORT); 20746794Smckusick } 20812331Smckusick if (strcmp(tape, "-") == 0) { 20912331Smckusick pipeout++; 21012331Smckusick tape = "standard output"; 21112331Smckusick } 21210910Ssam 21346614Smckusick if (blocksperfile) 21446614Smckusick blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 21546614Smckusick else { 21646614Smckusick /* 21746614Smckusick * Determine how to default tape size and density 21846614Smckusick * 21946614Smckusick * density tape size 22046614Smckusick * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 22146614Smckusick * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 22246614Smckusick * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 22346614Smckusick * (450*4 - slop) 22446614Smckusick */ 22546614Smckusick if (density == 0) 22646614Smckusick density = cartridge ? 100 : 160; 22746614Smckusick if (tsize == 0) 22846614Smckusick tsize = cartridge ? 1700L*120L : 2300L*120L; 22946614Smckusick } 23010910Ssam 23150495Smckusick if (index(tape, ':')) { 23250495Smckusick host = tape; 23350495Smckusick tape = index(host, ':'); 23450495Smckusick *tape++ = 0; 2356886Ssam #ifdef RDUMP 23650495Smckusick if (rmthost(host) == 0) 23750495Smckusick exit(X_ABORT); 23850495Smckusick #else 23950495Smckusick fprintf(stderr, "remote dump not enabled\n"); 2406886Ssam exit(X_ABORT); 24150495Smckusick #endif 2426886Ssam } 24328860Smckusick setuid(getuid()); /* rmthost() is the only reason to be setuid */ 24450495Smckusick 24546614Smckusick if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 24646614Smckusick signal(SIGHUP, sighup); 24746614Smckusick if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 24846614Smckusick signal(SIGTRAP, sigtrap); 24946614Smckusick if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 25046614Smckusick signal(SIGFPE, sigfpe); 25146614Smckusick if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 25246614Smckusick signal(SIGBUS, sigbus); 25346614Smckusick if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 25446614Smckusick signal(SIGSEGV, sigsegv); 25546614Smckusick if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 25646614Smckusick signal(SIGTERM, sigterm); 2571423Sroot if (signal(SIGINT, interrupt) == SIG_IGN) 2581423Sroot signal(SIGINT, SIG_IGN); 2591423Sroot 2601423Sroot set_operators(); /* /etc/group snarfed */ 2611423Sroot getfstab(); /* /etc/fstab snarfed */ 2621423Sroot /* 2631423Sroot * disk can be either the full special file name, 2641423Sroot * the suffix of the special file name, 2651423Sroot * the special name missing the leading '/', 2661423Sroot * the file system name with or without the leading '/'. 2671423Sroot */ 2681423Sroot dt = fstabsearch(disk); 26929900Smckusick if (dt != 0) { 2701423Sroot disk = rawname(dt->fs_spec); 27129900Smckusick strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 27229900Smckusick strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 27329900Smckusick } else { 27429900Smckusick strncpy(spcl.c_dev, disk, NAMELEN); 27529900Smckusick strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN); 27629900Smckusick } 27729900Smckusick strcpy(spcl.c_label, "none"); 27829900Smckusick gethostname(spcl.c_host, NAMELEN); 27946794Smckusick spcl.c_level = level - '0'; 28029900Smckusick spcl.c_type = TS_TAPE; 28146794Smckusick getdumptime(); /* /etc/dumpdates snarfed */ 2821423Sroot 28346794Smckusick msg("Date of this level %c dump: %s", level, 28446794Smckusick spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 28546794Smckusick msg("Date of last level %c dump: %s", lastlevel, 28646794Smckusick spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 2871423Sroot msg("Dumping %s ", disk); 2881423Sroot if (dt != 0) 2891423Sroot msgtail("(%s) ", dt->fs_file); 29050495Smckusick if (host) 29150495Smckusick msgtail("to %s on host %s\n", tape, host); 29250495Smckusick else 29350495Smckusick msgtail("to %s\n", tape); 2941423Sroot 29546794Smckusick if ((diskfd = open(disk, O_RDONLY)) < 0) { 2961423Sroot msg("Cannot open %s\n", disk); 2971423Sroot Exit(X_ABORT); 2981423Sroot } 29946794Smckusick sync(); 3005328Smckusic sblock = (struct fs *)buf; 30130560Smckusick bread(SBOFF, sblock, SBSIZE); 30246586Storek if (sblock->fs_magic != FS_MAGIC) 30346586Storek quit("bad sblock magic number\n"); 30430560Smckusick dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 30546586Storek dev_bshift = ffs(dev_bsize) - 1; 30646586Storek if (dev_bsize != (1 << dev_bshift)) 30746586Storek quit("dev_bsize (%d) is not a power of 2", dev_bsize); 30846586Storek tp_bshift = ffs(TP_BSIZE) - 1; 30946586Storek if (TP_BSIZE != (1 << tp_bshift)) 31046586Storek quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 31146794Smckusick maxino = sblock->fs_ipg * sblock->fs_ncg - 1; 31246794Smckusick mapsize = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 3135328Smckusic TP_BSIZE); 31446794Smckusick usedinomap = (char *)calloc(mapsize, sizeof(char)); 31546794Smckusick dumpdirmap = (char *)calloc(mapsize, sizeof(char)); 31646794Smckusick dumpinomap = (char *)calloc(mapsize, sizeof(char)); 31746794Smckusick tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 3181423Sroot 3191423Sroot msg("mapping (Pass I) [regular files]\n"); 32046794Smckusick anydirskipped = mapfiles(maxino, &tapesize); 3211423Sroot 32246794Smckusick msg("mapping (Pass II) [directories]\n"); 32346794Smckusick while (anydirskipped) { 32446794Smckusick anydirskipped = mapdirs(maxino, &tapesize); 32546794Smckusick } 3261423Sroot 32746614Smckusick if (pipeout) 32846794Smckusick tapesize += 10; /* 10 trailer blocks */ 32946614Smckusick else { 33046614Smckusick if (blocksperfile) 33148621Skarels fetapes = (float) tapesize / blocksperfile; 33246614Smckusick else if (cartridge) { 33346614Smckusick /* Estimate number of tapes, assuming streaming stops at 33446614Smckusick the end of each block written, and not in mid-block. 33546614Smckusick Assume no erroneous blocks; this can be compensated 33646614Smckusick for with an artificially low tape size. */ 33746614Smckusick fetapes = 33846794Smckusick ( tapesize /* blocks */ 33946614Smckusick * TP_BSIZE /* bytes/block */ 34046614Smckusick * (1.0/density) /* 0.1" / byte */ 34146614Smckusick + 34246794Smckusick tapesize /* blocks */ 34346614Smckusick * (1.0/ntrec) /* streaming-stops per block */ 34446614Smckusick * 15.48 /* 0.1" / streaming-stop */ 34546614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 34646614Smckusick } else { 34746614Smckusick /* Estimate number of tapes, for old fashioned 9-track 34846614Smckusick tape */ 34946614Smckusick int tenthsperirg = (density == 625) ? 3 : 7; 35046614Smckusick fetapes = 35146794Smckusick ( tapesize /* blocks */ 35246614Smckusick * TP_BSIZE /* bytes / block */ 35346614Smckusick * (1.0/density) /* 0.1" / byte */ 35446614Smckusick + 35546794Smckusick tapesize /* blocks */ 35646614Smckusick * (1.0/ntrec) /* IRG's / block */ 35746614Smckusick * tenthsperirg /* 0.1" / IRG */ 35846614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 35946614Smckusick } 36046614Smckusick etapes = fetapes; /* truncating assignment */ 36146614Smckusick etapes++; 36246794Smckusick /* count the dumped inodes map on each additional tape */ 36346794Smckusick tapesize += (etapes - 1) * 36446794Smckusick (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 36546794Smckusick tapesize += etapes + 10; /* headers + 10 trailer blks */ 36610910Ssam } 36746614Smckusick if (pipeout) 36846794Smckusick msg("estimated %ld tape blocks.\n", tapesize); 36946614Smckusick else 37046614Smckusick msg("estimated %ld tape blocks on %3.2f tape(s).\n", 37146794Smckusick tapesize, fetapes); 3721423Sroot 37310910Ssam alloctape(); /* Allocate tape buffer */ 37410910Ssam 37550495Smckusick startnewtape(1); 3761423Sroot time(&(tstart_writing)); 37746794Smckusick dumpmap(usedinomap, TS_CLRI, maxino); 3781423Sroot 3791423Sroot msg("dumping (Pass III) [directories]\n"); 38046794Smckusick for (map = dumpdirmap, ino = 0; ino < maxino; ) { 38146794Smckusick if ((ino % NBBY) == 0) 38247058Smckusick dirty = *map++; 38346794Smckusick else 38447058Smckusick dirty >>= 1; 38546794Smckusick ino++; 38647058Smckusick if ((dirty & 1) == 0) 38746794Smckusick continue; 38846794Smckusick /* 38946794Smckusick * Skip directory inodes deleted and maybe reallocated 39046794Smckusick */ 39146794Smckusick dp = getino(ino); 39246794Smckusick if ((dp->di_mode & IFMT) != IFDIR) 39346794Smckusick continue; 39446794Smckusick dumpino(dp, ino); 39546794Smckusick } 3961423Sroot 3971423Sroot msg("dumping (Pass IV) [regular files]\n"); 39846794Smckusick for (map = dumpinomap, ino = 0; ino < maxino; ) { 39946794Smckusick if ((ino % NBBY) == 0) 40047058Smckusick dirty = *map++; 40146794Smckusick else 40247058Smckusick dirty >>= 1; 40346794Smckusick ino++; 40447058Smckusick if ((dirty & 1) == 0) 40546794Smckusick continue; 40646794Smckusick /* 40746794Smckusick * Skip inodes deleted and reallocated as directories. 40846794Smckusick */ 40946794Smckusick dp = getino(ino); 41046794Smckusick if ((dp->di_mode & IFMT) == IFDIR) 41146794Smckusick continue; 41246794Smckusick dumpino(dp, ino); 41346794Smckusick } 4141423Sroot 4151423Sroot spcl.c_type = TS_END; 41646794Smckusick for (i = 0; i < ntrec; i++) 41746794Smckusick writeheader(maxino); 41846614Smckusick if (pipeout) 41946614Smckusick msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 42046614Smckusick else 42146614Smckusick msg("DUMP: %ld tape blocks on %d volumes(s)\n", 42246614Smckusick spcl.c_tapea, spcl.c_volume); 42346794Smckusick putdumptime(); 42446239Storek trewind(); 4251423Sroot broadcast("DUMP IS DONE!\7\7\n"); 42650495Smckusick msg("DUMP IS DONE\n"); 4271423Sroot Exit(X_FINOK); 42846586Storek /* NOTREACHED */ 4291423Sroot } 4301423Sroot 43146586Storek void 4321423Sroot sigAbort() 4331423Sroot { 43446586Storek if (pipeout) 43546586Storek quit("Unknown signal, cannot recover\n"); 4361423Sroot msg("Rewriting attempted as response to unknown signal.\n"); 4371423Sroot fflush(stderr); 4381423Sroot fflush(stdout); 4391423Sroot close_rewind(); 4401423Sroot exit(X_REWRITE); 4411423Sroot } 4421423Sroot 44346586Storek void sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();} 44446586Storek void sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();} 44546586Storek void sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();} 44646586Storek void sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();} 44746586Storek void sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();} 44846586Storek void sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();} 44946586Storek void sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();} 45046586Storek 45146586Storek char * 45246586Storek rawname(cp) 4531423Sroot char *cp; 4541423Sroot { 4551423Sroot static char rawbuf[32]; 4564608Smckusic char *rindex(); 4571423Sroot char *dp = rindex(cp, '/'); 4581423Sroot 4591423Sroot if (dp == 0) 4601423Sroot return (0); 4611423Sroot *dp = 0; 4621423Sroot strcpy(rawbuf, cp); 4631423Sroot *dp = '/'; 4641423Sroot strcat(rawbuf, "/r"); 4651423Sroot strcat(rawbuf, dp+1); 4661423Sroot return (rawbuf); 4671423Sroot } 46850496Smckusick 46950496Smckusick #ifdef sunos 47050496Smckusick char * 47150496Smckusick strerror(errnum) 47250496Smckusick int errnum; 47350496Smckusick { 47450496Smckusick extern int sys_nerr; 47550496Smckusick extern char *sys_errlist[]; 47650496Smckusick 47750496Smckusick if (errnum < sys_nerr) { 47850496Smckusick return(sys_errlist[errnum]); 47950496Smckusick } else { 48050496Smckusick return("bogus errno in strerror"); 48150496Smckusick } 48250496Smckusick } 48350496Smckusick #endif 484