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