xref: /netbsd-src/sbin/dump/main.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: main.c,v 1.71 2013/09/08 13:26:05 mlelstv 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.71 2013/09/08 13:26:05 mlelstv 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 		int error;
284 
285 		error = lstat(argv[i], &sb);
286 		if (Fflag || (!error && (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)))) {
287 			if (error)
288 				quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
289 			disk = argv[i];
290  multicheck:
291 			if (dirc != 0)
292 				quit("Can't dump a disk or image at the same time as a file list\n");
293 			break;
294 		}
295 		if ((dt = fstabsearch(argv[i])) != NULL) {
296 			disk = argv[i];
297 			mountpoint = xstrdup(dt->fs_file);
298 			goto multicheck;
299 		}
300 		if (statvfs(argv[i], &fsbuf) == -1)
301 			quit("Cannot statvfs %s: %s\n", argv[i],
302 			    strerror(errno));
303 		disk = fsbuf.f_mntfromname;
304 		if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
305 			goto multicheck;
306 		if (mountpoint == NULL) {
307 			mountpoint = xstrdup(fsbuf.f_mntonname);
308 			if (uflag) {
309 				msg("Ignoring u flag for subdir dump\n");
310 				uflag = 0;
311 			}
312 			if (level > '0') {
313 				msg("Subdir dump is done at level 0\n");
314 				level = '0';
315 			}
316 			msg("Dumping sub files/directories from %s\n",
317 			    mountpoint);
318 		} else {
319 			if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
320 				quit("%s is not on %s\n", argv[i], mountpoint);
321 		}
322 		msg("Dumping file/directory %s\n", argv[i]);
323 		dirc++;
324 	}
325 	if (mountpoint)
326 		free(mountpoint);
327 
328 	if (dirc == 0) {
329 		argv++;
330 		if (argc != 1) {
331 			(void)fprintf(stderr, "Excess arguments to dump:");
332 			while (--argc)
333 				(void)fprintf(stderr, " %s", *argv++);
334 			(void)fprintf(stderr, "\n");
335 			exit(X_STARTUP);
336 		}
337 	}
338 	if (Tflag && uflag) {
339 		(void)fprintf(stderr,
340 		    "You cannot use the T and u flags together.\n");
341 		exit(X_STARTUP);
342 	}
343 	if (strcmp(tape, "-") == 0) {
344 		pipeout++;
345 		tape = "standard output";
346 	}
347 
348 	if (blocksperfile)
349 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
350 	else if (!unlimited) {
351 		/*
352 		 * Determine how to default tape size and density
353 		 *
354 		 *		density				tape size
355 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
356 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
357 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
358 		 *						(450*4 - slop)
359 		 */
360 		if (density == 0)
361 			density = cartridge ? 100 : 160;
362 		if (tsize == 0)
363 			tsize = cartridge ? 1700L*120L : 2300L*120L;
364 	}
365 
366 	if ((cp = strchr(tape, ':')) != NULL) {
367 		host = tape;
368 		/* This is fine, because all the const strings don't have : */
369 		*cp++ = '\0';
370 		tape = cp;
371 #ifdef RDUMP
372 		if (rmthost(host) == 0)
373 			exit(X_STARTUP);
374 #else
375 		(void)fprintf(stderr, "remote dump not enabled\n");
376 		exit(X_STARTUP);
377 #endif
378 	}
379 
380 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
381 		signal(SIGHUP, sig);
382 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
383 		signal(SIGTRAP, sig);
384 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
385 		signal(SIGFPE, sig);
386 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
387 		signal(SIGBUS, sig);
388 #if 0
389 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
390 		signal(SIGSEGV, sig);
391 #endif
392 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
393 		signal(SIGTERM, sig);
394 	if (signal(SIGINT, interrupt) == SIG_IGN)
395 		signal(SIGINT, SIG_IGN);
396 
397 	/*
398 	 *	disk can be either the full special file name, or
399 	 *	the file system name.
400 	 */
401 	mountpoint = NULL;
402 	mntinfo = mntinfosearch(disk);
403 	if ((dt = fstabsearch(disk)) != NULL) {
404 		if (getfsspecname(buf, sizeof(buf), dt->fs_spec) == NULL)
405 			quit("%s (%s)", buf, strerror(errno));
406 		if (getdiskrawname(rbuf, sizeof(rbuf), buf) == NULL)
407 			quit("Can't get disk raw name for `%s' (%s)",
408 			    buf, strerror(errno));
409 		disk = rbuf;
410 		mountpoint = dt->fs_file;
411 		msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
412 	} else if (mntinfo != NULL) {
413 		if (getdiskrawname(rbuf, sizeof(rbuf), mntinfo->f_mntfromname)
414 		    == NULL)
415 			quit("Can't get disk raw name for `%s' (%s)",
416 			    mntinfo->f_mntfromname, strerror(errno));
417 		disk = rbuf;
418 		mountpoint = mntinfo->f_mntonname;
419 		msg("Found %s on %s in mount table\n", disk, mountpoint);
420 	}
421 	if (mountpoint != NULL) {
422 		if (dirc != 0)
423 			(void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
424 			    "a subset of %s", mountpoint);
425 		else
426 			(void)strlcpy(spcl.c_filesys, mountpoint,
427 			    sizeof(spcl.c_filesys));
428 	} else if (Fflag) {
429 		(void)strlcpy(spcl.c_filesys, "a file system image",
430 		    sizeof(spcl.c_filesys));
431 	} else {
432 		(void)strlcpy(spcl.c_filesys, "an unlisted file system",
433 		    sizeof(spcl.c_filesys));
434 	}
435 	(void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
436 	(void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label));
437 	(void)gethostname(spcl.c_host, sizeof(spcl.c_host));
438 	spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
439 
440 	if ((snap_backup != NULL || snap_internal) && mntinfo == NULL) {
441 		msg("WARNING: Cannot use -x or -X on unmounted file system.\n");
442 		snap_backup = NULL;
443 		snap_internal = 0;
444 	}
445 
446 #ifdef DUMP_LFS
447 	sync();
448 	if (snap_backup != NULL || snap_internal) {
449 		if (lfs_wrap_stop(mountpoint) < 0) {
450 			msg("Cannot stop writing on %s\n", mountpoint);
451 			exit(X_STARTUP);
452 		}
453 	}
454 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
455 		msg("Cannot open %s\n", disk);
456 		exit(X_STARTUP);
457 	}
458 	disk_dev = disk;
459 #else /* ! DUMP_LFS */
460 	if (snap_backup != NULL || snap_internal) {
461 		diskfd = snap_open(mntinfo->f_mntonname, snap_backup,
462 		    &tnow, &disk_dev);
463 		if (diskfd < 0) {
464 			msg("Cannot open snapshot of %s\n",
465 				mntinfo->f_mntonname);
466 			exit(X_STARTUP);
467 		}
468 		spcl.c_date = tnow;
469 	} else {
470 		if ((diskfd = open(disk, O_RDONLY)) < 0) {
471 			msg("Cannot open %s\n", disk);
472 			exit(X_STARTUP);
473 		}
474 		disk_dev = disk;
475 	}
476 	sync();
477 #endif /* ! DUMP_LFS */
478 
479 	needswap = fs_read_sblock(sblock_buf);
480 
481 	/* true incremental is always a level 10 dump */
482 	spcl.c_level = trueinc? iswap32(10): iswap32(level - '0');
483 	spcl.c_type = iswap32(TS_TAPE);
484 	spcl.c_date = iswap32(spcl.c_date);
485 	spcl.c_ddate = iswap32(spcl.c_ddate);
486 	if (!Tflag)
487 		getdumptime();		/* /etc/dumpdates snarfed */
488 
489 	date = iswap32(spcl.c_date);
490 	msg("Date of this level %c dump: %s", level,
491 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
492 	date = iswap32(spcl.c_ddate);
493  	msg("Date of last level %c dump: %s", lastlevel,
494 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
495 	msg("Dumping ");
496 	if (snap_backup != NULL || snap_internal)
497 		msgtail("a snapshot of ");
498 	if (dirc != 0)
499 		msgtail("a subset of ");
500 	msgtail("%s (%s) ", disk, spcl.c_filesys);
501 	if (host)
502 		msgtail("to %s on host %s\n", tape, host);
503 	else
504 		msgtail("to %s\n", tape);
505 	msg("Label: %s\n", labelstr);
506 
507 	ufsib = fs_parametrize();
508 
509 	dev_bshift = ffs(dev_bsize) - 1;
510 	if (dev_bsize != (1 << dev_bshift))
511 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
512 	tp_bshift = ffs(TP_BSIZE) - 1;
513 	if (TP_BSIZE != (1 << tp_bshift))
514 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
515 	maxino = fs_maxino();
516 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
517 	usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
518 	dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
519 	dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
520 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
521 
522 	nonodump = iswap32(spcl.c_level) < honorlevel;
523 
524 	initcache(readcache, readblksize);
525 
526 	(void)signal(SIGINFO, statussig);
527 
528 	msg("mapping (Pass I) [regular files]\n");
529 	anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
530 	    (dirc ? argv : NULL));
531 
532 	msg("mapping (Pass II) [directories]\n");
533 	while (anydirskipped) {
534 		anydirskipped = mapdirs(maxino, &tapesize);
535 	}
536 
537 	if (pipeout || unlimited) {
538 		tapesize += 10;	/* 10 trailer blocks */
539 		msg("estimated %llu tape blocks.\n",
540 		    (unsigned long long)tapesize);
541 	} else {
542 		double fetapes;
543 
544 		if (blocksperfile)
545 			fetapes = (double) tapesize / blocksperfile;
546 		else if (cartridge) {
547 			/* Estimate number of tapes, assuming streaming stops at
548 			   the end of each block written, and not in mid-block.
549 			   Assume no erroneous blocks; this can be compensated
550 			   for with an artificially low tape size. */
551 			fetapes =
552 			(	  (double) tapesize	/* blocks */
553 				* TP_BSIZE	/* bytes/block */
554 				* (1.0/density)	/* 0.1" / byte */
555 			  +
556 				  (double) tapesize	/* blocks */
557 				* (1.0/ntrec)	/* streaming-stops per block */
558 				* 15.48		/* 0.1" / streaming-stop */
559 			) * (1.0 / tsize );	/* tape / 0.1" */
560 		} else {
561 			/* Estimate number of tapes, for old fashioned 9-track
562 			   tape */
563 			int tenthsperirg = (density == 625) ? 3 : 7;
564 			fetapes =
565 			(	  tapesize	/* blocks */
566 				* TP_BSIZE	/* bytes / block */
567 				* (1.0/density)	/* 0.1" / byte */
568 			  +
569 				  tapesize	/* blocks */
570 				* (1.0/ntrec)	/* IRG's / block */
571 				* tenthsperirg	/* 0.1" / IRG */
572 			) * (1.0 / tsize );	/* tape / 0.1" */
573 		}
574 		etapes = fetapes;		/* truncating assignment */
575 		etapes++;
576 		/* count the dumped inodes map on each additional tape */
577 		tapesize += (etapes - 1) *
578 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
579 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
580 		msg("estimated %llu tape blocks on %3.2f tape(s).\n",
581 		    (unsigned long long)tapesize, fetapes);
582 	}
583 	/*
584 	 * If the user only wants an estimate of the number of
585 	 * tapes, exit now.
586 	 */
587 	if (just_estimate)
588 		exit(X_FINOK);
589 
590 	/*
591 	 * Allocate tape buffer.
592 	 */
593 	if (!alloctape())
594 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
595 
596 	startnewtape(1);
597 	(void)time((time_t *)&(tstart_writing));
598 	xferrate = 0;
599 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
600 
601 	msg("dumping (Pass III) [directories]\n");
602 	dirty = 0;		/* XXX just to get gcc to shut up */
603 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
604 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
605 			dirty = *map++;
606 		else
607 			dirty >>= 1;
608 		if ((dirty & 1) == 0)
609 			continue;
610 		/*
611 		 * Skip directory inodes deleted and maybe reallocated
612 		 */
613 		dp = getino(ino);
614 		if ((DIP(dp, mode) & IFMT) != IFDIR)
615 			continue;
616 		(void)dumpino(dp, ino);
617 	}
618 
619 	msg("dumping (Pass IV) [regular files]\n");
620 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
621 		int mode;
622 
623 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
624 			dirty = *map++;
625 		else
626 			dirty >>= 1;
627 		if ((dirty & 1) == 0)
628 			continue;
629 		/*
630 		 * Skip inodes deleted and reallocated as directories.
631 		 */
632 		dp = getino(ino);
633 		mode = DIP(dp, mode) & IFMT;
634 		if (mode == IFDIR)
635 			continue;
636 		(void)dumpino(dp, ino);
637 	}
638 
639 	spcl.c_type = iswap32(TS_END);
640 	for (i = 0; i < ntrec; i++)
641 		writeheader(maxino - 1);
642 	if (pipeout)
643 		msg("%d tape blocks\n",iswap32(spcl.c_tapea));
644 	else
645 		msg("%d tape blocks on %d volume%s\n",
646 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
647 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
648 	tnow = do_stats();
649 	date = iswap32(spcl.c_date);
650 	msg("Date of this level %c dump: %s", level,
651 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
652 	msg("Date this dump completed:  %s", ctime(&tnow));
653 	msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
654 	putdumptime();
655 	trewind(0);
656 	broadcast("DUMP IS DONE!\a\a\n");
657 #ifdef DUMP_LFS
658 	lfs_wrap_go();
659 #endif /* DUMP_LFS */
660 	msg("DUMP IS DONE\n");
661 	Exit(X_FINOK);
662 	/* NOTREACHED */
663 	exit(X_FINOK);		/* XXX: to satisfy gcc */
664 }
665 
666 static void
667 usage(void)
668 {
669 	const char *prog = getprogname();
670 
671 	(void)fprintf(stderr,
672 "usage: %s [-0123456789aceFinStuX] [-B records] [-b blocksize]\n"
673 "            [-d density] [-f file] [-h level] [-k read-blocksize]\n"
674 "            [-L label] [-l timeout] [-r cachesize] [-s feet]\n"
675 "            [-T date] [-x snap-backup] files-to-dump\n"
676 "       %s [-W | -w]\n", prog, prog);
677 	exit(X_STARTUP);
678 }
679 
680 /*
681  * Pick up a numeric argument.  It must be nonnegative and in the given
682  * range (except that a vmax of 0 means unlimited).
683  */
684 static long
685 numarg(const char *meaning, long vmin, long vmax)
686 {
687 	char *p;
688 	long val;
689 
690 	val = strtol(optarg, &p, 10);
691 	if (*p)
692 		errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
693 	if (val < vmin || (vmax && val > vmax))
694 		errx(X_STARTUP, "%s must be between %ld and %ld",
695 		    meaning, vmin, vmax);
696 	return (val);
697 }
698 
699 void
700 sig(int signo)
701 {
702 
703 	switch(signo) {
704 	case SIGALRM:
705 	case SIGBUS:
706 	case SIGFPE:
707 	case SIGHUP:
708 	case SIGTERM:
709 	case SIGTRAP:
710 		if (pipeout)
711 			quit("Signal on pipe: cannot recover\n");
712 		msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
713 		(void)fflush(stderr);
714 		(void)fflush(stdout);
715 		close_rewind();
716 		exit(X_REWRITE);
717 		/* NOTREACHED */
718 	case SIGSEGV:
719 		msg("SIGSEGV: ABORTING!\n");
720 		(void)signal(SIGSEGV, SIG_DFL);
721 		(void)kill(0, SIGSEGV);
722 		/* NOTREACHED */
723 	}
724 }
725 
726 /*
727  * obsolete --
728  *	Change set of key letters and ordered arguments into something
729  *	getopt(3) will like.
730  */
731 static void
732 obsolete(int *argcp, char **argvp[])
733 {
734 	int argc, flags;
735 	char *ap, **argv, *flagsp, **nargv, *p;
736 
737 	/* Setup. */
738 	argv = *argvp;
739 	argc = *argcp;
740 
741 	/* Return if no arguments or first argument has leading dash. */
742 	ap = argv[1];
743 	if (argc == 1 || *ap == '-')
744 		return;
745 
746 	/* Allocate space for new arguments. */
747 	*argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
748 	p = flagsp = xmalloc(strlen(ap) + 2);
749 
750 	*nargv++ = *argv;
751 	argv += 2;
752 
753 	for (flags = 0; *ap; ++ap) {
754 		switch (*ap) {
755 		case 'B':
756 		case 'b':
757 		case 'd':
758 		case 'f':
759 		case 'h':
760 		case 's':
761 		case 'T':
762 		case 'x':
763 			if (*argv == NULL) {
764 				warnx("option requires an argument -- %c", *ap);
765 				usage();
766 			}
767 			nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
768 			nargv[0][0] = '-';
769 			nargv[0][1] = *ap;
770 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
771 			++argv;
772 			++nargv;
773 			break;
774 		default:
775 			if (!flags) {
776 				*p++ = '-';
777 				flags = 1;
778 			}
779 			*p++ = *ap;
780 			break;
781 		}
782 	}
783 
784 	/* Terminate flags. */
785 	if (flags) {
786 		*p = '\0';
787 		*nargv++ = flagsp;
788 	} else
789 		free(flagsp);
790 
791 	/* Copy remaining arguments. */
792 	while ((*nargv++ = *argv++) != NULL)
793 		;
794 
795 	/* Update argument count. */
796 	*argcp = nargv - *argvp - 1;
797 }
798 
799 
800 void *
801 xcalloc(size_t number, size_t size)
802 {
803 	void *p;
804 
805 	p = calloc(number, size);
806 	if (p == NULL)
807 		quit("%s\n", strerror(errno));
808 	return (p);
809 }
810 
811 void *
812 xmalloc(size_t size)
813 {
814 	void *p;
815 
816 	p = malloc(size);
817 	if (p == NULL)
818 		quit("%s\n", strerror(errno));
819 	return (p);
820 }
821 
822 char *
823 xstrdup(const char *str)
824 {
825 	char *p;
826 
827 	p = strdup(str);
828 	if (p == NULL)
829 		quit("%s\n", strerror(errno));
830 	return (p);
831 }
832