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