xref: /netbsd-src/sbin/dump/main.c (revision 4e6df137e8e14049b5a701d249962c480449c141)
1 /*	$NetBSD: main.c,v 1.65 2010/02/26 02:11:40 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.65 2010/02/26 02:11:40 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 9 */
185 			level = '9';
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 	spcl.c_level = iswap32(level - '0');
474 	spcl.c_type = iswap32(TS_TAPE);
475 	spcl.c_date = iswap32(spcl.c_date);
476 	spcl.c_ddate = iswap32(spcl.c_ddate);
477 	if (!Tflag)
478 		getdumptime();		/* /etc/dumpdates snarfed */
479 
480 	date = iswap32(spcl.c_date);
481 	msg("Date of this level %c dump: %s", level,
482 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
483 	date = iswap32(spcl.c_ddate);
484  	msg("Date of last level %c dump: %s", lastlevel,
485 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
486 	msg("Dumping ");
487 	if (snap_backup != NULL || snap_internal)
488 		msgtail("a snapshot of ");
489 	if (dirc != 0)
490 		msgtail("a subset of ");
491 	msgtail("%s (%s) ", disk, spcl.c_filesys);
492 	if (host)
493 		msgtail("to %s on host %s\n", tape, host);
494 	else
495 		msgtail("to %s\n", tape);
496 	msg("Label: %s\n", labelstr);
497 
498 	ufsib = fs_parametrize();
499 
500 	dev_bshift = ffs(dev_bsize) - 1;
501 	if (dev_bsize != (1 << dev_bshift))
502 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
503 	tp_bshift = ffs(TP_BSIZE) - 1;
504 	if (TP_BSIZE != (1 << tp_bshift))
505 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
506 	maxino = fs_maxino();
507 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
508 	usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
509 	dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
510 	dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
511 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
512 
513 	nonodump = iswap32(spcl.c_level) < honorlevel;
514 
515 	initcache(readcache, readblksize);
516 
517 	(void)signal(SIGINFO, statussig);
518 
519 	msg("mapping (Pass I) [regular files]\n");
520 	anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
521 	    (dirc ? argv : NULL));
522 
523 	msg("mapping (Pass II) [directories]\n");
524 	while (anydirskipped) {
525 		anydirskipped = mapdirs(maxino, &tapesize);
526 	}
527 
528 	if (pipeout || unlimited) {
529 		tapesize += 10;	/* 10 trailer blocks */
530 		msg("estimated %llu tape blocks.\n",
531 		    (unsigned long long)tapesize);
532 	} else {
533 		double fetapes;
534 
535 		if (blocksperfile)
536 			fetapes = (double) tapesize / blocksperfile;
537 		else if (cartridge) {
538 			/* Estimate number of tapes, assuming streaming stops at
539 			   the end of each block written, and not in mid-block.
540 			   Assume no erroneous blocks; this can be compensated
541 			   for with an artificially low tape size. */
542 			fetapes =
543 			(	  (double) tapesize	/* blocks */
544 				* TP_BSIZE	/* bytes/block */
545 				* (1.0/density)	/* 0.1" / byte */
546 			  +
547 				  (double) tapesize	/* blocks */
548 				* (1.0/ntrec)	/* streaming-stops per block */
549 				* 15.48		/* 0.1" / streaming-stop */
550 			) * (1.0 / tsize );	/* tape / 0.1" */
551 		} else {
552 			/* Estimate number of tapes, for old fashioned 9-track
553 			   tape */
554 			int tenthsperirg = (density == 625) ? 3 : 7;
555 			fetapes =
556 			(	  tapesize	/* blocks */
557 				* TP_BSIZE	/* bytes / block */
558 				* (1.0/density)	/* 0.1" / byte */
559 			  +
560 				  tapesize	/* blocks */
561 				* (1.0/ntrec)	/* IRG's / block */
562 				* tenthsperirg	/* 0.1" / IRG */
563 			) * (1.0 / tsize );	/* tape / 0.1" */
564 		}
565 		etapes = fetapes;		/* truncating assignment */
566 		etapes++;
567 		/* count the dumped inodes map on each additional tape */
568 		tapesize += (etapes - 1) *
569 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
570 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
571 		msg("estimated %llu tape blocks on %3.2f tape(s).\n",
572 		    (unsigned long long)tapesize, fetapes);
573 	}
574 	/*
575 	 * If the user only wants an estimate of the number of
576 	 * tapes, exit now.
577 	 */
578 	if (just_estimate)
579 		exit(X_FINOK);
580 
581 	/*
582 	 * Allocate tape buffer.
583 	 */
584 	if (!alloctape())
585 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
586 
587 	startnewtape(1);
588 	(void)time((time_t *)&(tstart_writing));
589 	xferrate = 0;
590 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
591 
592 	msg("dumping (Pass III) [directories]\n");
593 	dirty = 0;		/* XXX just to get gcc to shut up */
594 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
595 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
596 			dirty = *map++;
597 		else
598 			dirty >>= 1;
599 		if ((dirty & 1) == 0)
600 			continue;
601 		/*
602 		 * Skip directory inodes deleted and maybe reallocated
603 		 */
604 		dp = getino(ino);
605 		if ((DIP(dp, mode) & IFMT) != IFDIR)
606 			continue;
607 		(void)dumpino(dp, ino);
608 	}
609 
610 	msg("dumping (Pass IV) [regular files]\n");
611 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
612 		int mode;
613 
614 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
615 			dirty = *map++;
616 		else
617 			dirty >>= 1;
618 		if ((dirty & 1) == 0)
619 			continue;
620 		/*
621 		 * Skip inodes deleted and reallocated as directories.
622 		 */
623 		dp = getino(ino);
624 		mode = DIP(dp, mode) & IFMT;
625 		if (mode == IFDIR)
626 			continue;
627 		(void)dumpino(dp, ino);
628 	}
629 
630 	spcl.c_type = iswap32(TS_END);
631 	for (i = 0; i < ntrec; i++)
632 		writeheader(maxino - 1);
633 	if (pipeout)
634 		msg("%d tape blocks\n",iswap32(spcl.c_tapea));
635 	else
636 		msg("%d tape blocks on %d volume%s\n",
637 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
638 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
639 	tnow = do_stats();
640 	date = iswap32(spcl.c_date);
641 	msg("Date of this level %c dump: %s", level,
642 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
643 	msg("Date this dump completed:  %s", ctime(&tnow));
644 	msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
645 	putdumptime();
646 	trewind(0);
647 	broadcast("DUMP IS DONE!\a\a\n");
648 #ifdef DUMP_LFS
649 	lfs_wrap_go();
650 #endif /* DUMP_LFS */
651 	msg("DUMP IS DONE\n");
652 	Exit(X_FINOK);
653 	/* NOTREACHED */
654 	exit(X_FINOK);		/* XXX: to satisfy gcc */
655 }
656 
657 static void
658 usage(void)
659 {
660 	const char *prog = getprogname();
661 
662 	(void)fprintf(stderr,
663 "usage: %s [-0123456789aceFnStuX] [-B records] [-b blocksize]\n"
664 "            [-d density] [-f file] [-h level] [-k read-blocksize]\n"
665 "            [-L label] [-l timeout] [-r read-cache] [-s feet]\n"
666 "            [-T date] [-x snap-backup] files-to-dump\n"
667 "       %s [-W | -w]\n", prog, prog);
668 	exit(X_STARTUP);
669 }
670 
671 /*
672  * Pick up a numeric argument.  It must be nonnegative and in the given
673  * range (except that a vmax of 0 means unlimited).
674  */
675 static long
676 numarg(const char *meaning, long vmin, long vmax)
677 {
678 	char *p;
679 	long val;
680 
681 	val = strtol(optarg, &p, 10);
682 	if (*p)
683 		errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
684 	if (val < vmin || (vmax && val > vmax))
685 		errx(X_STARTUP, "%s must be between %ld and %ld",
686 		    meaning, vmin, vmax);
687 	return (val);
688 }
689 
690 void
691 sig(int signo)
692 {
693 
694 	switch(signo) {
695 	case SIGALRM:
696 	case SIGBUS:
697 	case SIGFPE:
698 	case SIGHUP:
699 	case SIGTERM:
700 	case SIGTRAP:
701 		if (pipeout)
702 			quit("Signal on pipe: cannot recover\n");
703 		msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
704 		(void)fflush(stderr);
705 		(void)fflush(stdout);
706 		close_rewind();
707 		exit(X_REWRITE);
708 		/* NOTREACHED */
709 	case SIGSEGV:
710 		msg("SIGSEGV: ABORTING!\n");
711 		(void)signal(SIGSEGV, SIG_DFL);
712 		(void)kill(0, SIGSEGV);
713 		/* NOTREACHED */
714 	}
715 }
716 
717 char *
718 rawname(char *cp)
719 {
720 	static char rawbuf[MAXPATHLEN];
721 	char *dp = strrchr(cp, '/');
722 
723 	if (dp == NULL)
724 		return (NULL);
725 	*dp = '\0';
726 	(void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
727 	*dp = '/';
728 	return (rawbuf);
729 }
730 
731 /*
732  * obsolete --
733  *	Change set of key letters and ordered arguments into something
734  *	getopt(3) will like.
735  */
736 static void
737 obsolete(int *argcp, char **argvp[])
738 {
739 	int argc, flags;
740 	char *ap, **argv, *flagsp, **nargv, *p;
741 
742 	/* Setup. */
743 	argv = *argvp;
744 	argc = *argcp;
745 
746 	/* Return if no arguments or first argument has leading dash. */
747 	ap = argv[1];
748 	if (argc == 1 || *ap == '-')
749 		return;
750 
751 	/* Allocate space for new arguments. */
752 	*argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
753 	p = flagsp = xmalloc(strlen(ap) + 2);
754 
755 	*nargv++ = *argv;
756 	argv += 2;
757 
758 	for (flags = 0; *ap; ++ap) {
759 		switch (*ap) {
760 		case 'B':
761 		case 'b':
762 		case 'd':
763 		case 'f':
764 		case 'h':
765 		case 's':
766 		case 'T':
767 		case 'x':
768 			if (*argv == NULL) {
769 				warnx("option requires an argument -- %c", *ap);
770 				usage();
771 			}
772 			nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
773 			nargv[0][0] = '-';
774 			nargv[0][1] = *ap;
775 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
776 			++argv;
777 			++nargv;
778 			break;
779 		default:
780 			if (!flags) {
781 				*p++ = '-';
782 				flags = 1;
783 			}
784 			*p++ = *ap;
785 			break;
786 		}
787 	}
788 
789 	/* Terminate flags. */
790 	if (flags) {
791 		*p = '\0';
792 		*nargv++ = flagsp;
793 	} else
794 		free(flagsp);
795 
796 	/* Copy remaining arguments. */
797 	while ((*nargv++ = *argv++) != NULL)
798 		;
799 
800 	/* Update argument count. */
801 	*argcp = nargv - *argvp - 1;
802 }
803 
804 
805 void *
806 xcalloc(size_t number, size_t size)
807 {
808 	void *p;
809 
810 	p = calloc(number, size);
811 	if (p == NULL)
812 		quit("%s\n", strerror(errno));
813 	return (p);
814 }
815 
816 void *
817 xmalloc(size_t size)
818 {
819 	void *p;
820 
821 	p = malloc(size);
822 	if (p == NULL)
823 		quit("%s\n", strerror(errno));
824 	return (p);
825 }
826 
827 char *
828 xstrdup(const char *str)
829 {
830 	char *p;
831 
832 	p = strdup(str);
833 	if (p == NULL)
834 		quit("%s\n", strerror(errno));
835 	return (p);
836 }
837