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