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*50496Smckusick static char sccsid[] = "@(#)main.c 5.18 (Berkeley) 07/23/91"; 1647082Smckusick #endif /* not lint */ 1747082Smckusick 18*50496Smckusick #ifdef sunos 19*50496Smckusick #include <stdio.h> 20*50496Smckusick #include <errno.h> 21*50496Smckusick #include <ctype.h> 2246795Sbostic #include <sys/param.h> 23*50496Smckusick #include <sys/stat.h> 24*50496Smckusick #include <sys/time.h> 25*50496Smckusick #include <sys/dir.h> 26*50496Smckusick #include <sys/vnode.h> 27*50496Smckusick #include <ufs/inode.h> 28*50496Smckusick #else 29*50496Smckusick #include <sys/param.h> 3046795Sbostic #include <ufs/dir.h> 3146795Sbostic #include <ufs/dinode.h> 32*50496Smckusick #endif 3346795Sbostic #include <ufs/fs.h> 3446795Sbostic #include <protocols/dumprestore.h> 3546795Sbostic #include <signal.h> 3646795Sbostic #ifdef __STDC__ 3746795Sbostic #include <time.h> 3846795Sbostic #endif 3946795Sbostic #include <fcntl.h> 4046795Sbostic #include <fstab.h> 4146795Sbostic #include <stdio.h> 4246795Sbostic #ifdef __STDC__ 4346795Sbostic #include <unistd.h> 4446795Sbostic #include <stdlib.h> 4546795Sbostic #include <string.h> 4646795Sbostic #endif 471423Sroot #include "dump.h" 4837266Sbostic #include "pathnames.h" 491423Sroot 501423Sroot int notify = 0; /* notify operator flag */ 511423Sroot int blockswritten = 0; /* number of blocks written on current tape */ 521423Sroot int tapeno = 0; /* current tape number */ 5310910Ssam int density = 0; /* density in bytes/0.1" */ 5410910Ssam int ntrec = NTREC; /* # tape blocks in each tape record */ 5510910Ssam int cartridge = 0; /* Assume non-cartridge tape */ 5630560Smckusick long dev_bsize = 1; /* recalculated below */ 5746614Smckusick long blocksperfile; /* output blocks per file */ 5850495Smckusick char *host = NULL; /* remote host (if any) */ 596882Ssam #ifdef RDUMP 6046586Storek int rmthost(); 616882Ssam #endif 621423Sroot 631423Sroot main(argc, argv) 6446794Smckusick int argc; 6546795Sbostic char **argv; 661423Sroot { 6746794Smckusick register ino_t ino; 6847058Smckusick register int dirty; 6946794Smckusick register struct dinode *dp; 7046794Smckusick register struct fstab *dt; 7146794Smckusick register char *map; 7246794Smckusick register char *cp; 7346794Smckusick int i, anydirskipped, bflag = 0; 7446794Smckusick float fetapes; 7546794Smckusick ino_t maxino; 761423Sroot 7750495Smckusick spcl.c_date = 0; 781423Sroot time(&(spcl.c_date)); 791423Sroot 8010910Ssam tsize = 0; /* Default later, based on 'c' option for cart tapes */ 8137946Sbostic tape = _PATH_DEFTAPE; 8246794Smckusick dumpdates = _PATH_DUMPDATES; 8337266Sbostic temp = _PATH_DTMP; 8446586Storek if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0) 8546586Storek quit("TP_BSIZE must be a multiple of DEV_BSIZE\n"); 8646794Smckusick level = '0'; 8746794Smckusick argv++; 8846794Smckusick argc -= 2; 8946794Smckusick for (cp = *argv++; *cp; cp++) { 9046794Smckusick switch (*cp) { 9146794Smckusick case '-': 9246794Smckusick continue; 931423Sroot 9446794Smckusick case 'w': 9546794Smckusick lastdump('w'); /* tell us only what has to be done */ 9646794Smckusick exit(0); 9746794Smckusick 9846794Smckusick case 'W': /* what to do */ 9946794Smckusick lastdump('W'); /* tell us state of what is done */ 10046794Smckusick exit(0); /* do nothing else */ 10146794Smckusick 10246794Smckusick case 'f': /* output file */ 10346794Smckusick if (argc < 1) 10446794Smckusick break; 10546794Smckusick tape = *argv++; 1061423Sroot argc--; 10746794Smckusick continue; 1081423Sroot 10946794Smckusick case 'd': /* density, in bits per inch */ 11046794Smckusick if (argc < 1) 11146794Smckusick break; 11246794Smckusick density = atoi(*argv) / 10; 11346794Smckusick if (density < 1) { 11446794Smckusick fprintf(stderr, "bad density \"%s\"\n", *argv); 11546794Smckusick Exit(X_ABORT); 11646794Smckusick } 11746794Smckusick argc--; 1181423Sroot argv++; 11918494Smckusick if (density >= 625 && !bflag) 12018494Smckusick ntrec = HIGHDENSITYTREC; 12146794Smckusick continue; 1221423Sroot 12346794Smckusick case 's': /* tape size, feet */ 12446794Smckusick if (argc < 1) 12546794Smckusick break; 12646794Smckusick tsize = atol(*argv); 12746794Smckusick if (tsize < 1) { 12846794Smckusick fprintf(stderr, "bad size \"%s\"\n", *argv); 12946794Smckusick Exit(X_ABORT); 13046794Smckusick } 13146794Smckusick argc--; 1321423Sroot argv++; 13346794Smckusick tsize *= 12 * 10; 13446794Smckusick continue; 1351423Sroot 13646794Smckusick case 'b': /* blocks per tape write */ 13746794Smckusick if (argc < 1) 13846794Smckusick break; 13918494Smckusick bflag++; 14047058Smckusick ntrec = atoi(*argv); 14146794Smckusick if (ntrec < 1) { 14246794Smckusick fprintf(stderr, "%s \"%s\"\n", 14346794Smckusick "bad number of blocks per write ", *argv); 14446614Smckusick Exit(X_ABORT); 14546614Smckusick } 14646794Smckusick argc--; 14746794Smckusick argv++; 14846794Smckusick continue; 14910910Ssam 15046794Smckusick case 'B': /* blocks per output file */ 15146794Smckusick if (argc < 1) 15246794Smckusick break; 15346794Smckusick blocksperfile = atol(*argv); 15446794Smckusick if (blocksperfile < 1) { 15546794Smckusick fprintf(stderr, "%s \"%s\"\n", 15646794Smckusick "bad number of blocks per file ", *argv); 15746794Smckusick Exit(X_ABORT); 15846794Smckusick } 15946794Smckusick argc--; 16046614Smckusick argv++; 16146794Smckusick continue; 16246614Smckusick 16346794Smckusick case 'c': /* Tape is cart. not 9-track */ 16446794Smckusick cartridge++; 16546794Smckusick continue; 16610910Ssam 16746794Smckusick case '0': /* dump level */ 16846794Smckusick case '1': 16946794Smckusick case '2': 17046794Smckusick case '3': 17146794Smckusick case '4': 17246794Smckusick case '5': 17346794Smckusick case '6': 17446794Smckusick case '7': 17546794Smckusick case '8': 17646794Smckusick case '9': 17746794Smckusick level = *cp; 17846794Smckusick continue; 1791423Sroot 18046794Smckusick case 'u': /* update /etc/dumpdates */ 18146794Smckusick uflag++; 18246794Smckusick continue; 1831423Sroot 18446794Smckusick case 'n': /* notify operators */ 18546794Smckusick notify++; 18646794Smckusick continue; 1871423Sroot 18846794Smckusick default: 18946794Smckusick fprintf(stderr, "bad key '%c'\n", *cp); 19046794Smckusick Exit(X_ABORT); 19146794Smckusick } 19246794Smckusick fprintf(stderr, "missing argument to '%c'\n", *cp); 1931423Sroot Exit(X_ABORT); 1941423Sroot } 19546794Smckusick if (argc < 1) { 19646794Smckusick fprintf(stderr, "Must specify disk or filesystem\n"); 19746794Smckusick Exit(X_ABORT); 19846794Smckusick } else { 19946794Smckusick disk = *argv++; 2001423Sroot argc--; 2011423Sroot } 20246794Smckusick if (argc >= 1) { 20346794Smckusick fprintf(stderr, "Unknown arguments to dump:"); 20446794Smckusick while (argc--) 20546794Smckusick fprintf(stderr, " %s", *argv++); 20646794Smckusick fprintf(stderr, "\n"); 20746794Smckusick Exit(X_ABORT); 20846794Smckusick } 20912331Smckusick if (strcmp(tape, "-") == 0) { 21012331Smckusick pipeout++; 21112331Smckusick tape = "standard output"; 21212331Smckusick } 21310910Ssam 21446614Smckusick if (blocksperfile) 21546614Smckusick blocksperfile = blocksperfile / ntrec * ntrec; /* round down */ 21646614Smckusick else { 21746614Smckusick /* 21846614Smckusick * Determine how to default tape size and density 21946614Smckusick * 22046614Smckusick * density tape size 22146614Smckusick * 9-track 1600 bpi (160 bytes/.1") 2300 ft. 22246614Smckusick * 9-track 6250 bpi (625 bytes/.1") 2300 ft. 22346614Smckusick * cartridge 8000 bpi (100 bytes/.1") 1700 ft. 22446614Smckusick * (450*4 - slop) 22546614Smckusick */ 22646614Smckusick if (density == 0) 22746614Smckusick density = cartridge ? 100 : 160; 22846614Smckusick if (tsize == 0) 22946614Smckusick tsize = cartridge ? 1700L*120L : 2300L*120L; 23046614Smckusick } 23110910Ssam 23250495Smckusick if (index(tape, ':')) { 23350495Smckusick host = tape; 23450495Smckusick tape = index(host, ':'); 23550495Smckusick *tape++ = 0; 2366886Ssam #ifdef RDUMP 23750495Smckusick if (rmthost(host) == 0) 23850495Smckusick exit(X_ABORT); 23950495Smckusick #else 24050495Smckusick fprintf(stderr, "remote dump not enabled\n"); 2416886Ssam exit(X_ABORT); 24250495Smckusick #endif 2436886Ssam } 24428860Smckusick setuid(getuid()); /* rmthost() is the only reason to be setuid */ 24550495Smckusick 24646614Smckusick if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 24746614Smckusick signal(SIGHUP, sighup); 24846614Smckusick if (signal(SIGTRAP, SIG_IGN) != SIG_IGN) 24946614Smckusick signal(SIGTRAP, sigtrap); 25046614Smckusick if (signal(SIGFPE, SIG_IGN) != SIG_IGN) 25146614Smckusick signal(SIGFPE, sigfpe); 25246614Smckusick if (signal(SIGBUS, SIG_IGN) != SIG_IGN) 25346614Smckusick signal(SIGBUS, sigbus); 25446614Smckusick if (signal(SIGSEGV, SIG_IGN) != SIG_IGN) 25546614Smckusick signal(SIGSEGV, sigsegv); 25646614Smckusick if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 25746614Smckusick signal(SIGTERM, sigterm); 2581423Sroot if (signal(SIGINT, interrupt) == SIG_IGN) 2591423Sroot signal(SIGINT, SIG_IGN); 2601423Sroot 2611423Sroot set_operators(); /* /etc/group snarfed */ 2621423Sroot getfstab(); /* /etc/fstab snarfed */ 2631423Sroot /* 2641423Sroot * disk can be either the full special file name, 2651423Sroot * the suffix of the special file name, 2661423Sroot * the special name missing the leading '/', 2671423Sroot * the file system name with or without the leading '/'. 2681423Sroot */ 2691423Sroot dt = fstabsearch(disk); 27029900Smckusick if (dt != 0) { 2711423Sroot disk = rawname(dt->fs_spec); 27229900Smckusick strncpy(spcl.c_dev, dt->fs_spec, NAMELEN); 27329900Smckusick strncpy(spcl.c_filesys, dt->fs_file, NAMELEN); 27429900Smckusick } else { 27529900Smckusick strncpy(spcl.c_dev, disk, NAMELEN); 27629900Smckusick strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN); 27729900Smckusick } 27829900Smckusick strcpy(spcl.c_label, "none"); 27929900Smckusick gethostname(spcl.c_host, NAMELEN); 28046794Smckusick spcl.c_level = level - '0'; 28129900Smckusick spcl.c_type = TS_TAPE; 28246794Smckusick getdumptime(); /* /etc/dumpdates snarfed */ 2831423Sroot 28446794Smckusick msg("Date of this level %c dump: %s", level, 28546794Smckusick spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date)); 28646794Smckusick msg("Date of last level %c dump: %s", lastlevel, 28746794Smckusick spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate)); 2881423Sroot msg("Dumping %s ", disk); 2891423Sroot if (dt != 0) 2901423Sroot msgtail("(%s) ", dt->fs_file); 29150495Smckusick if (host) 29250495Smckusick msgtail("to %s on host %s\n", tape, host); 29350495Smckusick else 29450495Smckusick msgtail("to %s\n", tape); 2951423Sroot 29646794Smckusick if ((diskfd = open(disk, O_RDONLY)) < 0) { 2971423Sroot msg("Cannot open %s\n", disk); 2981423Sroot Exit(X_ABORT); 2991423Sroot } 30046794Smckusick sync(); 3015328Smckusic sblock = (struct fs *)buf; 30230560Smckusick bread(SBOFF, sblock, SBSIZE); 30346586Storek if (sblock->fs_magic != FS_MAGIC) 30446586Storek quit("bad sblock magic number\n"); 30530560Smckusick dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1); 30646586Storek dev_bshift = ffs(dev_bsize) - 1; 30746586Storek if (dev_bsize != (1 << dev_bshift)) 30846586Storek quit("dev_bsize (%d) is not a power of 2", dev_bsize); 30946586Storek tp_bshift = ffs(TP_BSIZE) - 1; 31046586Storek if (TP_BSIZE != (1 << tp_bshift)) 31146586Storek quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE); 31246794Smckusick maxino = sblock->fs_ipg * sblock->fs_ncg - 1; 31346794Smckusick mapsize = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY), 3145328Smckusic TP_BSIZE); 31546794Smckusick usedinomap = (char *)calloc(mapsize, sizeof(char)); 31646794Smckusick dumpdirmap = (char *)calloc(mapsize, sizeof(char)); 31746794Smckusick dumpinomap = (char *)calloc(mapsize, sizeof(char)); 31846794Smckusick tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 3191423Sroot 3201423Sroot msg("mapping (Pass I) [regular files]\n"); 32146794Smckusick anydirskipped = mapfiles(maxino, &tapesize); 3221423Sroot 32346794Smckusick msg("mapping (Pass II) [directories]\n"); 32446794Smckusick while (anydirskipped) { 32546794Smckusick anydirskipped = mapdirs(maxino, &tapesize); 32646794Smckusick } 3271423Sroot 32846614Smckusick if (pipeout) 32946794Smckusick tapesize += 10; /* 10 trailer blocks */ 33046614Smckusick else { 33146614Smckusick if (blocksperfile) 33248621Skarels fetapes = (float) tapesize / blocksperfile; 33346614Smckusick else if (cartridge) { 33446614Smckusick /* Estimate number of tapes, assuming streaming stops at 33546614Smckusick the end of each block written, and not in mid-block. 33646614Smckusick Assume no erroneous blocks; this can be compensated 33746614Smckusick for with an artificially low tape size. */ 33846614Smckusick fetapes = 33946794Smckusick ( tapesize /* blocks */ 34046614Smckusick * TP_BSIZE /* bytes/block */ 34146614Smckusick * (1.0/density) /* 0.1" / byte */ 34246614Smckusick + 34346794Smckusick tapesize /* blocks */ 34446614Smckusick * (1.0/ntrec) /* streaming-stops per block */ 34546614Smckusick * 15.48 /* 0.1" / streaming-stop */ 34646614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 34746614Smckusick } else { 34846614Smckusick /* Estimate number of tapes, for old fashioned 9-track 34946614Smckusick tape */ 35046614Smckusick int tenthsperirg = (density == 625) ? 3 : 7; 35146614Smckusick fetapes = 35246794Smckusick ( tapesize /* blocks */ 35346614Smckusick * TP_BSIZE /* bytes / block */ 35446614Smckusick * (1.0/density) /* 0.1" / byte */ 35546614Smckusick + 35646794Smckusick tapesize /* blocks */ 35746614Smckusick * (1.0/ntrec) /* IRG's / block */ 35846614Smckusick * tenthsperirg /* 0.1" / IRG */ 35946614Smckusick ) * (1.0 / tsize ); /* tape / 0.1" */ 36046614Smckusick } 36146614Smckusick etapes = fetapes; /* truncating assignment */ 36246614Smckusick etapes++; 36346794Smckusick /* count the dumped inodes map on each additional tape */ 36446794Smckusick tapesize += (etapes - 1) * 36546794Smckusick (howmany(mapsize * sizeof(char), TP_BSIZE) + 1); 36646794Smckusick tapesize += etapes + 10; /* headers + 10 trailer blks */ 36710910Ssam } 36846614Smckusick if (pipeout) 36946794Smckusick msg("estimated %ld tape blocks.\n", tapesize); 37046614Smckusick else 37146614Smckusick msg("estimated %ld tape blocks on %3.2f tape(s).\n", 37246794Smckusick tapesize, fetapes); 3731423Sroot 37410910Ssam alloctape(); /* Allocate tape buffer */ 37510910Ssam 37650495Smckusick startnewtape(1); 3771423Sroot time(&(tstart_writing)); 37846794Smckusick dumpmap(usedinomap, TS_CLRI, maxino); 3791423Sroot 3801423Sroot msg("dumping (Pass III) [directories]\n"); 38146794Smckusick for (map = dumpdirmap, ino = 0; ino < maxino; ) { 38246794Smckusick if ((ino % NBBY) == 0) 38347058Smckusick dirty = *map++; 38446794Smckusick else 38547058Smckusick dirty >>= 1; 38646794Smckusick ino++; 38747058Smckusick if ((dirty & 1) == 0) 38846794Smckusick continue; 38946794Smckusick /* 39046794Smckusick * Skip directory inodes deleted and maybe reallocated 39146794Smckusick */ 39246794Smckusick dp = getino(ino); 39346794Smckusick if ((dp->di_mode & IFMT) != IFDIR) 39446794Smckusick continue; 39546794Smckusick dumpino(dp, ino); 39646794Smckusick } 3971423Sroot 3981423Sroot msg("dumping (Pass IV) [regular files]\n"); 39946794Smckusick for (map = dumpinomap, ino = 0; ino < maxino; ) { 40046794Smckusick if ((ino % NBBY) == 0) 40147058Smckusick dirty = *map++; 40246794Smckusick else 40347058Smckusick dirty >>= 1; 40446794Smckusick ino++; 40547058Smckusick if ((dirty & 1) == 0) 40646794Smckusick continue; 40746794Smckusick /* 40846794Smckusick * Skip inodes deleted and reallocated as directories. 40946794Smckusick */ 41046794Smckusick dp = getino(ino); 41146794Smckusick if ((dp->di_mode & IFMT) == IFDIR) 41246794Smckusick continue; 41346794Smckusick dumpino(dp, ino); 41446794Smckusick } 4151423Sroot 4161423Sroot spcl.c_type = TS_END; 41746794Smckusick for (i = 0; i < ntrec; i++) 41846794Smckusick writeheader(maxino); 41946614Smckusick if (pipeout) 42046614Smckusick msg("DUMP: %ld tape blocks\n",spcl.c_tapea); 42146614Smckusick else 42246614Smckusick msg("DUMP: %ld tape blocks on %d volumes(s)\n", 42346614Smckusick spcl.c_tapea, spcl.c_volume); 42446794Smckusick putdumptime(); 42546239Storek trewind(); 4261423Sroot broadcast("DUMP IS DONE!\7\7\n"); 42750495Smckusick msg("DUMP IS DONE\n"); 4281423Sroot Exit(X_FINOK); 42946586Storek /* NOTREACHED */ 4301423Sroot } 4311423Sroot 43246586Storek void 4331423Sroot sigAbort() 4341423Sroot { 43546586Storek if (pipeout) 43646586Storek quit("Unknown signal, cannot recover\n"); 4371423Sroot msg("Rewriting attempted as response to unknown signal.\n"); 4381423Sroot fflush(stderr); 4391423Sroot fflush(stdout); 4401423Sroot close_rewind(); 4411423Sroot exit(X_REWRITE); 4421423Sroot } 4431423Sroot 44446586Storek void sighup(){ msg("SIGHUP() try rewriting\n"); sigAbort();} 44546586Storek void sigtrap(){ msg("SIGTRAP() try rewriting\n"); sigAbort();} 44646586Storek void sigfpe(){ msg("SIGFPE() try rewriting\n"); sigAbort();} 44746586Storek void sigbus(){ msg("SIGBUS() try rewriting\n"); sigAbort();} 44846586Storek void sigsegv(){ msg("SIGSEGV() ABORTING!\n"); abort();} 44946586Storek void sigalrm(){ msg("SIGALRM() try rewriting\n"); sigAbort();} 45046586Storek void sigterm(){ msg("SIGTERM() try rewriting\n"); sigAbort();} 45146586Storek 45246586Storek char * 45346586Storek rawname(cp) 4541423Sroot char *cp; 4551423Sroot { 4561423Sroot static char rawbuf[32]; 4574608Smckusic char *rindex(); 4581423Sroot char *dp = rindex(cp, '/'); 4591423Sroot 4601423Sroot if (dp == 0) 4611423Sroot return (0); 4621423Sroot *dp = 0; 4631423Sroot strcpy(rawbuf, cp); 4641423Sroot *dp = '/'; 4651423Sroot strcat(rawbuf, "/r"); 4661423Sroot strcat(rawbuf, dp+1); 4671423Sroot return (rawbuf); 4681423Sroot } 469*50496Smckusick 470*50496Smckusick #ifdef sunos 471*50496Smckusick char * 472*50496Smckusick strerror(errnum) 473*50496Smckusick int errnum; 474*50496Smckusick { 475*50496Smckusick extern int sys_nerr; 476*50496Smckusick extern char *sys_errlist[]; 477*50496Smckusick 478*50496Smckusick if (errnum < sys_nerr) { 479*50496Smckusick return(sys_errlist[errnum]); 480*50496Smckusick } else { 481*50496Smckusick return("bogus errno in strerror"); 482*50496Smckusick } 483*50496Smckusick } 484*50496Smckusick #endif 485