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