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