Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.7
      1 /*	$NetBSD: main.c,v 1.7 1996/02/05 23:59:40 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 4/15/94";
     45 #else
     46 static char rcsid[] = "$NetBSD: main.c,v 1.7 1996/02/05 23:59:40 mrg Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #ifdef sunos
     53 #include <sys/vnode.h>
     54 
     55 #include <ufs/inode.h>
     56 #include <ufs/fs.h>
     57 #else
     58 #include <ufs/ffs/fs.h>
     59 #include <ufs/ufs/dinode.h>
     60 #endif
     61 
     62 #include <protocols/dumprestore.h>
     63 
     64 #include <ctype.h>
     65 #include <err.h>
     66 #include <errno.h>
     67 #include <fcntl.h>
     68 #include <fstab.h>
     69 #include <signal.h>
     70 #include <stdio.h>
     71 #include <stdlib.h>
     72 #include <string.h>
     73 #include <unistd.h>
     74 
     75 #include "dump.h"
     76 #include "pathnames.h"
     77 
     78 #ifndef SBOFF
     79 #define SBOFF (SBLOCK * DEV_BSIZE)
     80 #endif
     81 
     82 int	notify = 0;	/* notify operator flag */
     83 int	blockswritten = 0;	/* number of blocks written on current tape */
     84 int	tapeno = 0;	/* current tape number */
     85 int	density = 0;	/* density in bytes/0.1" */
     86 int	ntrec = NTREC;	/* # tape blocks in each tape record */
     87 int	cartridge = 0;	/* Assume non-cartridge tape */
     88 long	dev_bsize = 1;	/* recalculated below */
     89 long	blocksperfile;	/* output blocks per file */
     90 char	*host = NULL;	/* remote host (if any) */
     91 
     92 static long numarg __P((char *, long, long));
     93 static void obsolete __P((int *, char **[]));
     94 static void usage __P((void));
     95 
     96 int
     97 main(argc, argv)
     98 	int argc;
     99 	char *argv[];
    100 {
    101 	register ino_t ino;
    102 	register int dirty;
    103 	register struct dinode *dp;
    104 	register struct	fstab *dt;
    105 	register char *map;
    106 	register int ch;
    107 	int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
    108 	ino_t maxino;
    109 
    110 	spcl.c_date = 0;
    111 	(void)time((time_t *)&spcl.c_date);
    112 
    113 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
    114 	if ((tape = getenv("TAPE")) == NULL)
    115 		tape = _PATH_DEFTAPE;
    116 	dumpdates = _PATH_DUMPDATES;
    117 	temp = _PATH_DTMP;
    118 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
    119 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
    120 	level = '0';
    121 
    122 	if (argc < 2)
    123 		usage();
    124 
    125 	obsolete(&argc, &argv);
    126 	while ((ch = getopt(argc, argv, "0123456789B:b:cd:f:h:ns:T:uWw")) != -1)
    127 		switch (ch) {
    128 		/* dump level */
    129 		case '0': case '1': case '2': case '3': case '4':
    130 		case '5': case '6': case '7': case '8': case '9':
    131 			level = ch;
    132 			break;
    133 
    134 		case 'B':		/* blocks per output file */
    135 			blocksperfile = numarg("blocks per file", 1L, 0L);
    136 			break;
    137 
    138 		case 'b':		/* blocks per tape write */
    139 			ntrec = numarg("blocks per write", 1L, 1000L);
    140 			break;
    141 
    142 		case 'c':		/* Tape is cart. not 9-track */
    143 			cartridge = 1;
    144 			break;
    145 
    146 		case 'd':		/* density, in bits per inch */
    147 			density = numarg("density", 10L, 327670L) / 10;
    148 			if (density >= 625 && !bflag)
    149 				ntrec = HIGHDENSITYTREC;
    150 			break;
    151 
    152 		case 'f':		/* output file */
    153 			tape = optarg;
    154 			break;
    155 
    156 		case 'h':
    157 			honorlevel = numarg("honor level", 0L, 10L);
    158 			break;
    159 
    160 		case 'n':		/* notify operators */
    161 			notify = 1;
    162 			break;
    163 
    164 		case 's':		/* tape size, feet */
    165 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
    166 			break;
    167 
    168 		case 'T':		/* time of last dump */
    169 			spcl.c_ddate = unctime(optarg);
    170 			if (spcl.c_ddate < 0) {
    171 				(void)fprintf(stderr, "bad time \"%s\"\n",
    172 				    optarg);
    173 				exit(X_ABORT);
    174 			}
    175 			Tflag = 1;
    176 			lastlevel = '?';
    177 			break;
    178 
    179 		case 'u':		/* update /etc/dumpdates */
    180 			uflag = 1;
    181 			break;
    182 
    183 		case 'W':		/* what to do */
    184 		case 'w':
    185 			lastdump(ch);
    186 			exit(0);	/* do nothing else */
    187 
    188 		default:
    189 			usage();
    190 		}
    191 	argc -= optind;
    192 	argv += optind;
    193 
    194 	if (argc < 1) {
    195 		(void)fprintf(stderr, "Must specify disk or filesystem\n");
    196 		exit(X_ABORT);
    197 	}
    198 	disk = *argv++;
    199 	argc--;
    200 	if (argc >= 1) {
    201 		(void)fprintf(stderr, "Unknown arguments to dump:");
    202 		while (argc--)
    203 			(void)fprintf(stderr, " %s", *argv++);
    204 		(void)fprintf(stderr, "\n");
    205 		exit(X_ABORT);
    206 	}
    207 	if (Tflag && uflag) {
    208 	        (void)fprintf(stderr,
    209 		    "You cannot use the T and u flags together.\n");
    210 		exit(X_ABORT);
    211 	}
    212 	if (strcmp(tape, "-") == 0) {
    213 		pipeout++;
    214 		tape = "standard output";
    215 	}
    216 
    217 	if (blocksperfile)
    218 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
    219 	else {
    220 		/*
    221 		 * Determine how to default tape size and density
    222 		 *
    223 		 *         	density				tape size
    224 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
    225 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
    226 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
    227 		 *						(450*4 - slop)
    228 		 */
    229 		if (density == 0)
    230 			density = cartridge ? 100 : 160;
    231 		if (tsize == 0)
    232 			tsize = cartridge ? 1700L*120L : 2300L*120L;
    233 	}
    234 
    235 	if (strchr(tape, ':')) {
    236 		host = tape;
    237 		tape = strchr(host, ':');
    238 		*tape++ = '\0';
    239 #ifdef RDUMP
    240 		if (rmthost(host) == 0)
    241 			exit(X_ABORT);
    242 #else
    243 		(void)fprintf(stderr, "remote dump not enabled\n");
    244 		exit(X_ABORT);
    245 #endif
    246 	}
    247 	(void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
    248 
    249 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    250 		signal(SIGHUP, sig);
    251 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
    252 		signal(SIGTRAP, sig);
    253 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
    254 		signal(SIGFPE, sig);
    255 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
    256 		signal(SIGBUS, sig);
    257 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
    258 		signal(SIGSEGV, sig);
    259 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    260 		signal(SIGTERM, sig);
    261 	if (signal(SIGINT, interrupt) == SIG_IGN)
    262 		signal(SIGINT, SIG_IGN);
    263 
    264 	set_operators();	/* /etc/group snarfed */
    265 	getfstab();		/* /etc/fstab snarfed */
    266 	/*
    267 	 *	disk can be either the full special file name,
    268 	 *	the suffix of the special file name,
    269 	 *	the special name missing the leading '/',
    270 	 *	the file system name with or without the leading '/'.
    271 	 */
    272 	dt = fstabsearch(disk);
    273 	if (dt != NULL) {
    274 		disk = rawname(dt->fs_spec);
    275 		(void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
    276 		(void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
    277 	} else {
    278 		(void)strncpy(spcl.c_dev, disk, NAMELEN);
    279 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
    280 		    NAMELEN);
    281 	}
    282 	(void)strcpy(spcl.c_label, "none");
    283 	(void)gethostname(spcl.c_host, NAMELEN);
    284 	spcl.c_level = level - '0';
    285 	spcl.c_type = TS_TAPE;
    286 	if (!Tflag)
    287 	        getdumptime();		/* /etc/dumpdates snarfed */
    288 
    289 	msg("Date of this level %c dump: %s", level,
    290 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
    291  	msg("Date of last level %c dump: %s", lastlevel,
    292 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
    293 	msg("Dumping %s ", disk);
    294 	if (dt != NULL)
    295 		msgtail("(%s) ", dt->fs_file);
    296 	if (host)
    297 		msgtail("to %s on host %s\n", tape, host);
    298 	else
    299 		msgtail("to %s\n", tape);
    300 
    301 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
    302 		msg("Cannot open %s\n", disk);
    303 		exit(X_ABORT);
    304 	}
    305 	sync();
    306 	sblock = (struct fs *)sblock_buf;
    307 	bread(SBOFF, (char *) sblock, SBSIZE);
    308 	if (sblock->fs_magic != FS_MAGIC)
    309 		quit("bad sblock magic number\n");
    310 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
    311 	dev_bshift = ffs(dev_bsize) - 1;
    312 	if (dev_bsize != (1 << dev_bshift))
    313 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
    314 	tp_bshift = ffs(TP_BSIZE) - 1;
    315 	if (TP_BSIZE != (1 << tp_bshift))
    316 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    317 #ifdef FS_44INODEFMT
    318 	if (sblock->fs_inodefmt >= FS_44INODEFMT)
    319 		spcl.c_flags |= DR_NEWINODEFMT;
    320 #endif
    321 	maxino = sblock->fs_ipg * sblock->fs_ncg;
    322 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    323 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    324 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
    325 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
    326 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    327 
    328 	nonodump = spcl.c_level < honorlevel;
    329 
    330 	msg("mapping (Pass I) [regular files]\n");
    331 	anydirskipped = mapfiles(maxino, &tapesize);
    332 
    333 	msg("mapping (Pass II) [directories]\n");
    334 	while (anydirskipped) {
    335 		anydirskipped = mapdirs(maxino, &tapesize);
    336 	}
    337 
    338 	if (pipeout) {
    339 		tapesize += 10;	/* 10 trailer blocks */
    340 		msg("estimated %ld tape blocks.\n", tapesize);
    341 	} else {
    342 		double fetapes;
    343 
    344 		if (blocksperfile)
    345 			fetapes = (double) tapesize / blocksperfile;
    346 		else if (cartridge) {
    347 			/* Estimate number of tapes, assuming streaming stops at
    348 			   the end of each block written, and not in mid-block.
    349 			   Assume no erroneous blocks; this can be compensated
    350 			   for with an artificially low tape size. */
    351 			fetapes =
    352 			(	  tapesize	/* blocks */
    353 				* TP_BSIZE	/* bytes/block */
    354 				* (1.0/density)	/* 0.1" / byte */
    355 			  +
    356 				  tapesize	/* blocks */
    357 				* (1.0/ntrec)	/* streaming-stops per block */
    358 				* 15.48		/* 0.1" / streaming-stop */
    359 			) * (1.0 / tsize );	/* tape / 0.1" */
    360 		} else {
    361 			/* Estimate number of tapes, for old fashioned 9-track
    362 			   tape */
    363 			int tenthsperirg = (density == 625) ? 3 : 7;
    364 			fetapes =
    365 			(	  tapesize	/* blocks */
    366 				* TP_BSIZE	/* bytes / block */
    367 				* (1.0/density)	/* 0.1" / byte */
    368 			  +
    369 				  tapesize	/* blocks */
    370 				* (1.0/ntrec)	/* IRG's / block */
    371 				* tenthsperirg	/* 0.1" / IRG */
    372 			) * (1.0 / tsize );	/* tape / 0.1" */
    373 		}
    374 		etapes = fetapes;		/* truncating assignment */
    375 		etapes++;
    376 		/* count the dumped inodes map on each additional tape */
    377 		tapesize += (etapes - 1) *
    378 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    379 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    380 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
    381 		    tapesize, fetapes);
    382 	}
    383 
    384 	/*
    385 	 * Allocate tape buffer.
    386 	 */
    387 	if (!alloctape())
    388 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    389 
    390 	startnewtape(1);
    391 	(void)time((time_t *)&(tstart_writing));
    392 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    393 
    394 	msg("dumping (Pass III) [directories]\n");
    395 	dirty = 0;		/* XXX just to get gcc to shut up */
    396 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    397 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    398 			dirty = *map++;
    399 		else
    400 			dirty >>= 1;
    401 		if ((dirty & 1) == 0)
    402 			continue;
    403 		/*
    404 		 * Skip directory inodes deleted and maybe reallocated
    405 		 */
    406 		dp = getino(ino);
    407 		if ((dp->di_mode & IFMT) != IFDIR)
    408 			continue;
    409 		(void)dumpino(dp, ino);
    410 	}
    411 
    412 	msg("dumping (Pass IV) [regular files]\n");
    413 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    414 		int mode;
    415 
    416 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    417 			dirty = *map++;
    418 		else
    419 			dirty >>= 1;
    420 		if ((dirty & 1) == 0)
    421 			continue;
    422 		/*
    423 		 * Skip inodes deleted and reallocated as directories.
    424 		 */
    425 		dp = getino(ino);
    426 		mode = dp->di_mode & IFMT;
    427 		if (mode == IFDIR)
    428 			continue;
    429 		(void)dumpino(dp, ino);
    430 	}
    431 
    432 	spcl.c_type = TS_END;
    433 	for (i = 0; i < ntrec; i++)
    434 		writeheader(maxino - 1);
    435 	if (pipeout)
    436 		msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
    437 	else
    438 		msg("DUMP: %ld tape blocks on %d volumes(s)\n",
    439 		    spcl.c_tapea, spcl.c_volume);
    440 	putdumptime();
    441 	trewind();
    442 	broadcast("DUMP IS DONE!\7\7\n");
    443 	msg("DUMP IS DONE\n");
    444 	Exit(X_FINOK);
    445 	/* NOTREACHED */
    446 }
    447 
    448 static void
    449 usage()
    450 {
    451 
    452 	(void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n            [-h level] [-s feet] [-T date] filesystem\n");
    453 	(void)fprintf(stderr, "       dump [-W | -w]\n");
    454 	exit(1);
    455 }
    456 
    457 /*
    458  * Pick up a numeric argument.  It must be nonnegative and in the given
    459  * range (except that a vmax of 0 means unlimited).
    460  */
    461 static long
    462 numarg(meaning, vmin, vmax)
    463 	char *meaning;
    464 	long vmin, vmax;
    465 {
    466 	char *p;
    467 	long val;
    468 
    469 	val = strtol(optarg, &p, 10);
    470 	if (*p)
    471 		errx(1, "illegal %s -- %s", meaning, optarg);
    472 	if (val < vmin || (vmax && val > vmax))
    473 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
    474 	return (val);
    475 }
    476 
    477 void
    478 sig(signo)
    479 	int signo;
    480 {
    481 	switch(signo) {
    482 	case SIGALRM:
    483 	case SIGBUS:
    484 	case SIGFPE:
    485 	case SIGHUP:
    486 	case SIGTERM:
    487 	case SIGTRAP:
    488 		if (pipeout)
    489 			quit("Signal on pipe: cannot recover\n");
    490 		msg("Rewriting attempted as response to unknown signal.\n");
    491 		(void)fflush(stderr);
    492 		(void)fflush(stdout);
    493 		close_rewind();
    494 		exit(X_REWRITE);
    495 		/* NOTREACHED */
    496 	case SIGSEGV:
    497 		msg("SIGSEGV: ABORTING!\n");
    498 		(void)signal(SIGSEGV, SIG_DFL);
    499 		(void)kill(0, SIGSEGV);
    500 		/* NOTREACHED */
    501 	}
    502 }
    503 
    504 char *
    505 rawname(cp)
    506 	char *cp;
    507 {
    508 	static char rawbuf[MAXPATHLEN];
    509 	char *dp = strrchr(cp, '/');
    510 
    511 	if (dp == NULL)
    512 		return (NULL);
    513 	*dp = '\0';
    514 	(void)strcpy(rawbuf, cp);
    515 	*dp = '/';
    516 	(void)strcat(rawbuf, "/r");
    517 	(void)strcat(rawbuf, dp + 1);
    518 	return (rawbuf);
    519 }
    520 
    521 /*
    522  * obsolete --
    523  *	Change set of key letters and ordered arguments into something
    524  *	getopt(3) will like.
    525  */
    526 static void
    527 obsolete(argcp, argvp)
    528 	int *argcp;
    529 	char **argvp[];
    530 {
    531 	int argc, flags;
    532 	char *ap, **argv, *flagsp, **nargv, *p;
    533 
    534 	/* Setup. */
    535 	argv = *argvp;
    536 	argc = *argcp;
    537 
    538 	/* Return if no arguments or first argument has leading dash. */
    539 	ap = argv[1];
    540 	if (argc == 1 || *ap == '-')
    541 		return;
    542 
    543 	/* Allocate space for new arguments. */
    544 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
    545 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
    546 		err(1, NULL);
    547 
    548 	*nargv++ = *argv;
    549 	argv += 2;
    550 
    551 	for (flags = 0; *ap; ++ap) {
    552 		switch (*ap) {
    553 		case 'B':
    554 		case 'b':
    555 		case 'd':
    556 		case 'f':
    557 		case 'h':
    558 		case 's':
    559 		case 'T':
    560 			if (*argv == NULL) {
    561 				warnx("option requires an argument -- %c", *ap);
    562 				usage();
    563 			}
    564 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
    565 				err(1, NULL);
    566 			nargv[0][0] = '-';
    567 			nargv[0][1] = *ap;
    568 			(void)strcpy(&nargv[0][2], *argv);
    569 			++argv;
    570 			++nargv;
    571 			break;
    572 		default:
    573 			if (!flags) {
    574 				*p++ = '-';
    575 				flags = 1;
    576 			}
    577 			*p++ = *ap;
    578 			break;
    579 		}
    580 	}
    581 
    582 	/* Terminate flags. */
    583 	if (flags) {
    584 		*p = '\0';
    585 		*nargv++ = flagsp;
    586 	}
    587 
    588 	/* Copy remaining arguments. */
    589 	while (*nargv++ = *argv++);
    590 
    591 	/* Update argument count. */
    592 	*argcp = nargv - *argvp - 1;
    593 }
    594