Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.69.2.1
      1 /*	$NetBSD: main.c,v 1.69.2.1 2013/06/23 06:28:50 tls 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
     35  The Regents of the University of California.  All rights reserved.");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/1/95";
     41 #else
     42 __RCSID("$NetBSD: main.c,v 1.69.2.1 2013/06/23 06:28:50 tls Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/stat.h>
     49 #include <sys/mount.h>
     50 
     51 #include <ufs/ffs/fs.h>
     52 #include <ufs/ffs/ffs_extern.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <fcntl.h>
     58 #include <fstab.h>
     59 #include <signal.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <time.h>
     64 #include <unistd.h>
     65 #include <util.h>
     66 
     67 #include "dump.h"
     68 #include "pathnames.h"
     69 #include "snapshot.h"
     70 
     71 int	timestamp;		/* print message timestamps */
     72 int	notify;			/* notify operator flag */
     73 int	blockswritten;		/* number of blocks written on current tape */
     74 int	tapeno;			/* current tape number */
     75 int	density;		/* density in bytes/0.1" */
     76 int	ntrec = NTREC;		/* # tape blocks in each tape record */
     77 int	cartridge;		/* Assume non-cartridge tape */
     78 long	dev_bsize = 1;		/* recalculated below */
     79 long	blocksperfile;		/* output blocks per file */
     80 const char *host;		/* remote host (if any) */
     81 int	readcache = -1;		/* read cache size (in readblksize blks) */
     82 int	readblksize = 32 * 1024; /* read block size */
     83 char    default_time_string[] = "%T %Z"; /* default timestamp string */
     84 char    *time_string = default_time_string; /* timestamp string */
     85 
     86 static long numarg(const char *, long, long);
     87 static void obsolete(int *, char **[]);
     88 static void usage(void);
     89 
     90 int
     91 main(int argc, char *argv[])
     92 {
     93 	ino_t ino;
     94 	int dirty;
     95 	union dinode *dp;
     96 	struct fstab *dt;
     97 	struct statvfs *mntinfo, fsbuf;
     98 	char *map, *cp;
     99 	int ch;
    100 	int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1;
    101 	int snap_internal = 0;
    102 	ino_t maxino;
    103 	time_t tnow, date;
    104 	int dirc;
    105 	char *mountpoint;
    106 	int just_estimate = 0;
    107 	char labelstr[LBLSIZE];
    108 	char buf[MAXPATHLEN], rbuf[MAXPATHLEN];
    109 	char *new_time_format;
    110 	char *snap_backup = NULL;
    111 
    112 	spcl.c_date = 0;
    113 	(void)time(&tnow);
    114 	spcl.c_date = tnow;
    115 	tzset(); /* set up timezone for strftime */
    116 	if ((new_time_format = getenv("TIMEFORMAT")) != NULL)
    117 		time_string = new_time_format;
    118 
    119 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
    120 	if ((tape = getenv("TAPE")) == NULL)
    121 		tape = _PATH_DEFTAPE;
    122 	dumpdates = _PATH_DUMPDATES;
    123 	temp = _PATH_DTMP;
    124 	strcpy(labelstr, "none");	/* XXX safe strcpy. */
    125 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
    126 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
    127 	level = '0';
    128 	timestamp = 0;
    129 
    130 	if (argc < 2)
    131 		usage();
    132 
    133 	obsolete(&argc, &argv);
    134 	while ((ch = getopt(argc, argv,
    135 	    "0123456789aB:b:cd:eFf:h:ik:l:L:nr:s:StT:uWwx:X")) != -1)
    136 		switch (ch) {
    137 		/* dump level */
    138 		case '0': case '1': case '2': case '3': case '4':
    139 		case '5': case '6': case '7': case '8': case '9':
    140 			level = ch;
    141 			break;
    142 
    143 		case 'a':		/* `auto-size', Write to EOM. */
    144 			unlimited = 1;
    145 			break;
    146 
    147 		case 'B':		/* blocks per output file */
    148 			blocksperfile = numarg("blocks per file", 1L, 0L);
    149 			break;
    150 
    151 		case 'b':		/* blocks per tape write */
    152 			ntrec = numarg("blocks per write", 1L, 1000L);
    153 			bflag = 1;
    154 			break;
    155 
    156 		case 'c':		/* Tape is cart. not 9-track */
    157 			cartridge = 1;
    158 			break;
    159 
    160 		case 'd':		/* density, in bits per inch */
    161 			density = numarg("density", 10L, 327670L) / 10;
    162 			if (density >= 625 && !bflag)
    163 				ntrec = HIGHDENSITYTREC;
    164 			break;
    165 
    166 		case 'e':		/* eject full tapes */
    167 			eflag = 1;
    168 			break;
    169 
    170 		case 'F':		/* files-to-dump is an fs image */
    171 			Fflag = 1;
    172 			break;
    173 
    174 		case 'f':		/* output file */
    175 			tape = optarg;
    176 			break;
    177 
    178 		case 'h':
    179 			honorlevel = numarg("honor level", 0L, 10L);
    180 			break;
    181 
    182 		case 'i':	/* "true incremental" regardless level */
    183 			level = 'i';
    184 			trueinc = 1;
    185 			break;
    186 
    187 		case 'k':
    188 			readblksize = numarg("read block size", 0, 64) * 1024;
    189 			break;
    190 
    191 		case 'l':		/* autoload after eject full tapes */
    192 			eflag = 1;
    193 			lflag = numarg("timeout (in seconds)", 1, 0);
    194 			break;
    195 
    196 		case 'L':
    197 			/*
    198 			 * Note that although there are LBLSIZE characters,
    199 			 * the last must be '\0', so the limit on strlen()
    200 			 * is really LBLSIZE-1.
    201 			 */
    202 			if (strlcpy(labelstr, optarg, sizeof(labelstr))
    203 			    >= sizeof(labelstr)) {
    204 				msg(
    205 		"WARNING Label `%s' is larger than limit of %lu characters.\n",
    206 				    optarg,
    207 				    (unsigned long)sizeof(labelstr) - 1);
    208 				msg("WARNING: Using truncated label `%s'.\n",
    209 				    labelstr);
    210 			}
    211 			break;
    212 		case 'n':		/* notify operators */
    213 			notify = 1;
    214 			break;
    215 
    216 		case 'r':		/* read cache size */
    217 			readcache = numarg("read cache size", 0, 512);
    218 			break;
    219 
    220 		case 's':		/* tape size, feet */
    221 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
    222 			break;
    223 
    224 		case 'S':		/* exit after estimating # of tapes */
    225 			just_estimate = 1;
    226 			break;
    227 
    228 		case 't':
    229 			timestamp = 1;
    230 			break;
    231 
    232 		case 'T':		/* time of last dump */
    233 			spcl.c_ddate = unctime(optarg);
    234 			if (spcl.c_ddate < 0) {
    235 				(void)fprintf(stderr, "bad time \"%s\"\n",
    236 				    optarg);
    237 				exit(X_STARTUP);
    238 			}
    239 			Tflag = 1;
    240 			lastlevel = '?';
    241 			break;
    242 
    243 		case 'u':		/* update /etc/dumpdates */
    244 			uflag = 1;
    245 			break;
    246 
    247 		case 'W':		/* what to do */
    248 		case 'w':
    249 			lastdump(ch);
    250 			exit(X_FINOK);	/* do nothing else */
    251 
    252 		case 'x':
    253 			snap_backup = optarg;
    254 			break;
    255 
    256 		case 'X':
    257 			snap_internal = 1;
    258 			break;
    259 
    260 		default:
    261 			usage();
    262 		}
    263 	argc -= optind;
    264 	argv += optind;
    265 
    266 	if (argc < 1) {
    267 		(void)fprintf(stderr,
    268 		    "Must specify disk or image, or file list\n");
    269 		exit(X_STARTUP);
    270 	}
    271 
    272 
    273 	/*
    274 	 *	determine if disk is a subdirectory, and setup appropriately
    275 	 */
    276 	getfstab();		/* /etc/fstab snarfed */
    277 	disk = NULL;
    278 	disk_dev = NULL;
    279 	mountpoint = NULL;
    280 	dirc = 0;
    281 	for (i = 0; i < argc; i++) {
    282 		struct stat sb;
    283 
    284 		if (lstat(argv[i], &sb) == -1)
    285 			quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
    286 		if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
    287 			disk = argv[i];
    288  multicheck:
    289 			if (dirc != 0)
    290 				quit(
    291 	"Can't dump a disk or image at the same time as a file list\n");
    292 			break;
    293 		}
    294 		if ((dt = fstabsearch(argv[i])) != NULL) {
    295 			if (getfsspecname(buf, sizeof(buf), dt->fs_spec)
    296 			    == NULL)
    297 				quit("%s (%s)", buf, strerror(errno));
    298 			disk = buf;
    299 			mountpoint = xstrdup(dt->fs_file);
    300 			goto multicheck;
    301 		}
    302 		if (statvfs(argv[i], &fsbuf) == -1)
    303 			quit("Cannot statvfs %s: %s\n", argv[i],
    304 			    strerror(errno));
    305 		disk = fsbuf.f_mntfromname;
    306 		if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
    307 			goto multicheck;
    308 		if (mountpoint == NULL) {
    309 			mountpoint = xstrdup(fsbuf.f_mntonname);
    310 			if (uflag) {
    311 				msg("Ignoring u flag for subdir dump\n");
    312 				uflag = 0;
    313 			}
    314 			if (level > '0') {
    315 				msg("Subdir dump is done at level 0\n");
    316 				level = '0';
    317 			}
    318 			msg("Dumping sub files/directories from %s\n",
    319 			    mountpoint);
    320 		} else {
    321 			if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
    322 				quit("%s is not on %s\n", argv[i], mountpoint);
    323 		}
    324 		msg("Dumping file/directory %s\n", argv[i]);
    325 		dirc++;
    326 	}
    327 	if (mountpoint)
    328 		free(mountpoint);
    329 
    330 	if (dirc == 0) {
    331 		argv++;
    332 		if (argc != 1) {
    333 			(void)fprintf(stderr, "Excess arguments to dump:");
    334 			while (--argc)
    335 				(void)fprintf(stderr, " %s", *argv++);
    336 			(void)fprintf(stderr, "\n");
    337 			exit(X_STARTUP);
    338 		}
    339 	}
    340 	if (Tflag && uflag) {
    341 		(void)fprintf(stderr,
    342 		    "You cannot use the T and u flags together.\n");
    343 		exit(X_STARTUP);
    344 	}
    345 	if (strcmp(tape, "-") == 0) {
    346 		pipeout++;
    347 		tape = "standard output";
    348 	}
    349 
    350 	if (blocksperfile)
    351 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
    352 	else if (!unlimited) {
    353 		/*
    354 		 * Determine how to default tape size and density
    355 		 *
    356 		 *		density				tape size
    357 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
    358 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
    359 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
    360 		 *						(450*4 - slop)
    361 		 */
    362 		if (density == 0)
    363 			density = cartridge ? 100 : 160;
    364 		if (tsize == 0)
    365 			tsize = cartridge ? 1700L*120L : 2300L*120L;
    366 	}
    367 
    368 	if ((cp = strchr(tape, ':')) != NULL) {
    369 		host = tape;
    370 		/* This is fine, because all the const strings don't have : */
    371 		*cp++ = '\0';
    372 		tape = cp;
    373 #ifdef RDUMP
    374 		if (rmthost(host) == 0)
    375 			exit(X_STARTUP);
    376 #else
    377 		(void)fprintf(stderr, "remote dump not enabled\n");
    378 		exit(X_STARTUP);
    379 #endif
    380 	}
    381 
    382 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    383 		signal(SIGHUP, sig);
    384 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
    385 		signal(SIGTRAP, sig);
    386 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
    387 		signal(SIGFPE, sig);
    388 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
    389 		signal(SIGBUS, sig);
    390 #if 0
    391 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
    392 		signal(SIGSEGV, sig);
    393 #endif
    394 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    395 		signal(SIGTERM, sig);
    396 	if (signal(SIGINT, interrupt) == SIG_IGN)
    397 		signal(SIGINT, SIG_IGN);
    398 
    399 	/*
    400 	 *	disk can be either the full special file name, or
    401 	 *	the file system name.
    402 	 */
    403 	mountpoint = NULL;
    404 	mntinfo = mntinfosearch(disk);
    405 	if ((dt = fstabsearch(disk)) != NULL) {
    406 		if (getfsspecname(buf, sizeof(buf), dt->fs_spec) == NULL)
    407 			quit("%s (%s)", buf, strerror(errno));
    408 		if (getdiskrawname(rbuf, sizeof(rbuf), buf) == NULL)
    409 			quit("Can't get disk raw name for `%s' (%s)",
    410 			    buf, strerror(errno));
    411 		disk = rbuf;
    412 		mountpoint = dt->fs_file;
    413 		msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
    414 	} else if (mntinfo != NULL) {
    415 		if (getdiskrawname(rbuf, sizeof(rbuf), mntinfo->f_mntfromname)
    416 		    == NULL)
    417 			quit("Can't get disk raw name for `%s' (%s)",
    418 			    mntinfo->f_mntfromname, strerror(errno));
    419 		mountpoint = mntinfo->f_mntonname;
    420 		msg("Found %s on %s in mount table\n", disk, mountpoint);
    421 	}
    422 	if (mountpoint != NULL) {
    423 		if (dirc != 0)
    424 			(void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
    425 			    "a subset of %s", mountpoint);
    426 		else
    427 			(void)strlcpy(spcl.c_filesys, mountpoint,
    428 			    sizeof(spcl.c_filesys));
    429 	} else if (Fflag) {
    430 		(void)strlcpy(spcl.c_filesys, "a file system image",
    431 		    sizeof(spcl.c_filesys));
    432 	} else {
    433 		(void)strlcpy(spcl.c_filesys, "an unlisted file system",
    434 		    sizeof(spcl.c_filesys));
    435 	}
    436 	(void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
    437 	(void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label));
    438 	(void)gethostname(spcl.c_host, sizeof(spcl.c_host));
    439 	spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
    440 
    441 	if ((snap_backup != NULL || snap_internal) && mntinfo == NULL) {
    442 		msg("WARNING: Cannot use -x or -X on unmounted file system.\n");
    443 		snap_backup = NULL;
    444 		snap_internal = 0;
    445 	}
    446 
    447 #ifdef DUMP_LFS
    448 	sync();
    449 	if (snap_backup != NULL || snap_internal) {
    450 		if (lfs_wrap_stop(mountpoint) < 0) {
    451 			msg("Cannot stop writing on %s\n", mountpoint);
    452 			exit(X_STARTUP);
    453 		}
    454 	}
    455 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
    456 		msg("Cannot open %s\n", disk);
    457 		exit(X_STARTUP);
    458 	}
    459 	disk_dev = disk;
    460 #else /* ! DUMP_LFS */
    461 	if (snap_backup != NULL || snap_internal) {
    462 		diskfd = snap_open(mntinfo->f_mntonname, snap_backup,
    463 		    &tnow, &disk_dev);
    464 		if (diskfd < 0) {
    465 			msg("Cannot open snapshot of %s\n",
    466 				mntinfo->f_mntonname);
    467 			exit(X_STARTUP);
    468 		}
    469 		spcl.c_date = tnow;
    470 	} else {
    471 		if ((diskfd = open(disk, O_RDONLY)) < 0) {
    472 			msg("Cannot open %s\n", disk);
    473 			exit(X_STARTUP);
    474 		}
    475 		disk_dev = disk;
    476 	}
    477 	sync();
    478 #endif /* ! DUMP_LFS */
    479 
    480 	needswap = fs_read_sblock(sblock_buf);
    481 
    482 	/* true incremental is always a level 10 dump */
    483 	spcl.c_level = trueinc? iswap32(10): iswap32(level - '0');
    484 	spcl.c_type = iswap32(TS_TAPE);
    485 	spcl.c_date = iswap32(spcl.c_date);
    486 	spcl.c_ddate = iswap32(spcl.c_ddate);
    487 	if (!Tflag)
    488 		getdumptime();		/* /etc/dumpdates snarfed */
    489 
    490 	date = iswap32(spcl.c_date);
    491 	msg("Date of this level %c dump: %s", level,
    492 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    493 	date = iswap32(spcl.c_ddate);
    494  	msg("Date of last level %c dump: %s", lastlevel,
    495 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
    496 	msg("Dumping ");
    497 	if (snap_backup != NULL || snap_internal)
    498 		msgtail("a snapshot of ");
    499 	if (dirc != 0)
    500 		msgtail("a subset of ");
    501 	msgtail("%s (%s) ", disk, spcl.c_filesys);
    502 	if (host)
    503 		msgtail("to %s on host %s\n", tape, host);
    504 	else
    505 		msgtail("to %s\n", tape);
    506 	msg("Label: %s\n", labelstr);
    507 
    508 	ufsib = fs_parametrize();
    509 
    510 	dev_bshift = ffs(dev_bsize) - 1;
    511 	if (dev_bsize != (1 << dev_bshift))
    512 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
    513 	tp_bshift = ffs(TP_BSIZE) - 1;
    514 	if (TP_BSIZE != (1 << tp_bshift))
    515 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    516 	maxino = fs_maxino();
    517 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    518 	usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    519 	dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    520 	dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    521 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    522 
    523 	nonodump = iswap32(spcl.c_level) < honorlevel;
    524 
    525 	initcache(readcache, readblksize);
    526 
    527 	(void)signal(SIGINFO, statussig);
    528 
    529 	msg("mapping (Pass I) [regular files]\n");
    530 	anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
    531 	    (dirc ? argv : NULL));
    532 
    533 	msg("mapping (Pass II) [directories]\n");
    534 	while (anydirskipped) {
    535 		anydirskipped = mapdirs(maxino, &tapesize);
    536 	}
    537 
    538 	if (pipeout || unlimited) {
    539 		tapesize += 10;	/* 10 trailer blocks */
    540 		msg("estimated %llu tape blocks.\n",
    541 		    (unsigned long long)tapesize);
    542 	} else {
    543 		double fetapes;
    544 
    545 		if (blocksperfile)
    546 			fetapes = (double) tapesize / blocksperfile;
    547 		else if (cartridge) {
    548 			/* Estimate number of tapes, assuming streaming stops at
    549 			   the end of each block written, and not in mid-block.
    550 			   Assume no erroneous blocks; this can be compensated
    551 			   for with an artificially low tape size. */
    552 			fetapes =
    553 			(	  (double) tapesize	/* blocks */
    554 				* TP_BSIZE	/* bytes/block */
    555 				* (1.0/density)	/* 0.1" / byte */
    556 			  +
    557 				  (double) tapesize	/* blocks */
    558 				* (1.0/ntrec)	/* streaming-stops per block */
    559 				* 15.48		/* 0.1" / streaming-stop */
    560 			) * (1.0 / tsize );	/* tape / 0.1" */
    561 		} else {
    562 			/* Estimate number of tapes, for old fashioned 9-track
    563 			   tape */
    564 			int tenthsperirg = (density == 625) ? 3 : 7;
    565 			fetapes =
    566 			(	  tapesize	/* blocks */
    567 				* TP_BSIZE	/* bytes / block */
    568 				* (1.0/density)	/* 0.1" / byte */
    569 			  +
    570 				  tapesize	/* blocks */
    571 				* (1.0/ntrec)	/* IRG's / block */
    572 				* tenthsperirg	/* 0.1" / IRG */
    573 			) * (1.0 / tsize );	/* tape / 0.1" */
    574 		}
    575 		etapes = fetapes;		/* truncating assignment */
    576 		etapes++;
    577 		/* count the dumped inodes map on each additional tape */
    578 		tapesize += (etapes - 1) *
    579 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    580 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    581 		msg("estimated %llu tape blocks on %3.2f tape(s).\n",
    582 		    (unsigned long long)tapesize, fetapes);
    583 	}
    584 	/*
    585 	 * If the user only wants an estimate of the number of
    586 	 * tapes, exit now.
    587 	 */
    588 	if (just_estimate)
    589 		exit(X_FINOK);
    590 
    591 	/*
    592 	 * Allocate tape buffer.
    593 	 */
    594 	if (!alloctape())
    595 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    596 
    597 	startnewtape(1);
    598 	(void)time((time_t *)&(tstart_writing));
    599 	xferrate = 0;
    600 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    601 
    602 	msg("dumping (Pass III) [directories]\n");
    603 	dirty = 0;		/* XXX just to get gcc to shut up */
    604 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    605 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    606 			dirty = *map++;
    607 		else
    608 			dirty >>= 1;
    609 		if ((dirty & 1) == 0)
    610 			continue;
    611 		/*
    612 		 * Skip directory inodes deleted and maybe reallocated
    613 		 */
    614 		dp = getino(ino);
    615 		if ((DIP(dp, mode) & IFMT) != IFDIR)
    616 			continue;
    617 		(void)dumpino(dp, ino);
    618 	}
    619 
    620 	msg("dumping (Pass IV) [regular files]\n");
    621 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    622 		int mode;
    623 
    624 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    625 			dirty = *map++;
    626 		else
    627 			dirty >>= 1;
    628 		if ((dirty & 1) == 0)
    629 			continue;
    630 		/*
    631 		 * Skip inodes deleted and reallocated as directories.
    632 		 */
    633 		dp = getino(ino);
    634 		mode = DIP(dp, mode) & IFMT;
    635 		if (mode == IFDIR)
    636 			continue;
    637 		(void)dumpino(dp, ino);
    638 	}
    639 
    640 	spcl.c_type = iswap32(TS_END);
    641 	for (i = 0; i < ntrec; i++)
    642 		writeheader(maxino - 1);
    643 	if (pipeout)
    644 		msg("%d tape blocks\n",iswap32(spcl.c_tapea));
    645 	else
    646 		msg("%d tape blocks on %d volume%s\n",
    647 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
    648 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
    649 	tnow = do_stats();
    650 	date = iswap32(spcl.c_date);
    651 	msg("Date of this level %c dump: %s", level,
    652 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    653 	msg("Date this dump completed:  %s", ctime(&tnow));
    654 	msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
    655 	putdumptime();
    656 	trewind(0);
    657 	broadcast("DUMP IS DONE!\a\a\n");
    658 #ifdef DUMP_LFS
    659 	lfs_wrap_go();
    660 #endif /* DUMP_LFS */
    661 	msg("DUMP IS DONE\n");
    662 	Exit(X_FINOK);
    663 	/* NOTREACHED */
    664 	exit(X_FINOK);		/* XXX: to satisfy gcc */
    665 }
    666 
    667 static void
    668 usage(void)
    669 {
    670 	const char *prog = getprogname();
    671 
    672 	(void)fprintf(stderr,
    673 "usage: %s [-0123456789aceFinStuX] [-B records] [-b blocksize]\n"
    674 "            [-d density] [-f file] [-h level] [-k read-blocksize]\n"
    675 "            [-L label] [-l timeout] [-r cachesize] [-s feet]\n"
    676 "            [-T date] [-x snap-backup] files-to-dump\n"
    677 "       %s [-W | -w]\n", prog, prog);
    678 	exit(X_STARTUP);
    679 }
    680 
    681 /*
    682  * Pick up a numeric argument.  It must be nonnegative and in the given
    683  * range (except that a vmax of 0 means unlimited).
    684  */
    685 static long
    686 numarg(const char *meaning, long vmin, long vmax)
    687 {
    688 	char *p;
    689 	long val;
    690 
    691 	val = strtol(optarg, &p, 10);
    692 	if (*p)
    693 		errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
    694 	if (val < vmin || (vmax && val > vmax))
    695 		errx(X_STARTUP, "%s must be between %ld and %ld",
    696 		    meaning, vmin, vmax);
    697 	return (val);
    698 }
    699 
    700 void
    701 sig(int signo)
    702 {
    703 
    704 	switch(signo) {
    705 	case SIGALRM:
    706 	case SIGBUS:
    707 	case SIGFPE:
    708 	case SIGHUP:
    709 	case SIGTERM:
    710 	case SIGTRAP:
    711 		if (pipeout)
    712 			quit("Signal on pipe: cannot recover\n");
    713 		msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
    714 		(void)fflush(stderr);
    715 		(void)fflush(stdout);
    716 		close_rewind();
    717 		exit(X_REWRITE);
    718 		/* NOTREACHED */
    719 	case SIGSEGV:
    720 		msg("SIGSEGV: ABORTING!\n");
    721 		(void)signal(SIGSEGV, SIG_DFL);
    722 		(void)kill(0, SIGSEGV);
    723 		/* NOTREACHED */
    724 	}
    725 }
    726 
    727 /*
    728  * obsolete --
    729  *	Change set of key letters and ordered arguments into something
    730  *	getopt(3) will like.
    731  */
    732 static void
    733 obsolete(int *argcp, char **argvp[])
    734 {
    735 	int argc, flags;
    736 	char *ap, **argv, *flagsp, **nargv, *p;
    737 
    738 	/* Setup. */
    739 	argv = *argvp;
    740 	argc = *argcp;
    741 
    742 	/* Return if no arguments or first argument has leading dash. */
    743 	ap = argv[1];
    744 	if (argc == 1 || *ap == '-')
    745 		return;
    746 
    747 	/* Allocate space for new arguments. */
    748 	*argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
    749 	p = flagsp = xmalloc(strlen(ap) + 2);
    750 
    751 	*nargv++ = *argv;
    752 	argv += 2;
    753 
    754 	for (flags = 0; *ap; ++ap) {
    755 		switch (*ap) {
    756 		case 'B':
    757 		case 'b':
    758 		case 'd':
    759 		case 'f':
    760 		case 'h':
    761 		case 's':
    762 		case 'T':
    763 		case 'x':
    764 			if (*argv == NULL) {
    765 				warnx("option requires an argument -- %c", *ap);
    766 				usage();
    767 			}
    768 			nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
    769 			nargv[0][0] = '-';
    770 			nargv[0][1] = *ap;
    771 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
    772 			++argv;
    773 			++nargv;
    774 			break;
    775 		default:
    776 			if (!flags) {
    777 				*p++ = '-';
    778 				flags = 1;
    779 			}
    780 			*p++ = *ap;
    781 			break;
    782 		}
    783 	}
    784 
    785 	/* Terminate flags. */
    786 	if (flags) {
    787 		*p = '\0';
    788 		*nargv++ = flagsp;
    789 	} else
    790 		free(flagsp);
    791 
    792 	/* Copy remaining arguments. */
    793 	while ((*nargv++ = *argv++) != NULL)
    794 		;
    795 
    796 	/* Update argument count. */
    797 	*argcp = nargv - *argvp - 1;
    798 }
    799 
    800 
    801 void *
    802 xcalloc(size_t number, size_t size)
    803 {
    804 	void *p;
    805 
    806 	p = calloc(number, size);
    807 	if (p == NULL)
    808 		quit("%s\n", strerror(errno));
    809 	return (p);
    810 }
    811 
    812 void *
    813 xmalloc(size_t size)
    814 {
    815 	void *p;
    816 
    817 	p = malloc(size);
    818 	if (p == NULL)
    819 		quit("%s\n", strerror(errno));
    820 	return (p);
    821 }
    822 
    823 char *
    824 xstrdup(const char *str)
    825 {
    826 	char *p;
    827 
    828 	p = strdup(str);
    829 	if (p == NULL)
    830 		quit("%s\n", strerror(errno));
    831 	return (p);
    832 }
    833