1 /* $NetBSD: main.c,v 1.63 2006/10/26 20:02:30 hannken Exp $ */
4 * Copyright (c) 1980, 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)main.c 8.6 (Berkeley) 5/1/95";
42 __RCSID("$NetBSD: main.c,v 1.63 2006/10/26 20:02:30 hannken Exp $");
46 #include <sys/param.h>
49 #include <sys/mount.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
55 #include <protocols/dumprestore.h>
70 #include "pathnames.h"
73 int timestamp
; /* print message timestamps */
74 int notify
; /* notify operator flag */
75 int blockswritten
; /* number of blocks written on current tape */
76 int tapeno
; /* current tape number */
77 int density
; /* density in bytes/0.1" */
78 int ntrec
= NTREC
; /* # tape blocks in each tape record */
79 int cartridge
; /* Assume non-cartridge tape */
80 long dev_bsize
= 1; /* recalculated below */
81 long blocksperfile
; /* output blocks per file */
82 const char *host
; /* remote host (if any) */
83 int readcache
= -1; /* read cache size (in readblksize blks) */
84 int readblksize
= 32 * 1024; /* read block size */
85 char default_time_string
[] = "%T %Z"; /* default timestamp string */
86 char *time_string
= default_time_string
; /* timestamp string */
88 int main(int, char *[]);
89 static long numarg(const char *, long, long);
90 static void obsolete(int *, char **[]);
91 static void usage(void);
94 main(int argc
, char *argv
[])
100 struct statvfs
*mntinfo
, fsbuf
;
103 int i
, anydirskipped
, bflag
= 0, Tflag
= 0, Fflag
= 0, honorlevel
= 1;
104 int snap_internal
= 0;
109 int just_estimate
= 0;
110 char labelstr
[LBLSIZE
];
111 char *new_time_format
;
112 char *snap_backup
= NULL
;
117 tzset(); /* set up timezone for strftime */
118 if ((new_time_format
= getenv("TIMEFORMAT")) != NULL
)
119 time_string
= new_time_format
;
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
;
126 strcpy(labelstr
, "none"); /* XXX safe strcpy. */
127 if (TP_BSIZE
/ DEV_BSIZE
== 0 || TP_BSIZE
% DEV_BSIZE
!= 0)
128 quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
135 obsolete(&argc
, &argv
);
136 while ((ch
= getopt(argc
, argv
,
137 "0123456789aB:b:cd:eFf:h:k:l:L:nr:s:StT:uWwx:X")) != -1)
140 case '0': case '1': case '2': case '3': case '4':
141 case '5': case '6': case '7': case '8': case '9':
145 case 'a': /* `auto-size', Write to EOM. */
149 case 'B': /* blocks per output file */
150 blocksperfile
= numarg("blocks per file", 1L, 0L);
153 case 'b': /* blocks per tape write */
154 ntrec
= numarg("blocks per write", 1L, 1000L);
158 case 'c': /* Tape is cart. not 9-track */
162 case 'd': /* density, in bits per inch */
163 density
= numarg("density", 10L, 327670L) / 10;
164 if (density
>= 625 && !bflag
)
165 ntrec
= HIGHDENSITYTREC
;
168 case 'e': /* eject full tapes */
172 case 'F': /* files-to-dump is an fs image */
176 case 'f': /* output file */
181 honorlevel
= numarg("honor level", 0L, 10L);
185 readblksize
= numarg("read block size", 0, 64) * 1024;
188 case 'l': /* autoload after eject full tapes */
190 lflag
= numarg("timeout (in seconds)", 1, 0);
195 * Note that although there are LBLSIZE characters,
196 * the last must be '\0', so the limit on strlen()
197 * is really LBLSIZE-1.
199 if (strlcpy(labelstr
, optarg
, sizeof(labelstr
))
200 >= sizeof(labelstr
)) {
202 "WARNING Label `%s' is larger than limit of %lu characters.\n",
204 (unsigned long)sizeof(labelstr
) - 1);
205 msg("WARNING: Using truncated label `%s'.\n",
209 case 'n': /* notify operators */
213 case 'r': /* read cache size */
214 readcache
= numarg("read cache size", 0, 512);
217 case 's': /* tape size, feet */
218 tsize
= numarg("tape size", 1L, 0L) * 12 * 10;
221 case 'S': /* exit after estimating # of tapes */
229 case 'T': /* time of last dump */
230 spcl
.c_ddate
= unctime(optarg
);
231 if (spcl
.c_ddate
< 0) {
232 (void)fprintf(stderr
, "bad time \"%s\"\n",
240 case 'u': /* update /etc/dumpdates */
244 case 'W': /* what to do */
247 exit(X_FINOK
); /* do nothing else */
250 snap_backup
= optarg
;
264 (void)fprintf(stderr
,
265 "Must specify disk or image, or file list\n");
271 * determine if disk is a subdirectory, and setup appropriately
273 getfstab(); /* /etc/fstab snarfed */
278 for (i
= 0; i
< argc
; i
++) {
281 if (lstat(argv
[i
], &sb
) == -1)
282 quit("Cannot stat %s: %s\n", argv
[i
], strerror(errno
));
283 if (Fflag
|| S_ISCHR(sb
.st_mode
) || S_ISBLK(sb
.st_mode
)) {
288 "Can't dump a disk or image at the same time as a file list\n");
291 if ((dt
= fstabsearch(argv
[i
])) != NULL
) {
293 mountpoint
= xstrdup(dt
->fs_file
);
296 if (statvfs(argv
[i
], &fsbuf
) == -1)
297 quit("Cannot statvfs %s: %s\n", argv
[i
],
299 disk
= fsbuf
.f_mntfromname
;
300 if (strcmp(argv
[i
], fsbuf
.f_mntonname
) == 0)
302 if (mountpoint
== NULL
) {
303 mountpoint
= xstrdup(fsbuf
.f_mntonname
);
305 msg("Ignoring u flag for subdir dump\n");
309 msg("Subdir dump is done at level 0\n");
312 msg("Dumping sub files/directories from %s\n",
315 if (strcmp(mountpoint
, fsbuf
.f_mntonname
) != 0)
316 quit("%s is not on %s\n", argv
[i
], mountpoint
);
318 msg("Dumping file/directory %s\n", argv
[i
]);
327 (void)fprintf(stderr
, "Excess arguments to dump:");
329 (void)fprintf(stderr
, " %s", *argv
++);
330 (void)fprintf(stderr
, "\n");
334 if (Tflag
&& uflag
) {
335 (void)fprintf(stderr
,
336 "You cannot use the T and u flags together.\n");
339 if (strcmp(tape
, "-") == 0) {
341 tape
= "standard output";
345 blocksperfile
= blocksperfile
/ ntrec
* ntrec
; /* round down */
346 else if (!unlimited
) {
348 * Determine how to default tape size and density
351 * 9-track 1600 bpi (160 bytes/.1") 2300 ft.
352 * 9-track 6250 bpi (625 bytes/.1") 2300 ft.
353 * cartridge 8000 bpi (100 bytes/.1") 1700 ft.
357 density
= cartridge
? 100 : 160;
359 tsize
= cartridge
? 1700L*120L : 2300L*120L;
362 if ((cp
= strchr(tape
, ':')) != NULL
) {
364 /* This is fine, because all the const strings don't have : */
368 if (rmthost(host
) == 0)
371 (void)fprintf(stderr
, "remote dump not enabled\n");
376 if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
)
378 if (signal(SIGTRAP
, SIG_IGN
) != SIG_IGN
)
379 signal(SIGTRAP
, sig
);
380 if (signal(SIGFPE
, SIG_IGN
) != SIG_IGN
)
382 if (signal(SIGBUS
, SIG_IGN
) != SIG_IGN
)
385 if (signal(SIGSEGV
, SIG_IGN
) != SIG_IGN
)
386 signal(SIGSEGV
, sig
);
388 if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
)
389 signal(SIGTERM
, sig
);
390 if (signal(SIGINT
, interrupt
) == SIG_IGN
)
391 signal(SIGINT
, SIG_IGN
);
394 * disk can be either the full special file name, or
395 * the file system name.
398 mntinfo
= mntinfosearch(disk
);
399 if ((dt
= fstabsearch(disk
)) != NULL
) {
400 disk
= rawname(dt
->fs_spec
);
401 mountpoint
= dt
->fs_file
;
402 msg("Found %s on %s in %s\n", disk
, mountpoint
, _PATH_FSTAB
);
403 } else if (mntinfo
!= NULL
) {
404 disk
= rawname(mntinfo
->f_mntfromname
);
405 mountpoint
= mntinfo
->f_mntonname
;
406 msg("Found %s on %s in mount table\n", disk
, mountpoint
);
408 if (mountpoint
!= NULL
) {
410 (void)snprintf(spcl
.c_filesys
, sizeof(spcl
.c_filesys
),
411 "a subset of %s", mountpoint
);
413 (void)strlcpy(spcl
.c_filesys
, mountpoint
,
414 sizeof(spcl
.c_filesys
));
416 (void)strlcpy(spcl
.c_filesys
, "a file system image",
417 sizeof(spcl
.c_filesys
));
419 (void)strlcpy(spcl
.c_filesys
, "an unlisted file system",
420 sizeof(spcl
.c_filesys
));
422 (void)strlcpy(spcl
.c_dev
, disk
, sizeof(spcl
.c_dev
));
423 (void)strlcpy(spcl
.c_label
, labelstr
, sizeof(spcl
.c_label
));
424 (void)gethostname(spcl
.c_host
, sizeof(spcl
.c_host
));
425 spcl
.c_host
[sizeof(spcl
.c_host
) - 1] = '\0';
427 if ((snap_backup
!= NULL
|| snap_internal
) && mntinfo
== NULL
) {
428 msg("WARNING: Cannot use -x or -X on unmounted file system.\n");
435 if (snap_backup
!= NULL
|| snap_internal
) {
436 if (lfs_wrap_stop(mountpoint
) < 0) {
437 msg("Cannot stop writing on %s\n", mountpoint
);
441 if ((diskfd
= open(disk
, O_RDONLY
)) < 0) {
442 msg("Cannot open %s\n", disk
);
446 #else /* ! DUMP_LFS */
447 if (snap_backup
!= NULL
|| snap_internal
) {
448 diskfd
= snap_open(mntinfo
->f_mntonname
, snap_backup
,
451 msg("Cannot open snapshot of %s\n",
452 mntinfo
->f_mntonname
);
457 if ((diskfd
= open(disk
, O_RDONLY
)) < 0) {
458 msg("Cannot open %s\n", disk
);
464 #endif /* ! DUMP_LFS */
466 needswap
= fs_read_sblock(sblock_buf
);
468 spcl
.c_level
= iswap32(level
- '0');
469 spcl
.c_type
= iswap32(TS_TAPE
);
470 spcl
.c_date
= iswap32(spcl
.c_date
);
471 spcl
.c_ddate
= iswap32(spcl
.c_ddate
);
473 getdumptime(); /* /etc/dumpdates snarfed */
475 date
= iswap32(spcl
.c_date
);
476 msg("Date of this level %c dump: %s", level
,
477 spcl
.c_date
== 0 ? "the epoch\n" : ctime(&date
));
478 date
= iswap32(spcl
.c_ddate
);
479 msg("Date of last level %c dump: %s", lastlevel
,
480 spcl
.c_ddate
== 0 ? "the epoch\n" : ctime(&date
));
482 if (snap_backup
!= NULL
|| snap_internal
)
483 msgtail("a snapshot of ");
485 msgtail("a subset of ");
486 msgtail("%s (%s) ", disk
, spcl
.c_filesys
);
488 msgtail("to %s on host %s\n", tape
, host
);
490 msgtail("to %s\n", tape
);
491 msg("Label: %s\n", labelstr
);
493 ufsib
= fs_parametrize();
495 dev_bshift
= ffs(dev_bsize
) - 1;
496 if (dev_bsize
!= (1 << dev_bshift
))
497 quit("dev_bsize (%ld) is not a power of 2", dev_bsize
);
498 tp_bshift
= ffs(TP_BSIZE
) - 1;
499 if (TP_BSIZE
!= (1 << tp_bshift
))
500 quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE
);
501 maxino
= fs_maxino();
502 mapsize
= roundup(howmany(maxino
, NBBY
), TP_BSIZE
);
503 usedinomap
= (char *)xcalloc((unsigned) mapsize
, sizeof(char));
504 dumpdirmap
= (char *)xcalloc((unsigned) mapsize
, sizeof(char));
505 dumpinomap
= (char *)xcalloc((unsigned) mapsize
, sizeof(char));
506 tapesize
= 3 * (howmany(mapsize
* sizeof(char), TP_BSIZE
) + 1);
508 nonodump
= iswap32(spcl
.c_level
) < honorlevel
;
510 initcache(readcache
, readblksize
);
512 (void)signal(SIGINFO
, statussig
);
514 msg("mapping (Pass I) [regular files]\n");
515 anydirskipped
= mapfiles(maxino
, &tapesize
, mountpoint
,
516 (dirc
? argv
: NULL
));
518 msg("mapping (Pass II) [directories]\n");
519 while (anydirskipped
) {
520 anydirskipped
= mapdirs(maxino
, &tapesize
);
523 if (pipeout
|| unlimited
) {
524 tapesize
+= 10; /* 10 trailer blocks */
525 msg("estimated %llu tape blocks.\n",
526 (unsigned long long)tapesize
);
531 fetapes
= (double) tapesize
/ blocksperfile
;
532 else if (cartridge
) {
533 /* Estimate number of tapes, assuming streaming stops at
534 the end of each block written, and not in mid-block.
535 Assume no erroneous blocks; this can be compensated
536 for with an artificially low tape size. */
538 ( (double) tapesize
/* blocks */
539 * TP_BSIZE
/* bytes/block */
540 * (1.0/density
) /* 0.1" / byte */
542 (double) tapesize
/* blocks */
543 * (1.0/ntrec
) /* streaming-stops per block */
544 * 15.48 /* 0.1" / streaming-stop */
545 ) * (1.0 / tsize
); /* tape / 0.1" */
547 /* Estimate number of tapes, for old fashioned 9-track
549 int tenthsperirg
= (density
== 625) ? 3 : 7;
551 ( tapesize
/* blocks */
552 * TP_BSIZE
/* bytes / block */
553 * (1.0/density
) /* 0.1" / byte */
555 tapesize
/* blocks */
556 * (1.0/ntrec
) /* IRG's / block */
557 * tenthsperirg
/* 0.1" / IRG */
558 ) * (1.0 / tsize
); /* tape / 0.1" */
560 etapes
= fetapes
; /* truncating assignment */
562 /* count the dumped inodes map on each additional tape */
563 tapesize
+= (etapes
- 1) *
564 (howmany(mapsize
* sizeof(char), TP_BSIZE
) + 1);
565 tapesize
+= etapes
+ 10; /* headers + 10 trailer blks */
566 msg("estimated %llu tape blocks on %3.2f tape(s).\n",
567 (unsigned long long)tapesize
, fetapes
);
570 * If the user only wants an estimate of the number of
577 * Allocate tape buffer.
580 quit("can't allocate tape buffers - try a smaller blocking factor.\n");
583 (void)time((time_t *)&(tstart_writing
));
585 dumpmap(usedinomap
, TS_CLRI
, maxino
- 1);
587 msg("dumping (Pass III) [directories]\n");
588 dirty
= 0; /* XXX just to get gcc to shut up */
589 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
590 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
594 if ((dirty
& 1) == 0)
597 * Skip directory inodes deleted and maybe reallocated
600 if ((DIP(dp
, mode
) & IFMT
) != IFDIR
)
602 (void)dumpino(dp
, ino
);
605 msg("dumping (Pass IV) [regular files]\n");
606 for (map
= dumpinomap
, ino
= 1; ino
< maxino
; ino
++) {
609 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
613 if ((dirty
& 1) == 0)
616 * Skip inodes deleted and reallocated as directories.
619 mode
= DIP(dp
, mode
) & IFMT
;
622 (void)dumpino(dp
, ino
);
625 spcl
.c_type
= iswap32(TS_END
);
626 for (i
= 0; i
< ntrec
; i
++)
627 writeheader(maxino
- 1);
629 msg("%d tape blocks\n",iswap32(spcl
.c_tapea
));
631 msg("%d tape blocks on %d volume%s\n",
632 iswap32(spcl
.c_tapea
), iswap32(spcl
.c_volume
),
633 (iswap32(spcl
.c_volume
) == 1) ? "" : "s");
635 date
= iswap32(spcl
.c_date
);
636 msg("Date of this level %c dump: %s", level
,
637 spcl
.c_date
== 0 ? "the epoch\n" : ctime(&date
));
638 msg("Date this dump completed: %s", ctime(&tnow
));
639 msg("Average transfer rate: %d KB/s\n", xferrate
/ tapeno
);
642 broadcast("DUMP IS DONE!\a\a\n");
645 #endif /* DUMP_LFS */
646 msg("DUMP IS DONE\n");
649 exit(X_FINOK
); /* XXX: to satisfy gcc */
655 const char *prog
= getprogname();
657 (void)fprintf(stderr
,
658 "usage: %s [-0123456789aceFnStuX] [-B records] [-b blocksize]\n"
659 " [-d density] [-f file] [-h level] [-k read-blocksize]\n"
660 " [-L label] [-l timeout] [-r read-cache] [-s feet]\n"
661 " [-T date] [-x snap-backup] files-to-dump\n"
662 " %s [-W | -w]\n", prog
, prog
);
667 * Pick up a numeric argument. It must be nonnegative and in the given
668 * range (except that a vmax of 0 means unlimited).
671 numarg(const char *meaning
, long vmin
, long vmax
)
676 val
= strtol(optarg
, &p
, 10);
678 errx(X_STARTUP
, "illegal %s -- %s", meaning
, optarg
);
679 if (val
< vmin
|| (vmax
&& val
> vmax
))
680 errx(X_STARTUP
, "%s must be between %ld and %ld",
681 meaning
, vmin
, vmax
);
697 quit("Signal on pipe: cannot recover\n");
698 msg("Rewriting attempted as response to signal %s.\n", sys_siglist
[signo
]);
699 (void)fflush(stderr
);
700 (void)fflush(stdout
);
705 msg("SIGSEGV: ABORTING!\n");
706 (void)signal(SIGSEGV
, SIG_DFL
);
707 (void)kill(0, SIGSEGV
);
715 static char rawbuf
[MAXPATHLEN
];
716 char *dp
= strrchr(cp
, '/');
721 (void)snprintf(rawbuf
, sizeof rawbuf
, "%s/r%s", cp
, dp
+ 1);
728 * Change set of key letters and ordered arguments into something
729 * getopt(3) will like.
732 obsolete(int *argcp
, char **argvp
[])
735 char *ap
, **argv
, *flagsp
, **nargv
, *p
;
741 /* Return if no arguments or first argument has leading dash. */
743 if (argc
== 1 || *ap
== '-')
746 /* Allocate space for new arguments. */
747 *argvp
= nargv
= xmalloc((argc
+ 1) * sizeof(char *));
748 p
= flagsp
= xmalloc(strlen(ap
) + 2);
753 for (flags
= 0; *ap
; ++ap
) {
764 warnx("option requires an argument -- %c", *ap
);
767 nargv
[0] = xmalloc(strlen(*argv
) + 2 + 1);
770 (void)strcpy(&nargv
[0][2], *argv
); /* XXX safe strcpy */
784 /* Terminate flags. */
791 /* Copy remaining arguments. */
792 while ((*nargv
++ = *argv
++) != NULL
)
795 /* Update argument count. */
796 *argcp
= nargv
- *argvp
- 1;
801 xcalloc(size_t number
, size_t size
)
805 p
= calloc(number
, size
);
807 quit("%s\n", strerror(errno
));
818 quit("%s\n", strerror(errno
));
823 xstrdup(const char *str
)
829 quit("%s\n", strerror(errno
));