xref: /csrg-svn/sbin/dump/main.c (revision 50495)
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*50495Smckusick static char sccsid[] = "@(#)main.c	5.17 (Berkeley) 07/23/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 */
46*50495Smckusick char	*host = NULL;	/* remote host (if any) */
476882Ssam #ifdef RDUMP
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 
65*50495Smckusick 	spcl.c_date = 0;
661423Sroot 	time(&(spcl.c_date));
671423Sroot 
6810910Ssam 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
6937946Sbostic 	tape = _PATH_DEFTAPE;
7046794Smckusick 	dumpdates = _PATH_DUMPDATES;
7137266Sbostic 	temp = _PATH_DTMP;
7246586Storek 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
7346586Storek 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
7446794Smckusick 	level = '0';
7546794Smckusick 	argv++;
7646794Smckusick 	argc -= 2;
7746794Smckusick 	for (cp = *argv++; *cp; cp++) {
7846794Smckusick 		switch (*cp) {
7946794Smckusick 		case '-':
8046794Smckusick 			continue;
811423Sroot 
8246794Smckusick 		case 'w':
8346794Smckusick 			lastdump('w');	/* tell us only what has to be done */
8446794Smckusick 			exit(0);
8546794Smckusick 
8646794Smckusick 		case 'W':		/* what to do */
8746794Smckusick 			lastdump('W');	/* tell us state of what is done */
8846794Smckusick 			exit(0);	/* do nothing else */
8946794Smckusick 
9046794Smckusick 		case 'f':		/* output file */
9146794Smckusick 			if (argc < 1)
9246794Smckusick 				break;
9346794Smckusick 			tape = *argv++;
941423Sroot 			argc--;
9546794Smckusick 			continue;
961423Sroot 
9746794Smckusick 		case 'd':		/* density, in bits per inch */
9846794Smckusick 			if (argc < 1)
9946794Smckusick 				break;
10046794Smckusick 			density = atoi(*argv) / 10;
10146794Smckusick 			if (density < 1) {
10246794Smckusick 				fprintf(stderr, "bad density \"%s\"\n", *argv);
10346794Smckusick 				Exit(X_ABORT);
10446794Smckusick 			}
10546794Smckusick 			argc--;
1061423Sroot 			argv++;
10718494Smckusick 			if (density >= 625 && !bflag)
10818494Smckusick 				ntrec = HIGHDENSITYTREC;
10946794Smckusick 			continue;
1101423Sroot 
11146794Smckusick 		case 's':		/* tape size, feet */
11246794Smckusick 			if (argc < 1)
11346794Smckusick 				break;
11446794Smckusick 			tsize = atol(*argv);
11546794Smckusick 			if (tsize < 1) {
11646794Smckusick 				fprintf(stderr, "bad size \"%s\"\n", *argv);
11746794Smckusick 				Exit(X_ABORT);
11846794Smckusick 			}
11946794Smckusick 			argc--;
1201423Sroot 			argv++;
12146794Smckusick 			tsize *= 12 * 10;
12246794Smckusick 			continue;
1231423Sroot 
12446794Smckusick 		case 'b':		/* blocks per tape write */
12546794Smckusick 			if (argc < 1)
12646794Smckusick 				break;
12718494Smckusick 			bflag++;
12847058Smckusick 			ntrec = atoi(*argv);
12946794Smckusick 			if (ntrec < 1) {
13046794Smckusick 				fprintf(stderr, "%s \"%s\"\n",
13146794Smckusick 				    "bad number of blocks per write ", *argv);
13246614Smckusick 				Exit(X_ABORT);
13346614Smckusick 			}
13446794Smckusick 			argc--;
13546794Smckusick 			argv++;
13646794Smckusick 			continue;
13710910Ssam 
13846794Smckusick 		case 'B':		/* blocks per output file */
13946794Smckusick 			if (argc < 1)
14046794Smckusick 				break;
14146794Smckusick 			blocksperfile = atol(*argv);
14246794Smckusick 			if (blocksperfile < 1) {
14346794Smckusick 				fprintf(stderr, "%s \"%s\"\n",
14446794Smckusick 				    "bad number of blocks per file ", *argv);
14546794Smckusick 				Exit(X_ABORT);
14646794Smckusick 			}
14746794Smckusick 			argc--;
14846614Smckusick 			argv++;
14946794Smckusick 			continue;
15046614Smckusick 
15146794Smckusick 		case 'c':		/* Tape is cart. not 9-track */
15246794Smckusick 			cartridge++;
15346794Smckusick 			continue;
15410910Ssam 
15546794Smckusick 		case '0':		/* dump level */
15646794Smckusick 		case '1':
15746794Smckusick 		case '2':
15846794Smckusick 		case '3':
15946794Smckusick 		case '4':
16046794Smckusick 		case '5':
16146794Smckusick 		case '6':
16246794Smckusick 		case '7':
16346794Smckusick 		case '8':
16446794Smckusick 		case '9':
16546794Smckusick 			level = *cp;
16646794Smckusick 			continue;
1671423Sroot 
16846794Smckusick 		case 'u':		/* update /etc/dumpdates */
16946794Smckusick 			uflag++;
17046794Smckusick 			continue;
1711423Sroot 
17246794Smckusick 		case 'n':		/* notify operators */
17346794Smckusick 			notify++;
17446794Smckusick 			continue;
1751423Sroot 
17646794Smckusick 		default:
17746794Smckusick 			fprintf(stderr, "bad key '%c'\n", *cp);
17846794Smckusick 			Exit(X_ABORT);
17946794Smckusick 		}
18046794Smckusick 		fprintf(stderr, "missing argument to '%c'\n", *cp);
1811423Sroot 		Exit(X_ABORT);
1821423Sroot 	}
18346794Smckusick 	if (argc < 1) {
18446794Smckusick 		fprintf(stderr, "Must specify disk or filesystem\n");
18546794Smckusick 		Exit(X_ABORT);
18646794Smckusick 	} else {
18746794Smckusick 		disk = *argv++;
1881423Sroot 		argc--;
1891423Sroot 	}
19046794Smckusick 	if (argc >= 1) {
19146794Smckusick 		fprintf(stderr, "Unknown arguments to dump:");
19246794Smckusick 		while (argc--)
19346794Smckusick 			fprintf(stderr, " %s", *argv++);
19446794Smckusick 		fprintf(stderr, "\n");
19546794Smckusick 		Exit(X_ABORT);
19646794Smckusick 	}
19712331Smckusick 	if (strcmp(tape, "-") == 0) {
19812331Smckusick 		pipeout++;
19912331Smckusick 		tape = "standard output";
20012331Smckusick 	}
20110910Ssam 
20246614Smckusick 	if (blocksperfile)
20346614Smckusick 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
20446614Smckusick 	else {
20546614Smckusick 		/*
20646614Smckusick 		 * Determine how to default tape size and density
20746614Smckusick 		 *
20846614Smckusick 		 *         	density				tape size
20946614Smckusick 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
21046614Smckusick 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
21146614Smckusick 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
21246614Smckusick 		 *						(450*4 - slop)
21346614Smckusick 		 */
21446614Smckusick 		if (density == 0)
21546614Smckusick 			density = cartridge ? 100 : 160;
21646614Smckusick 		if (tsize == 0)
21746614Smckusick 			tsize = cartridge ? 1700L*120L : 2300L*120L;
21846614Smckusick 	}
21910910Ssam 
220*50495Smckusick 	if (index(tape, ':')) {
221*50495Smckusick 		host = tape;
222*50495Smckusick 		tape = index(host, ':');
223*50495Smckusick 		*tape++ = 0;
2246886Ssam #ifdef RDUMP
225*50495Smckusick 		if (rmthost(host) == 0)
226*50495Smckusick 			exit(X_ABORT);
227*50495Smckusick #else
228*50495Smckusick 		fprintf(stderr, "remote dump not enabled\n");
2296886Ssam 		exit(X_ABORT);
230*50495Smckusick #endif
2316886Ssam 	}
23228860Smckusick 	setuid(getuid());	/* rmthost() is the only reason to be setuid */
233*50495Smckusick 
23446614Smckusick 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
23546614Smckusick 		signal(SIGHUP, sighup);
23646614Smckusick 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
23746614Smckusick 		signal(SIGTRAP, sigtrap);
23846614Smckusick 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
23946614Smckusick 		signal(SIGFPE, sigfpe);
24046614Smckusick 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
24146614Smckusick 		signal(SIGBUS, sigbus);
24246614Smckusick 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
24346614Smckusick 		signal(SIGSEGV, sigsegv);
24446614Smckusick 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
24546614Smckusick 		signal(SIGTERM, sigterm);
2461423Sroot 	if (signal(SIGINT, interrupt) == SIG_IGN)
2471423Sroot 		signal(SIGINT, SIG_IGN);
2481423Sroot 
2491423Sroot 	set_operators();	/* /etc/group snarfed */
2501423Sroot 	getfstab();		/* /etc/fstab snarfed */
2511423Sroot 	/*
2521423Sroot 	 *	disk can be either the full special file name,
2531423Sroot 	 *	the suffix of the special file name,
2541423Sroot 	 *	the special name missing the leading '/',
2551423Sroot 	 *	the file system name with or without the leading '/'.
2561423Sroot 	 */
2571423Sroot 	dt = fstabsearch(disk);
25829900Smckusick 	if (dt != 0) {
2591423Sroot 		disk = rawname(dt->fs_spec);
26029900Smckusick 		strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
26129900Smckusick 		strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
26229900Smckusick 	} else {
26329900Smckusick 		strncpy(spcl.c_dev, disk, NAMELEN);
26429900Smckusick 		strncpy(spcl.c_filesys, "an unlisted file system", NAMELEN);
26529900Smckusick 	}
26629900Smckusick 	strcpy(spcl.c_label, "none");
26729900Smckusick 	gethostname(spcl.c_host, NAMELEN);
26846794Smckusick 	spcl.c_level = level - '0';
26929900Smckusick 	spcl.c_type = TS_TAPE;
27046794Smckusick 	getdumptime();		/* /etc/dumpdates snarfed */
2711423Sroot 
27246794Smckusick 	msg("Date of this level %c dump: %s", level,
27346794Smckusick 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
27446794Smckusick  	msg("Date of last level %c dump: %s", lastlevel,
27546794Smckusick 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
2761423Sroot 	msg("Dumping %s ", disk);
2771423Sroot 	if (dt != 0)
2781423Sroot 		msgtail("(%s) ", dt->fs_file);
279*50495Smckusick 	if (host)
280*50495Smckusick 		msgtail("to %s on host %s\n", tape, host);
281*50495Smckusick 	else
282*50495Smckusick 		msgtail("to %s\n", tape);
2831423Sroot 
28446794Smckusick 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
2851423Sroot 		msg("Cannot open %s\n", disk);
2861423Sroot 		Exit(X_ABORT);
2871423Sroot 	}
28846794Smckusick 	sync();
2895328Smckusic 	sblock = (struct fs *)buf;
29030560Smckusick 	bread(SBOFF, sblock, SBSIZE);
29146586Storek 	if (sblock->fs_magic != FS_MAGIC)
29246586Storek 		quit("bad sblock magic number\n");
29330560Smckusick 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
29446586Storek 	dev_bshift = ffs(dev_bsize) - 1;
29546586Storek 	if (dev_bsize != (1 << dev_bshift))
29646586Storek 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
29746586Storek 	tp_bshift = ffs(TP_BSIZE) - 1;
29846586Storek 	if (TP_BSIZE != (1 << tp_bshift))
29946586Storek 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
30046794Smckusick 	maxino = sblock->fs_ipg * sblock->fs_ncg - 1;
30146794Smckusick 	mapsize = roundup(howmany(sblock->fs_ipg * sblock->fs_ncg, NBBY),
3025328Smckusic 		TP_BSIZE);
30346794Smckusick 	usedinomap = (char *)calloc(mapsize, sizeof(char));
30446794Smckusick 	dumpdirmap = (char *)calloc(mapsize, sizeof(char));
30546794Smckusick 	dumpinomap = (char *)calloc(mapsize, sizeof(char));
30646794Smckusick 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
3071423Sroot 
3081423Sroot 	msg("mapping (Pass I) [regular files]\n");
30946794Smckusick 	anydirskipped = mapfiles(maxino, &tapesize);
3101423Sroot 
31146794Smckusick 	msg("mapping (Pass II) [directories]\n");
31246794Smckusick 	while (anydirskipped) {
31346794Smckusick 		anydirskipped = mapdirs(maxino, &tapesize);
31446794Smckusick 	}
3151423Sroot 
31646614Smckusick 	if (pipeout)
31746794Smckusick 		tapesize += 10;	/* 10 trailer blocks */
31846614Smckusick 	else {
31946614Smckusick 		if (blocksperfile)
32048621Skarels 			fetapes = (float) tapesize / blocksperfile;
32146614Smckusick 		else if (cartridge) {
32246614Smckusick 			/* Estimate number of tapes, assuming streaming stops at
32346614Smckusick 			   the end of each block written, and not in mid-block.
32446614Smckusick 			   Assume no erroneous blocks; this can be compensated
32546614Smckusick 			   for with an artificially low tape size. */
32646614Smckusick 			fetapes =
32746794Smckusick 			(	  tapesize	/* blocks */
32846614Smckusick 				* TP_BSIZE	/* bytes/block */
32946614Smckusick 				* (1.0/density)	/* 0.1" / byte */
33046614Smckusick 			  +
33146794Smckusick 				  tapesize	/* blocks */
33246614Smckusick 				* (1.0/ntrec)	/* streaming-stops per block */
33346614Smckusick 				* 15.48		/* 0.1" / streaming-stop */
33446614Smckusick 			) * (1.0 / tsize );	/* tape / 0.1" */
33546614Smckusick 		} else {
33646614Smckusick 			/* Estimate number of tapes, for old fashioned 9-track
33746614Smckusick 			   tape */
33846614Smckusick 			int tenthsperirg = (density == 625) ? 3 : 7;
33946614Smckusick 			fetapes =
34046794Smckusick 			(	  tapesize	/* blocks */
34146614Smckusick 				* TP_BSIZE	/* bytes / block */
34246614Smckusick 				* (1.0/density)	/* 0.1" / byte */
34346614Smckusick 			  +
34446794Smckusick 				  tapesize	/* blocks */
34546614Smckusick 				* (1.0/ntrec)	/* IRG's / block */
34646614Smckusick 				* tenthsperirg	/* 0.1" / IRG */
34746614Smckusick 			) * (1.0 / tsize );	/* tape / 0.1" */
34846614Smckusick 		}
34946614Smckusick 		etapes = fetapes;		/* truncating assignment */
35046614Smckusick 		etapes++;
35146794Smckusick 		/* count the dumped inodes map on each additional tape */
35246794Smckusick 		tapesize += (etapes - 1) *
35346794Smckusick 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
35446794Smckusick 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
35510910Ssam 	}
35646614Smckusick 	if (pipeout)
35746794Smckusick 		msg("estimated %ld tape blocks.\n", tapesize);
35846614Smckusick 	else
35946614Smckusick 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
36046794Smckusick 		    tapesize, fetapes);
3611423Sroot 
36210910Ssam 	alloctape();			/* Allocate tape buffer */
36310910Ssam 
364*50495Smckusick 	startnewtape(1);
3651423Sroot 	time(&(tstart_writing));
36646794Smckusick 	dumpmap(usedinomap, TS_CLRI, maxino);
3671423Sroot 
3681423Sroot 	msg("dumping (Pass III) [directories]\n");
36946794Smckusick 	for (map = dumpdirmap, ino = 0; ino < maxino; ) {
37046794Smckusick 		if ((ino % NBBY) == 0)
37147058Smckusick 			dirty = *map++;
37246794Smckusick 		else
37347058Smckusick 			dirty >>= 1;
37446794Smckusick 		ino++;
37547058Smckusick 		if ((dirty & 1) == 0)
37646794Smckusick 			continue;
37746794Smckusick 		/*
37846794Smckusick 		 * Skip directory inodes deleted and maybe reallocated
37946794Smckusick 		 */
38046794Smckusick 		dp = getino(ino);
38146794Smckusick 		if ((dp->di_mode & IFMT) != IFDIR)
38246794Smckusick 			continue;
38346794Smckusick 		dumpino(dp, ino);
38446794Smckusick 	}
3851423Sroot 
3861423Sroot 	msg("dumping (Pass IV) [regular files]\n");
38746794Smckusick 	for (map = dumpinomap, ino = 0; ino < maxino; ) {
38846794Smckusick 		if ((ino % NBBY) == 0)
38947058Smckusick 			dirty = *map++;
39046794Smckusick 		else
39147058Smckusick 			dirty >>= 1;
39246794Smckusick 		ino++;
39347058Smckusick 		if ((dirty & 1) == 0)
39446794Smckusick 			continue;
39546794Smckusick 		/*
39646794Smckusick 		 * Skip inodes deleted and reallocated as directories.
39746794Smckusick 		 */
39846794Smckusick 		dp = getino(ino);
39946794Smckusick 		if ((dp->di_mode & IFMT) == IFDIR)
40046794Smckusick 			continue;
40146794Smckusick 		dumpino(dp, ino);
40246794Smckusick 	}
4031423Sroot 
4041423Sroot 	spcl.c_type = TS_END;
40546794Smckusick 	for (i = 0; i < ntrec; i++)
40646794Smckusick 		writeheader(maxino);
40746614Smckusick 	if (pipeout)
40846614Smckusick 		msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
40946614Smckusick 	else
41046614Smckusick 		msg("DUMP: %ld tape blocks on %d volumes(s)\n",
41146614Smckusick 		    spcl.c_tapea, spcl.c_volume);
41246794Smckusick 	putdumptime();
41346239Storek 	trewind();
4141423Sroot 	broadcast("DUMP IS DONE!\7\7\n");
415*50495Smckusick 	msg("DUMP IS DONE\n");
4161423Sroot 	Exit(X_FINOK);
41746586Storek 	/* NOTREACHED */
4181423Sroot }
4191423Sroot 
42046586Storek void
4211423Sroot sigAbort()
4221423Sroot {
42346586Storek 	if (pipeout)
42446586Storek 		quit("Unknown signal, cannot recover\n");
4251423Sroot 	msg("Rewriting attempted as response to unknown signal.\n");
4261423Sroot 	fflush(stderr);
4271423Sroot 	fflush(stdout);
4281423Sroot 	close_rewind();
4291423Sroot 	exit(X_REWRITE);
4301423Sroot }
4311423Sroot 
43246586Storek void	sighup(){	msg("SIGHUP()  try rewriting\n"); sigAbort();}
43346586Storek void	sigtrap(){	msg("SIGTRAP()  try rewriting\n"); sigAbort();}
43446586Storek void	sigfpe(){	msg("SIGFPE()  try rewriting\n"); sigAbort();}
43546586Storek void	sigbus(){	msg("SIGBUS()  try rewriting\n"); sigAbort();}
43646586Storek void	sigsegv(){	msg("SIGSEGV()  ABORTING!\n"); abort();}
43746586Storek void	sigalrm(){	msg("SIGALRM()  try rewriting\n"); sigAbort();}
43846586Storek void	sigterm(){	msg("SIGTERM()  try rewriting\n"); sigAbort();}
43946586Storek 
44046586Storek char *
44146586Storek rawname(cp)
4421423Sroot 	char *cp;
4431423Sroot {
4441423Sroot 	static char rawbuf[32];
4454608Smckusic 	char *rindex();
4461423Sroot 	char *dp = rindex(cp, '/');
4471423Sroot 
4481423Sroot 	if (dp == 0)
4491423Sroot 		return (0);
4501423Sroot 	*dp = 0;
4511423Sroot 	strcpy(rawbuf, cp);
4521423Sroot 	*dp = '/';
4531423Sroot 	strcat(rawbuf, "/r");
4541423Sroot 	strcat(rawbuf, dp+1);
4551423Sroot 	return (rawbuf);
4561423Sroot }
457