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