xref: /original-bsd/sbin/dump/main.c (revision 0842ddeb)
1 /*-
2  * Copyright (c) 1980, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 05/01/95";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/time.h>
20 #ifdef sunos
21 #include <sys/vnode.h>
22 
23 #include <ufs/inode.h>
24 #include <ufs/fs.h>
25 #else
26 #include <ufs/ufs/dinode.h>
27 #include <ufs/ffs/fs.h>
28 #endif
29 
30 #include <protocols/dumprestore.h>
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <fstab.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "dump.h"
44 #include "pathnames.h"
45 
46 #ifndef SBOFF
47 #define SBOFF (SBLOCK * DEV_BSIZE)
48 #endif
49 
50 int	notify = 0;	/* notify operator flag */
51 int	blockswritten = 0;	/* number of blocks written on current tape */
52 int	tapeno = 0;	/* current tape number */
53 int	density = 0;	/* density in bytes/0.1" */
54 int	ntrec = NTREC;	/* # tape blocks in each tape record */
55 int	cartridge = 0;	/* Assume non-cartridge tape */
56 long	dev_bsize = 1;	/* recalculated below */
57 long	blocksperfile;	/* output blocks per file */
58 char	*host = NULL;	/* remote host (if any) */
59 
60 static long numarg __P((char *, long, long));
61 static void obsolete __P((int *, char **[]));
62 static void usage __P((void));
63 
64 int
65 main(argc, argv)
66 	int argc;
67 	char *argv[];
68 {
69 	register ino_t ino;
70 	register int dirty;
71 	register struct dinode *dp;
72 	register struct	fstab *dt;
73 	register char *map;
74 	register int ch;
75 	int i, anydirskipped, bflag = 0, Tflag = 0, honorlevel = 1;
76 	ino_t maxino;
77 
78 	spcl.c_date = 0;
79 	(void)time((time_t *)&spcl.c_date);
80 
81 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
82 	tape = _PATH_DEFTAPE;
83 	dumpdates = _PATH_DUMPDATES;
84 	temp = _PATH_DTMP;
85 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
86 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
87 	level = '0';
88 
89 	if (argc < 2)
90 		usage();
91 
92 	obsolete(&argc, &argv);
93 	while ((ch = getopt(argc, argv, "0123456789B:b:cd:f:h:ns:T:uWw")) != -1)
94 		switch (ch) {
95 		/* dump level */
96 		case '0': case '1': case '2': case '3': case '4':
97 		case '5': case '6': case '7': case '8': case '9':
98 			level = ch;
99 			break;
100 
101 		case 'B':		/* blocks per output file */
102 			blocksperfile = numarg("blocks per file", 1L, 0L);
103 			break;
104 
105 		case 'b':		/* blocks per tape write */
106 			ntrec = numarg("blocks per write", 1L, 1000L);
107 			break;
108 
109 		case 'c':		/* Tape is cart. not 9-track */
110 			cartridge = 1;
111 			break;
112 
113 		case 'd':		/* density, in bits per inch */
114 			density = numarg("density", 10L, 327670L) / 10;
115 			if (density >= 625 && !bflag)
116 				ntrec = HIGHDENSITYTREC;
117 			break;
118 
119 		case 'f':		/* output file */
120 			tape = optarg;
121 			break;
122 
123 		case 'h':
124 			honorlevel = numarg("honor level", 0L, 10L);
125 			break;
126 
127 		case 'n':		/* notify operators */
128 			notify = 1;
129 			break;
130 
131 		case 's':		/* tape size, feet */
132 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
133 			break;
134 
135 		case 'T':		/* time of last dump */
136 			spcl.c_ddate = unctime(optarg);
137 			if (spcl.c_ddate < 0) {
138 				(void)fprintf(stderr, "bad time \"%s\"\n",
139 				    optarg);
140 				exit(X_ABORT);
141 			}
142 			Tflag = 1;
143 			lastlevel = '?';
144 			break;
145 
146 		case 'u':		/* update /etc/dumpdates */
147 			uflag = 1;
148 			break;
149 
150 		case 'W':		/* what to do */
151 		case 'w':
152 			lastdump(ch);
153 			exit(0);	/* do nothing else */
154 
155 		default:
156 			usage();
157 		}
158 	argc -= optind;
159 	argv += optind;
160 
161 	if (argc < 1) {
162 		(void)fprintf(stderr, "Must specify disk or filesystem\n");
163 		exit(X_ABORT);
164 	}
165 	disk = *argv++;
166 	argc--;
167 	if (argc >= 1) {
168 		(void)fprintf(stderr, "Unknown arguments to dump:");
169 		while (argc--)
170 			(void)fprintf(stderr, " %s", *argv++);
171 		(void)fprintf(stderr, "\n");
172 		exit(X_ABORT);
173 	}
174 	if (Tflag && uflag) {
175 	        (void)fprintf(stderr,
176 		    "You cannot use the T and u flags together.\n");
177 		exit(X_ABORT);
178 	}
179 	if (strcmp(tape, "-") == 0) {
180 		pipeout++;
181 		tape = "standard output";
182 	}
183 
184 	if (blocksperfile)
185 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
186 	else {
187 		/*
188 		 * Determine how to default tape size and density
189 		 *
190 		 *         	density				tape size
191 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
192 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
193 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
194 		 *						(450*4 - slop)
195 		 */
196 		if (density == 0)
197 			density = cartridge ? 100 : 160;
198 		if (tsize == 0)
199 			tsize = cartridge ? 1700L*120L : 2300L*120L;
200 	}
201 
202 	if (strchr(tape, ':')) {
203 		host = tape;
204 		tape = strchr(host, ':');
205 		*tape++ = '\0';
206 #ifdef RDUMP
207 		if (rmthost(host) == 0)
208 			exit(X_ABORT);
209 #else
210 		(void)fprintf(stderr, "remote dump not enabled\n");
211 		exit(X_ABORT);
212 #endif
213 	}
214 	(void)setuid(getuid()); /* rmthost() is the only reason to be setuid */
215 
216 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
217 		signal(SIGHUP, sig);
218 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
219 		signal(SIGTRAP, sig);
220 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
221 		signal(SIGFPE, sig);
222 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
223 		signal(SIGBUS, sig);
224 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
225 		signal(SIGSEGV, sig);
226 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
227 		signal(SIGTERM, sig);
228 	if (signal(SIGINT, interrupt) == SIG_IGN)
229 		signal(SIGINT, SIG_IGN);
230 
231 	set_operators();	/* /etc/group snarfed */
232 	getfstab();		/* /etc/fstab snarfed */
233 	/*
234 	 *	disk can be either the full special file name,
235 	 *	the suffix of the special file name,
236 	 *	the special name missing the leading '/',
237 	 *	the file system name with or without the leading '/'.
238 	 */
239 	dt = fstabsearch(disk);
240 	if (dt != NULL) {
241 		disk = rawname(dt->fs_spec);
242 		(void)strncpy(spcl.c_dev, dt->fs_spec, NAMELEN);
243 		(void)strncpy(spcl.c_filesys, dt->fs_file, NAMELEN);
244 	} else {
245 		(void)strncpy(spcl.c_dev, disk, NAMELEN);
246 		(void)strncpy(spcl.c_filesys, "an unlisted file system",
247 		    NAMELEN);
248 	}
249 	(void)strcpy(spcl.c_label, "none");
250 	(void)gethostname(spcl.c_host, NAMELEN);
251 	spcl.c_level = level - '0';
252 	spcl.c_type = TS_TAPE;
253 	if (!Tflag)
254 	        getdumptime();		/* /etc/dumpdates snarfed */
255 
256 	msg("Date of this level %c dump: %s", level,
257 		spcl.c_date == 0 ? "the epoch\n" : ctime(&spcl.c_date));
258  	msg("Date of last level %c dump: %s", lastlevel,
259 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&spcl.c_ddate));
260 	msg("Dumping %s ", disk);
261 	if (dt != NULL)
262 		msgtail("(%s) ", dt->fs_file);
263 	if (host)
264 		msgtail("to %s on host %s\n", tape, host);
265 	else
266 		msgtail("to %s\n", tape);
267 
268 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
269 		msg("Cannot open %s\n", disk);
270 		exit(X_ABORT);
271 	}
272 	sync();
273 	sblock = (struct fs *)sblock_buf;
274 	bread(SBOFF, (char *) sblock, SBSIZE);
275 	if (sblock->fs_magic != FS_MAGIC)
276 		quit("bad sblock magic number\n");
277 	dev_bsize = sblock->fs_fsize / fsbtodb(sblock, 1);
278 	dev_bshift = ffs(dev_bsize) - 1;
279 	if (dev_bsize != (1 << dev_bshift))
280 		quit("dev_bsize (%d) is not a power of 2", dev_bsize);
281 	tp_bshift = ffs(TP_BSIZE) - 1;
282 	if (TP_BSIZE != (1 << tp_bshift))
283 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
284 #ifdef FS_44INODEFMT
285 	if (sblock->fs_inodefmt >= FS_44INODEFMT)
286 		spcl.c_flags |= DR_NEWINODEFMT;
287 #endif
288 	maxino = sblock->fs_ipg * sblock->fs_ncg;
289 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
290 	usedinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
291 	dumpdirmap = (char *)calloc((unsigned) mapsize, sizeof(char));
292 	dumpinomap = (char *)calloc((unsigned) mapsize, sizeof(char));
293 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
294 
295 	nonodump = spcl.c_level < honorlevel;
296 
297 	msg("mapping (Pass I) [regular files]\n");
298 	anydirskipped = mapfiles(maxino, &tapesize);
299 
300 	msg("mapping (Pass II) [directories]\n");
301 	while (anydirskipped) {
302 		anydirskipped = mapdirs(maxino, &tapesize);
303 	}
304 
305 	if (pipeout) {
306 		tapesize += 10;	/* 10 trailer blocks */
307 		msg("estimated %ld tape blocks.\n", tapesize);
308 	} else {
309 		double fetapes;
310 
311 		if (blocksperfile)
312 			fetapes = (double) tapesize / blocksperfile;
313 		else if (cartridge) {
314 			/* Estimate number of tapes, assuming streaming stops at
315 			   the end of each block written, and not in mid-block.
316 			   Assume no erroneous blocks; this can be compensated
317 			   for with an artificially low tape size. */
318 			fetapes =
319 			(	  tapesize	/* blocks */
320 				* TP_BSIZE	/* bytes/block */
321 				* (1.0/density)	/* 0.1" / byte */
322 			  +
323 				  tapesize	/* blocks */
324 				* (1.0/ntrec)	/* streaming-stops per block */
325 				* 15.48		/* 0.1" / streaming-stop */
326 			) * (1.0 / tsize );	/* tape / 0.1" */
327 		} else {
328 			/* Estimate number of tapes, for old fashioned 9-track
329 			   tape */
330 			int tenthsperirg = (density == 625) ? 3 : 7;
331 			fetapes =
332 			(	  tapesize	/* blocks */
333 				* TP_BSIZE	/* bytes / block */
334 				* (1.0/density)	/* 0.1" / byte */
335 			  +
336 				  tapesize	/* blocks */
337 				* (1.0/ntrec)	/* IRG's / block */
338 				* tenthsperirg	/* 0.1" / IRG */
339 			) * (1.0 / tsize );	/* tape / 0.1" */
340 		}
341 		etapes = fetapes;		/* truncating assignment */
342 		etapes++;
343 		/* count the dumped inodes map on each additional tape */
344 		tapesize += (etapes - 1) *
345 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
346 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
347 		msg("estimated %ld tape blocks on %3.2f tape(s).\n",
348 		    tapesize, fetapes);
349 	}
350 
351 	/*
352 	 * Allocate tape buffer.
353 	 */
354 	if (!alloctape())
355 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
356 
357 	startnewtape(1);
358 	(void)time((time_t *)&(tstart_writing));
359 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
360 
361 	msg("dumping (Pass III) [directories]\n");
362 	dirty = 0;		/* XXX just to get gcc to shut up */
363 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
364 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
365 			dirty = *map++;
366 		else
367 			dirty >>= 1;
368 		if ((dirty & 1) == 0)
369 			continue;
370 		/*
371 		 * Skip directory inodes deleted and maybe reallocated
372 		 */
373 		dp = getino(ino);
374 		if ((dp->di_mode & IFMT) != IFDIR)
375 			continue;
376 		(void)dumpino(dp, ino);
377 	}
378 
379 	msg("dumping (Pass IV) [regular files]\n");
380 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
381 		int mode;
382 
383 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
384 			dirty = *map++;
385 		else
386 			dirty >>= 1;
387 		if ((dirty & 1) == 0)
388 			continue;
389 		/*
390 		 * Skip inodes deleted and reallocated as directories.
391 		 */
392 		dp = getino(ino);
393 		mode = dp->di_mode & IFMT;
394 		if (mode == IFDIR)
395 			continue;
396 		(void)dumpino(dp, ino);
397 	}
398 
399 	spcl.c_type = TS_END;
400 	for (i = 0; i < ntrec; i++)
401 		writeheader(maxino - 1);
402 	if (pipeout)
403 		msg("DUMP: %ld tape blocks\n",spcl.c_tapea);
404 	else
405 		msg("DUMP: %ld tape blocks on %d volumes(s)\n",
406 		    spcl.c_tapea, spcl.c_volume);
407 	putdumptime();
408 	trewind();
409 	broadcast("DUMP IS DONE!\7\7\n");
410 	msg("DUMP IS DONE\n");
411 	Exit(X_FINOK);
412 	/* NOTREACHED */
413 }
414 
415 static void
416 usage()
417 {
418 
419 	(void)fprintf(stderr, "usage: dump [-0123456789cnu] [-B records] [-b blocksize] [-d density] [-f file]\n            [-h level] [-s feet] [-T date] filesystem\n");
420 	(void)fprintf(stderr, "       dump [-W | -w]\n");
421 	exit(1);
422 }
423 
424 /*
425  * Pick up a numeric argument.  It must be nonnegative and in the given
426  * range (except that a vmax of 0 means unlimited).
427  */
428 static long
429 numarg(meaning, vmin, vmax)
430 	char *meaning;
431 	long vmin, vmax;
432 {
433 	char *p;
434 	long val;
435 
436 	val = strtol(optarg, &p, 10);
437 	if (*p)
438 		errx(1, "illegal %s -- %s", meaning, optarg);
439 	if (val < vmin || (vmax && val > vmax))
440 		errx(1, "%s must be between %ld and %ld", meaning, vmin, vmax);
441 	return (val);
442 }
443 
444 void
445 sig(signo)
446 	int signo;
447 {
448 	switch(signo) {
449 	case SIGALRM:
450 	case SIGBUS:
451 	case SIGFPE:
452 	case SIGHUP:
453 	case SIGTERM:
454 	case SIGTRAP:
455 		if (pipeout)
456 			quit("Signal on pipe: cannot recover\n");
457 		msg("Rewriting attempted as response to unknown signal.\n");
458 		(void)fflush(stderr);
459 		(void)fflush(stdout);
460 		close_rewind();
461 		exit(X_REWRITE);
462 		/* NOTREACHED */
463 	case SIGSEGV:
464 		msg("SIGSEGV: ABORTING!\n");
465 		(void)signal(SIGSEGV, SIG_DFL);
466 		(void)kill(0, SIGSEGV);
467 		/* NOTREACHED */
468 	}
469 }
470 
471 char *
472 rawname(cp)
473 	char *cp;
474 {
475 	static char rawbuf[MAXPATHLEN];
476 	char *dp = strrchr(cp, '/');
477 
478 	if (dp == NULL)
479 		return (NULL);
480 	*dp = '\0';
481 	(void)strcpy(rawbuf, cp);
482 	*dp = '/';
483 	(void)strcat(rawbuf, "/r");
484 	(void)strcat(rawbuf, dp + 1);
485 	return (rawbuf);
486 }
487 
488 /*
489  * obsolete --
490  *	Change set of key letters and ordered arguments into something
491  *	getopt(3) will like.
492  */
493 static void
494 obsolete(argcp, argvp)
495 	int *argcp;
496 	char **argvp[];
497 {
498 	int argc, flags;
499 	char *ap, **argv, *flagsp, **nargv, *p;
500 
501 	/* Setup. */
502 	argv = *argvp;
503 	argc = *argcp;
504 
505 	/* Return if no arguments or first argument has leading dash. */
506 	ap = argv[1];
507 	if (argc == 1 || *ap == '-')
508 		return;
509 
510 	/* Allocate space for new arguments. */
511 	if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL ||
512 	    (p = flagsp = malloc(strlen(ap) + 2)) == NULL)
513 		err(1, NULL);
514 
515 	*nargv++ = *argv;
516 	argv += 2;
517 
518 	for (flags = 0; *ap; ++ap) {
519 		switch (*ap) {
520 		case 'B':
521 		case 'b':
522 		case 'd':
523 		case 'f':
524 		case 'h':
525 		case 's':
526 		case 'T':
527 			if (*argv == NULL) {
528 				warnx("option requires an argument -- %c", *ap);
529 				usage();
530 			}
531 			if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL)
532 				err(1, NULL);
533 			nargv[0][0] = '-';
534 			nargv[0][1] = *ap;
535 			(void)strcpy(&nargv[0][2], *argv);
536 			++argv;
537 			++nargv;
538 			break;
539 		default:
540 			if (!flags) {
541 				*p++ = '-';
542 				flags = 1;
543 			}
544 			*p++ = *ap;
545 			break;
546 		}
547 	}
548 
549 	/* Terminate flags. */
550 	if (flags) {
551 		*p = '\0';
552 		*nargv++ = flagsp;
553 	}
554 
555 	/* Copy remaining arguments. */
556 	while (*nargv++ = *argv++);
557 
558 	/* Update argument count. */
559 	*argcp = nargv - *argvp - 1;
560 }
561