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