dumpmain.c revision fe0e7ec4d916b05b52d8c7cc8a3e6a1b28e77b6f
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "dump.h"
#include <rmt.h>
#include <limits.h>
#include <priv_utils.h>
#include "roll_log.h"
int notify = 0; /* notify operator flag */
int blockswritten = 0; /* number of blocks written on current tape */
int density = 0; /* density in bytes/0.1" */
int tenthsperirg; /* inter-record-gap in 0.1"'s */
int cartridge = 0; /* assume non-cartridge tape */
int diskette = 0; /* assume not dumping to a diskette */
int printsize = 0; /* just print estimated size and exit */
#ifdef DEBUG
int xflag; /* debugging switch */
#endif
char *myname;
/*
* This should be struct fs, but there are trailing bits on disk
* that we also need to read in as part of it. It's an array of
* longs instead of char to force proper alignment.
*/
static long sblock_buf[SBSIZE/sizeof (long)];
#ifdef __STDC__
static char *mb(u_offset_t);
static void nextstate(int);
#else
static char *mb();
static void nextstate();
#endif
#define FUDGE_FACTOR 0x2000000
int
{
char *arg;
double fetapes = 0.0;
u_offset_t esize_shift = 0;
myname++;
else
}
autoload_period = 12;
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif /* TEXT_DOMAIN */
(void) textdomain(TEXT_DOMAIN);
/*
* If someone strips the set-uid bit, dump will still work for local
* tapes. Fail when we try to access a remote tape.
*/
}
tsize = 0; /* no default size, detect EOT dynamically */
disk_dynamic = 0;
incno = '9';
uflag = 0;
arg = "u";
tlabel = "none";
if (argc > 1) {
argv++;
argc--;
if (*arg == '-')
arg++;
}
while (*arg)
switch (*arg++) { /* BE CAUTIOUS OF FALLTHROUGHS */
case 'M':
/*
* This undocumented option causes each process to
* mkdir debug_chdir/getpid(), and chdir to it. This is
* to ease the collection of profiling information and
* core dumps.
*/
if (argc > 1) {
argv++;
argc--;
debug_chdir = *argv;
"Each process shall try to chdir to %s/<pid>\n"),
child_chdir();
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'w':
exit(0);
break;
case 'W': /* what to do */
exit(0); /* do nothing else */
break;
case 'T':
if (argc > 1) {
int count;
int multiplier;
char units;
argv++;
argc--;
if (count < 1) {
"Unreasonable autoload timeout period\n"));
dumpabort();
/*NOTREACHED*/
}
switch (units) {
case 's':
multiplier = 1;
break;
case 'h':
multiplier = 3600;
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'm':
multiplier = 60;
break;
default:
"Unknown timeout units indicator `%c'\n"),
units);
dumpabort();
/*NOTREACHED*/
}
autoload_tries = 1 +
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'f': /* output file */
if (argc > 1) {
argv++;
argc--;
if (*tape == '\0') {
dumpabort();
/*NOTREACHED*/
}
} else {
dumpabort();
/*NOTREACHED*/
}
"Cannot verify when dumping to standard out.\n"));
dumpabort();
/*NOTREACHED*/
}
break;
case 'd': /* density, in bits per inch */
if (argc > 1) {
argv++;
argc--;
if (density <= 0) {
"Density must be a positive integer\n"));
dumpabort();
/*NOTREACHED*/
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 's': /* tape size, feet */
if (argc > 1) {
argv++;
argc--;
"Tape size must be a positive integer\n"));
dumpabort();
/*NOTREACHED*/
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 't': /* tracks */
if (argc > 1) {
argv++;
argc--;
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'b': /* blocks per tape write */
if (argc > 1) {
argv++;
argc--;
bflag++;
/*
* We save the ntrec in case we need to change
* tp_bsize later, we will have to recalculate
* it.
*/
"Block size must be a positive, even integer <= %d\n"),
MAXNTREC*2);
dumpabort();
/*NOTREACHED*/
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'c': /* Tape is cart. not 9-track */
case 'C': /* 'C' to be consistent with 'D' */
cartridge++;
break;
case '0': /* dump level */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
uflag++;
break;
case 'n': /* notify operators */
notify++;
break;
case 'a': /* create archive file */
archive = 1;
if (argc > 1) {
argv++;
argc--;
if (**argv == '\0') {
dumpabort();
/*NOTREACHED*/
}
if (archivefile == NULL) {
dumpabort();
/*NOTREACHED*/
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'v':
verify++;
doingverify++;
"Cannot verify when dumping to standard out.\n"));
dumpabort();
/*NOTREACHED*/
}
break;
case 'D':
diskette++;
break;
case 'N':
if (argc > 1) {
argv++;
argc--;
if (**argv == '\0') {
"entry.\n"));
dumpabort();
/*NOTREACHED*/
}
"long.\n"));
dumpabort();
/*NOTREACHED*/
}
"not contain white space.\n"));
dumpabort();
/*NOTREACHED*/
}
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'L':
if (argc > 1) {
argv++;
argc--;
if (**argv == '\0') {
dumpabort();
/*NOTREACHED*/
}
"Truncating label to maximum supported length: `%s'\n"),
tlabel);
}
} else {
dumpabort();
/*NOTREACHED*/
}
break;
case 'l':
autoload++;
break;
case 'o':
offline++;
break;
case 'S':
printsize++;
break;
#ifdef DEBUG
case 'z':
xflag++;
break;
#endif
default:
dumpabort();
/*NOTREACHED*/
}
if (argc > 1) {
argv++;
argc--;
if (**argv == '\0') {
dumpabort();
/*NOTREACHED*/
}
disk_dynamic = 0;
}
"Usage: %s [0123456789fustdWwnNDCcbavloS [argument]] filesystem\n"),
myname);
}
if (!filenum)
filenum = 1;
pipeout++;
dumpabort();
/*NOTREACHED*/
}
/*CONSTANTCONDITION*/
} else if (*tape == '+') {
nextdevice();
} else {
/* if not already set, set diskette to default */
nextdevice();
}
error = 1;
}
error = 1;
}
error = 1;
}
if (error) {
dumpabort();
/*NOTREACHED*/
}
/*
* Determine how to default tape size and density
*
* density tape size
* 9-track 1600 bpi (160 bytes/.1") 2300 ft.
* 9-track 6250 bpi (625 bytes/.1") 2300 ft.
*
* Most Sun-2's came with 4 track (20MB) cartridge tape drives,
* while most other machines (Sun-3's and non-Sun's) come with
* 9 track (45MB) cartridge tape drives. Some Sun-2's came with
* 9 track drives, but there is no way for the software to detect
* which drive type is installed. Sigh... We make the gross
* assumption that #ifdef mc68010 will test for a Sun-2.
*
* cartridge 8000 bpi (100 bytes/.1") 425 * tracks ft.
*/
if (density == 0)
if (tracks == 0)
tracks = 9;
if (!bflag) {
if (cartridge)
else if (diskette)
else if (density >= 625)
else
/*
* save ntrec in case we have to change tp_bsize later.
*/
}
if (!diskette) {
if (cartridge)
}
if (host) {
if (cp == (char *)0)
else
cp++;
dumpabort();
/*NOTREACHED*/
}
}
mnttabread(); /* /etc/fstab, /etc/mtab snarfed */
/*
* disk can be either the full special file name,
* the suffix of the special file name,
* the special name missing the leading '/',
* the file system name with or without the leading '/'.
* NB: we attempt to avoid dumping the block device
* (using rawname) because specfs and the vm system
* are not necessarily in sync.
*/
/*
* Attempt to roll the log before doing the dump. There's nothing
* the user can do if we are unable to roll the log, so we'll silently
* ignore failures.
*/
/* Try it again with leading '/'. */
char *slashed;
(void) rl_roll_log(slashed);
}
}
if (dt != 0) {
if (disk_dynamic) {
/* LINTED: disk is not NULL */
}
} else {
#ifdef PARTIAL
/* check for partial filesystem dump */
if (dt != 0) {
if (disk_dynamic)
}
else
#endif /* PARTIAL */
{
"an unlisted file system",
if (disk_dynamic)
disk_dynamic = 1;
}
/*
* If disk == old_disk, then disk_dynamic's state
* does not change.
*/
}
}
if (fi < 0) {
}
dumpabort();
/*NOTREACHED*/
}
sync();
"Warning - super-block on device `%s' is corrupt - run fsck\n"),
disk);
dumpabort();
/*NOTREACHED*/
}
sblock->fs_version);
dumpabort();
/*NOTREACHED*/
}
sblock->fs_version);
dumpabort();
/*NOTREACHED*/
}
/*
* Try to set up for using mmap(2). It only works on the block
* device, but if we can use it, things go somewhat faster. If
* we can't open it, we'll silently fall back to the old method
* unmounted. Dumping a live filesystem this way runs into
* buffer consistency problems. Of course, we don't support
* running dump on a mounted filesystem, but some people do it
* anyway.
*/
}
}
dumpabort();
/*NOTREACHED*/
}
sblock->fs_version);
dumpabort();
/*NOTREACHED*/
}
sblock->fs_version);
dumpabort();
/*NOTREACHED*/
}
if (!doingactive)
allocino();
/* LINTED XXX time truncated - tolerate until tape format changes */
if (snapdate)
if (!printsize) {
if (filesystem != 0)
msgtail("(%.*s:%s) ",
/* LINTED unsigned -> signed cast ok */
}
if (!doingactive) {
} else {
"Internal error: NULL map pointer while re-dumping active files"));
dumpabort();
/*NOTREACHED*/
}
/* retain active map */
}
dumptoarchive = 1;
/*
* Read cylinder group inode-used bitmaps to avoid reading clear inodes.
*/
{
}
/* XXX right-shift clrmap one bit. why? */
*clrp = i >> 1;
}
}
if (!printsize) {
}
ino = 0;
#ifdef PARTIAL
#endif /* PARTIAL */
if (!doingactive)
else
#ifdef PARTIAL
}
#endif /* PARTIAL */
do {
if (!printsize) {
}
nadded = 0;
ino = 0;
} while (nadded);
ino = 0; /* adjust estimated size for shadow inodes */
ino = 0;
freeshad();
if (diskette) {
/* estimate number of floppies */
if (tsize != 0)
} else if (cartridge) {
/*
* Estimate number of tapes, assuming streaming stops at
* the end of each block written, and not in mid-block.
* Assume no erroneous blocks; this can be compensated for
* with an artificially low tape size.
*/
if (tsize != 0)
+
(double)esize /* blocks */
* tenthsperirg) /* 0.1" / streaming-stop */
} else {
/* Estimate number of tapes, for old fashioned 9-track tape */
#ifdef sun
/* sun has long irg's */
#else
#endif
if (tsize != 0)
* (tp_bsize /* bytes / block */
+
(double)esize /* blocks */
* tenthsperirg) /* 0.1" / IRG */
}
etapes++;
/* count the nodemap on each additional tape */
for (i = 1; i < etapes; i++)
/*
* If the above bmapest is called, it changes o_esize and f_esize.
* So we will recalculate esize here anyway to make sure.
* Also, add tape headers and trailer records.
*/
/*
* If the estimated number of tp_bsize tape blocks is greater than
* INT_MAX we have to adjust tp_bsize and ntrec to handle
* the larger dump. esize is an estimate, so we 'fudge'
* INT_MAX a little. If tp_bsize is adjusted, it will be adjusted
* to the size needed for this dump (2048, 4096, 8192, ...)
*/
forceflag++;
"Block factor %d ('b' flag) is too small for this size dump.");
dumpabort();
/*NOTREACHED*/
}
/*
* recalculate esize from:
* o_esize - header tape records
* (f_esize + (num_mult -1)) >> esize_shift - new non-header
* etapes - TS_TAPE records
* ntrec - TS_END records
*
* ntrec is adjusted so a tape record is still 'b' flag
* number of DEV_BSIZE (512) in size
*/
}
if (forceflag != 0) {
"Forcing larger tape block size (%d).\n");
}
alloctape(); /* allocate tape buffers */
/*
* If all we wanted was the size estimate,
* just print it out and exit.
*/
if (printsize) {
Exit(0);
}
if (tsize != 0) {
if (diskette)
"Estimated %lld blocks (%s) on %3.2f diskettes.\n");
else
"Estimated %lld blocks (%s) on %3.2f tapes.\n");
} else {
}
*telapsed = 0;
(void) time(tstart_writing);
/* filmap indicates all non-directory inodes */
{
for (i = 0; i < msiz; i++)
}
/*
* When we receive EOT notification from
* the writer, the signal handler calls
* rollforward and then jumps here.
*/
(void) setjmp(checkpoint_buf);
switch (dumpstate) {
case DS_INIT:
/*
* We get here if a tape error occurred
* after releasing the name lock but before
* the volume containing the last of the
* dir info was completed. We have to start
* all over in this case.
*/
{
"Warning - output error occurred after releasing name lock\n\
\tThe dump will restart\n");
goto restart;
}
/* NOTREACHED */
case DS_START:
case DS_CLRI:
ino = UFSROOTINO;
dumptoarchive = 1;
/* FALLTHROUGH */
case DS_BITS:
ino = UFSROOTINO;
dumptoarchive = 1;
if (!doingverify) {
"Dumping (Pass III) [directories]\n");
}
/* FALLTHROUGH */
case DS_DIRS:
dumptoarchive = 1;
if (!doingverify) {
"Dumping (Pass IV) [regular files]\n");
}
/* FALLTHROUGH */
case DS_FILES:
dumptoarchive = 0;
flushcmds();
/* FALLTHROUGH */
case DS_END:
dumptoarchive = 1;
for (i = 0; i < ntrec; i++) {
spclrec();
}
flusht();
break;
case DS_DONE:
break;
default:
dumpabort();
/*NOTREACHED*/
}
}
if ((! doingactive) && (! active))
trewind();
if (verify && !doingverify) {
}
gettext("%lld blocks (%s) on %ld volumes"),
else
gettext("%lld blocks (%s) on 1 volume"),
* 1000.0));
}
if (archive)
activepass();
goto restart;
}
if (! doingactive)
putitime();
/*NOTREACHED*/
return (0);
}
void
{
char *sigtype;
switch (sig) {
case SIGHUP:
sigtype = "SIGHUP";
break;
case SIGTRAP:
sigtype = "SIGTRAP";
break;
case SIGFPE:
sigtype = "SIGFPE";
break;
case SIGBUS:
abort();
/*NOTREACHED*/
case SIGSEGV:
abort();
/*NOTREACHED*/
case SIGALRM:
sigtype = "SIGALRM";
break;
case SIGTERM:
sigtype = "SIGTERM";
break;
case SIGPIPE:
dumpabort();
/*NOTREACHED*/
default:
sigtype = "SIGNAL";
break;
}
if (pipeout) {
dumpabort();
/*NOTREACHED*/
}
close_rewind();
}
/* Note that returned value is malloc'd if != cp && != NULL */
char *
{
char *dp;
extern char *getfullrawname();
return (cp);
if (dp == 0)
return (0);
if (*dp == '\0') {
return (0);
}
return (cp);
}
return (dp);
}
static char *
{
static char buf[16];
if (blks < 1024)
else
return (buf);
}
#ifdef signal
{
return ((void (*)(int))-1);
return (osa.sa_handler);
}
#endif
static void
{
/* LINTED assigned value never used - kept for documentary purposes */
/* LINTED assigned value never used - kept for documentary purposes */
ino = 0;
/* LINTED assigned value never used - kept for documentary purposes */
pos = 0;
leftover = 0;
}
/*
* timeclock() function, for keeping track of how much time we've spent
* writing to the tape device. it always returns the amount of time
* already spent, in milliseconds. if you pass it a positive, then that's
* telling it that we're writing, so the time counts. if you pass it a
* zero, then that's telling it we're not writing; perhaps we're waiting
* for user input.
*
* a state of -1 resets everything.
*/
{
static int *currentState = NULL;
static struct timeval *clockstart;
#ifdef DEBUG
else if (state > 0)
else
#endif /* DEBUG */
/* if we haven't setup the shared memory, init */
if (currentState == (int *)NULL) {
dumpabort();
/*NOTREACHED*/
}
/*LINTED [mmap always returns an aligned value]*/
if (currentState == (int *)-1) {
"Cannot memory map monitor variables: %s\n"),
dumpabort();
/*NOTREACHED*/
}
/* LINTED currentState is sufficiently aligned */
}
*currentState = 0;
return (0);
}
if (*currentState != 0) {
/* LINTED: result will fit in a time32_t */
/* LINTED: result will fit in a time32_t */
}
if (state != 0)
*currentState = state;
return (*emilli);
}
static int
{
int result = 1;
result = 0;
}
return (result);
}
/*
* Safely open a file or device.
*/
static int
{
int fd;
int working_mode;
int saverr;
char *errtext;
/*
* Don't want to be spoofed into trashing something we
* shouldn't, thus the following rigamarole. If it doesn't
* exist, we create it and proceed. Otherwise, require that
* what's there be a real file with no extraneous links and
* owned by whoever ran us.
*
* The silliness with using both lstat() and fstat() is to avoid
* race-condition games with someone replacing the file with a
* symlink after we've opened it. If there was an flstat(),
* we wouldn't need the fstat().
*
* The initial open with the hard-coded flags is ok even if we
* are intending to open only for reading. If it succeeds,
* then the file did not exist, and we'll synthesize an appropriate
* complaint below. Otherwise, it does exist, so we won't be
* truncating it with the open.
*/
perms)) < 0) {
return (-1);
}
return (-1);
}
"Unexpected condition detected: %s used to exist, but doesn't any longer\n");
}
return (-1);
}
return (-1);
}
return (-1);
}
/*
* Can't just use memcmp(3C), because the access
* time is updated by open(2).
*/
"Unexpected change detected: %s's lstat(2) information changed\n");
return (-1);
}
"Unexpected change detected: %s's stat(2) information changed\n"),
return (-1);
}
/*
* If inode, device, or type are wrong, bail out.
* Note using post_stat instead of post_lstat for the
* S_ISCHR() test. This is to allow the /dev ->
* /devices bit to work, as long as the final target
* is a character device (i.e., raw disk or tape).
*/
"Unexpected condition detected: %s is not a supported device\n"),
return (-1);
} else if (!device &&
"Unexpected condition detected: %s is not a regular file\n"),
return (-1);
}
/*
* Bad link count implies someone's linked our
* target to something else, which we probably
* shouldn't step on.
*/
"Unexpected condition detected: %s must have exactly one link\n"),
return (-1);
}
/*
* Root might make a file, but non-root might
* need to open it. If the permissions let us
* get this far, then let it through.
*/
post_lstat.st_uid != 0) {
"Unsupported condition detected: %s must be owned by uid %ld or 0\n"),
(long)getuid());
return (-1);
}
msg("ftruncate(%s): %s\n",
return (-1);
}
}
} else {
/*
* Didn't exist, but couldn't open it.
*/
return (-1);
}
} else {
/*
* If truncating open succeeded for a read-only open,
* bail out, as we really shouldn't have succeeded.
*/
/* Undo the O_CREAT */
msg("open(%s): %s\n",
return (-1);
}
}
return (fd);
}
/*
* Safely open a file.
*/
int
{
}
/*
* Safely open a device.
*/
int
{
}
/*
* STDIO version of safe_open
*/
FILE *
{
int fd;
int bmode;
/*
* accepts only modes "r", "r+", and "w"
*/
if (smode[0] == 'r') {
}
} else {
smode);
return (NULL);
}
/*
* caller is expected to report error.
*/
if (fd >= 0)
}
void
child_chdir(void)
{
char name[MAXPATHLEN];
if (debug_chdir != NULL) {
debug_chdir, (long)getpid());
}
}