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