Home | History | Annotate | Line # | Download | only in dump
main.c revision 1.57
      1  1.57  christos /*	$NetBSD: main.c,v 1.57 2004/04/21 01:05:32 christos Exp $	*/
      2   1.6       cgd 
      3   1.1       cgd /*-
      4   1.3   mycroft  * Copyright (c) 1980, 1991, 1993, 1994
      5   1.3   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.56       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.15     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.15     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
     35  1.15     lukem 	The Regents of the University of California.  All rights reserved.\n");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.6       cgd #if 0
     40  1.16     lukem static char sccsid[] = "@(#)main.c	8.6 (Berkeley) 5/1/95";
     41   1.6       cgd #else
     42  1.57  christos __RCSID("$NetBSD: main.c,v 1.57 2004/04/21 01:05:32 christos Exp $");
     43   1.6       cgd #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46   1.3   mycroft #include <sys/param.h>
     47   1.3   mycroft #include <sys/time.h>
     48  1.14     lukem #include <sys/stat.h>
     49  1.14     lukem #include <sys/mount.h>
     50  1.14     lukem 
     51  1.16     lukem #include <ufs/ufs/dinode.h>
     52   1.3   mycroft #include <ufs/ffs/fs.h>
     53  1.18    bouyer #include <ufs/ffs/ffs_extern.h>
     54   1.3   mycroft 
     55   1.1       cgd #include <protocols/dumprestore.h>
     56   1.3   mycroft 
     57   1.3   mycroft #include <ctype.h>
     58   1.5   mycroft #include <err.h>
     59   1.3   mycroft #include <errno.h>
     60   1.1       cgd #include <fcntl.h>
     61   1.1       cgd #include <fstab.h>
     62   1.3   mycroft #include <signal.h>
     63   1.1       cgd #include <stdio.h>
     64   1.1       cgd #include <stdlib.h>
     65   1.1       cgd #include <string.h>
     66  1.10     lukem #include <time.h>
     67   1.3   mycroft #include <unistd.h>
     68   1.3   mycroft 
     69   1.1       cgd #include "dump.h"
     70   1.1       cgd #include "pathnames.h"
     71   1.1       cgd 
     72  1.44     lukem int	timestamp;		/* print message timestamps */
     73  1.27       scw int	notify;			/* notify operator flag */
     74  1.27       scw int	blockswritten;		/* number of blocks written on current tape */
     75  1.27       scw int	tapeno;			/* current tape number */
     76  1.27       scw int	density;		/* density in bytes/0.1" */
     77  1.17     lukem int	ntrec = NTREC;		/* # tape blocks in each tape record */
     78  1.27       scw int	cartridge;		/* Assume non-cartridge tape */
     79  1.17     lukem long	dev_bsize = 1;		/* recalculated below */
     80  1.27       scw long	blocksperfile;		/* output blocks per file */
     81  1.27       scw char	*host;			/* remote host (if any) */
     82  1.22    bouyer int	readcache = -1;		/* read cache size (in readblksize blks) */
     83  1.22    bouyer int	readblksize = 32 * 1024; /* read block size */
     84  1.39     blymn char    default_time_string[] = "%T %Z"; /* default timestamp string */
     85  1.39     blymn char    *time_string = default_time_string; /* timestamp string */
     86   1.1       cgd 
     87  1.31     lukem int	main(int, char *[]);
     88  1.31     lukem static long numarg(char *, long, long);
     89  1.31     lukem static void obsolete(int *, char **[]);
     90  1.31     lukem static void usage(void);
     91   1.3   mycroft 
     92   1.3   mycroft int
     93  1.31     lukem main(int argc, char *argv[])
     94   1.1       cgd {
     95  1.11     lukem 	ino_t ino;
     96  1.45     lukem 	int dirty;
     97  1.55      fvdl 	union dinode *dp;
     98  1.33     lukem 	struct fstab *dt;
     99  1.57  christos 	struct statvfs *mntinfo, fsbuf;
    100  1.11     lukem 	char *map;
    101  1.11     lukem 	int ch;
    102  1.38     lukem 	int i, anydirskipped, bflag = 0, Tflag = 0, Fflag = 0, honorlevel = 1;
    103   1.1       cgd 	ino_t maxino;
    104  1.18    bouyer 	time_t tnow, date;
    105  1.33     lukem 	int dirc;
    106  1.33     lukem 	char *mountpoint;
    107  1.17     lukem 	int just_estimate = 0;
    108  1.21     lukem 	char labelstr[LBLSIZE];
    109  1.39     blymn 	char *new_time_format;
    110  1.45     lukem 
    111   1.1       cgd 	spcl.c_date = 0;
    112  1.47     lukem 	(void)time(&tnow);
    113  1.47     lukem 	spcl.c_date = tnow;
    114  1.39     blymn 	tzset(); /* set up timezone for strftime */
    115  1.39     blymn 	if ((new_time_format = getenv("TIMEFORMAT")) != NULL)
    116  1.39     blymn 		time_string = new_time_format;
    117  1.45     lukem 
    118   1.1       cgd 	tsize = 0;	/* Default later, based on 'c' option for cart tapes */
    119   1.7       mrg 	if ((tape = getenv("TAPE")) == NULL)
    120   1.7       mrg 		tape = _PATH_DEFTAPE;
    121   1.1       cgd 	dumpdates = _PATH_DUMPDATES;
    122   1.1       cgd 	temp = _PATH_DTMP;
    123  1.21     lukem 	strcpy(labelstr, "none");	/* XXX safe strcpy. */
    124   1.1       cgd 	if (TP_BSIZE / DEV_BSIZE == 0 || TP_BSIZE % DEV_BSIZE != 0)
    125   1.1       cgd 		quit("TP_BSIZE must be a multiple of DEV_BSIZE\n");
    126   1.1       cgd 	level = '0';
    127  1.39     blymn 	timestamp = 0;
    128  1.45     lukem 
    129   1.5   mycroft 	if (argc < 2)
    130   1.5   mycroft 		usage();
    131   1.5   mycroft 
    132   1.5   mycroft 	obsolete(&argc, &argv);
    133  1.17     lukem 	while ((ch = getopt(argc, argv,
    134  1.50    bouyer 	    "0123456789aB:b:cd:eFf:h:k:l:L:nr:s:StT:uWw")) != -1)
    135   1.5   mycroft 		switch (ch) {
    136   1.5   mycroft 		/* dump level */
    137   1.5   mycroft 		case '0': case '1': case '2': case '3': case '4':
    138   1.5   mycroft 		case '5': case '6': case '7': case '8': case '9':
    139   1.5   mycroft 			level = ch;
    140   1.3   mycroft 			break;
    141   1.1       cgd 
    142  1.49     lukem 		case 'a':		/* `auto-size', Write to EOM. */
    143  1.49     lukem 			unlimited = 1;
    144  1.49     lukem 			break;
    145  1.49     lukem 
    146   1.5   mycroft 		case 'B':		/* blocks per output file */
    147   1.5   mycroft 			blocksperfile = numarg("blocks per file", 1L, 0L);
    148   1.5   mycroft 			break;
    149   1.1       cgd 
    150   1.5   mycroft 		case 'b':		/* blocks per tape write */
    151   1.5   mycroft 			ntrec = numarg("blocks per write", 1L, 1000L);
    152   1.9     mikel 			bflag = 1;
    153   1.5   mycroft 			break;
    154   1.1       cgd 
    155   1.5   mycroft 		case 'c':		/* Tape is cart. not 9-track */
    156   1.5   mycroft 			cartridge = 1;
    157   1.3   mycroft 			break;
    158   1.1       cgd 
    159   1.1       cgd 		case 'd':		/* density, in bits per inch */
    160   1.5   mycroft 			density = numarg("density", 10L, 327670L) / 10;
    161   1.1       cgd 			if (density >= 625 && !bflag)
    162   1.1       cgd 				ntrec = HIGHDENSITYTREC;
    163  1.28      tron 			break;
    164  1.28      tron 
    165  1.38     lukem 		case 'e':		/* eject full tapes */
    166  1.28      tron 			eflag = 1;
    167   1.3   mycroft 			break;
    168   1.1       cgd 
    169  1.38     lukem 		case 'F':		/* files-to-dump is an fs image */
    170  1.38     lukem 			Fflag = 1;
    171  1.38     lukem 			break;
    172  1.38     lukem 
    173   1.5   mycroft 		case 'f':		/* output file */
    174   1.5   mycroft 			tape = optarg;
    175   1.5   mycroft 			break;
    176   1.5   mycroft 
    177   1.5   mycroft 		case 'h':
    178   1.5   mycroft 			honorlevel = numarg("honor level", 0L, 10L);
    179   1.5   mycroft 			break;
    180   1.5   mycroft 
    181  1.22    bouyer 		case 'k':
    182  1.22    bouyer 			readblksize = numarg("read block size", 0, 64) * 1024;
    183  1.22    bouyer 			break;
    184  1.22    bouyer 
    185  1.41    bouyer 		case 'l':		/* autoload after eject full tapes */
    186  1.41    bouyer 			eflag = 1;
    187  1.50    bouyer 			lflag = numarg("timeout (in seconds)", 1, 0);
    188  1.41    bouyer 			break;
    189  1.41    bouyer 
    190  1.21     lukem 		case 'L':
    191  1.21     lukem 			/*
    192  1.21     lukem 			 * Note that although there are LBLSIZE characters,
    193  1.21     lukem 			 * the last must be '\0', so the limit on strlen()
    194  1.21     lukem 			 * is really LBLSIZE-1.
    195  1.21     lukem 			 */
    196  1.51    itojun 			if (strlcpy(labelstr, optarg, sizeof(labelstr))
    197  1.51    itojun 			    >= sizeof(labelstr)) {
    198  1.21     lukem 				msg(
    199  1.53    itojun 		"WARNING Label `%s' is larger than limit of %lu characters.\n",
    200  1.53    itojun 				    optarg,
    201  1.53    itojun 				    (unsigned long)sizeof(labelstr) - 1);
    202  1.21     lukem 				msg("WARNING: Using truncated label `%s'.\n",
    203  1.21     lukem 				    labelstr);
    204  1.21     lukem 			}
    205  1.21     lukem 			break;
    206   1.5   mycroft 		case 'n':		/* notify operators */
    207   1.5   mycroft 			notify = 1;
    208   1.5   mycroft 			break;
    209   1.5   mycroft 
    210  1.22    bouyer 		case 'r':		/* read cache size */
    211  1.22    bouyer 			readcache = numarg("read cache size", 0, 512);
    212  1.22    bouyer 			break;
    213  1.45     lukem 
    214   1.1       cgd 		case 's':		/* tape size, feet */
    215   1.5   mycroft 			tsize = numarg("tape size", 1L, 0L) * 12 * 10;
    216   1.3   mycroft 			break;
    217   1.1       cgd 
    218  1.17     lukem 		case 'S':		/* exit after estimating # of tapes */
    219  1.17     lukem 			just_estimate = 1;
    220  1.17     lukem 			break;
    221  1.17     lukem 
    222  1.39     blymn 		case 't':
    223  1.39     blymn 			timestamp = 1;
    224  1.39     blymn 			break;
    225  1.39     blymn 
    226   1.1       cgd 		case 'T':		/* time of last dump */
    227   1.5   mycroft 			spcl.c_ddate = unctime(optarg);
    228   1.1       cgd 			if (spcl.c_ddate < 0) {
    229   1.3   mycroft 				(void)fprintf(stderr, "bad time \"%s\"\n",
    230   1.5   mycroft 				    optarg);
    231  1.48     lukem 				exit(X_STARTUP);
    232   1.1       cgd 			}
    233   1.3   mycroft 			Tflag = 1;
    234   1.1       cgd 			lastlevel = '?';
    235   1.3   mycroft 			break;
    236   1.1       cgd 
    237   1.1       cgd 		case 'u':		/* update /etc/dumpdates */
    238   1.3   mycroft 			uflag = 1;
    239   1.3   mycroft 			break;
    240   1.1       cgd 
    241   1.5   mycroft 		case 'W':		/* what to do */
    242   1.5   mycroft 		case 'w':
    243   1.5   mycroft 			lastdump(ch);
    244  1.48     lukem 			exit(X_FINOK);	/* do nothing else */
    245   1.1       cgd 
    246   1.1       cgd 		default:
    247   1.5   mycroft 			usage();
    248   1.1       cgd 		}
    249   1.5   mycroft 	argc -= optind;
    250   1.5   mycroft 	argv += optind;
    251   1.5   mycroft 
    252   1.1       cgd 	if (argc < 1) {
    253  1.38     lukem 		(void)fprintf(stderr,
    254  1.38     lukem 		    "Must specify disk or image, or file list\n");
    255  1.48     lukem 		exit(X_STARTUP);
    256   1.1       cgd 	}
    257  1.14     lukem 
    258  1.33     lukem 
    259  1.14     lukem 	/*
    260  1.14     lukem 	 *	determine if disk is a subdirectory, and setup appropriately
    261  1.14     lukem 	 */
    262  1.33     lukem 	getfstab();		/* /etc/fstab snarfed */
    263  1.33     lukem 	disk = NULL;
    264  1.33     lukem 	mountpoint = NULL;
    265  1.33     lukem 	dirc = 0;
    266  1.14     lukem 	for (i = 0; i < argc; i++) {
    267  1.14     lukem 		struct stat sb;
    268  1.14     lukem 
    269  1.33     lukem 		if (lstat(argv[i], &sb) == -1)
    270  1.33     lukem 			quit("Cannot stat %s: %s\n", argv[i], strerror(errno));
    271  1.38     lukem 		if (Fflag || S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) {
    272  1.33     lukem 			disk = argv[i];
    273  1.33     lukem  multicheck:
    274  1.33     lukem 			if (dirc != 0)
    275  1.33     lukem 				quit(
    276  1.38     lukem 	"Can't dump a disk or image at the same time as a file list\n");
    277  1.14     lukem 			break;
    278  1.14     lukem 		}
    279  1.33     lukem 		if ((dt = fstabsearch(argv[i])) != NULL) {
    280  1.33     lukem 			disk = dt->fs_spec;
    281  1.34   hannken 			mountpoint = xstrdup(dt->fs_file);
    282  1.33     lukem 			goto multicheck;
    283  1.14     lukem 		}
    284  1.57  christos 		if (statvfs(argv[i], &fsbuf) == -1)
    285  1.57  christos 			quit("Cannot statvfs %s: %s\n", argv[i],
    286  1.33     lukem 			    strerror(errno));
    287  1.33     lukem 		disk = fsbuf.f_mntfromname;
    288  1.33     lukem 		if (strcmp(argv[i], fsbuf.f_mntonname) == 0)
    289  1.33     lukem 			goto multicheck;
    290  1.33     lukem 		if (mountpoint == NULL) {
    291  1.33     lukem 			mountpoint = xstrdup(fsbuf.f_mntonname);
    292  1.14     lukem 			if (uflag) {
    293  1.14     lukem 				msg("Ignoring u flag for subdir dump\n");
    294  1.14     lukem 				uflag = 0;
    295  1.14     lukem 			}
    296  1.14     lukem 			if (level > '0') {
    297  1.14     lukem 				msg("Subdir dump is done at level 0\n");
    298  1.14     lukem 				level = '0';
    299  1.14     lukem 			}
    300  1.33     lukem 			msg("Dumping sub files/directories from %s\n",
    301  1.33     lukem 			    mountpoint);
    302  1.14     lukem 		} else {
    303  1.33     lukem 			if (strcmp(mountpoint, fsbuf.f_mntonname) != 0)
    304  1.33     lukem 				quit("%s is not on %s\n", argv[i], mountpoint);
    305  1.14     lukem 		}
    306  1.14     lukem 		msg("Dumping file/directory %s\n", argv[i]);
    307  1.33     lukem 		dirc++;
    308  1.14     lukem 	}
    309  1.33     lukem 	if (mountpoint)
    310  1.33     lukem 		free(mountpoint);
    311  1.33     lukem 
    312  1.33     lukem 	if (dirc == 0) {
    313  1.33     lukem 		argv++;
    314  1.14     lukem 		if (argc != 1) {
    315  1.14     lukem 			(void)fprintf(stderr, "Excess arguments to dump:");
    316  1.14     lukem 			while (--argc)
    317  1.14     lukem 				(void)fprintf(stderr, " %s", *argv++);
    318  1.14     lukem 			(void)fprintf(stderr, "\n");
    319  1.48     lukem 			exit(X_STARTUP);
    320  1.14     lukem 		}
    321   1.1       cgd 	}
    322   1.1       cgd 	if (Tflag && uflag) {
    323  1.44     lukem 		(void)fprintf(stderr,
    324   1.1       cgd 		    "You cannot use the T and u flags together.\n");
    325  1.48     lukem 		exit(X_STARTUP);
    326   1.1       cgd 	}
    327   1.1       cgd 	if (strcmp(tape, "-") == 0) {
    328   1.1       cgd 		pipeout++;
    329   1.1       cgd 		tape = "standard output";
    330   1.1       cgd 	}
    331   1.1       cgd 
    332   1.1       cgd 	if (blocksperfile)
    333   1.1       cgd 		blocksperfile = blocksperfile / ntrec * ntrec; /* round down */
    334  1.49     lukem 	else if (!unlimited) {
    335   1.1       cgd 		/*
    336   1.1       cgd 		 * Determine how to default tape size and density
    337   1.1       cgd 		 *
    338  1.44     lukem 		 *		density				tape size
    339   1.1       cgd 		 * 9-track	1600 bpi (160 bytes/.1")	2300 ft.
    340   1.1       cgd 		 * 9-track	6250 bpi (625 bytes/.1")	2300 ft.
    341   1.1       cgd 		 * cartridge	8000 bpi (100 bytes/.1")	1700 ft.
    342   1.1       cgd 		 *						(450*4 - slop)
    343   1.1       cgd 		 */
    344   1.1       cgd 		if (density == 0)
    345   1.1       cgd 			density = cartridge ? 100 : 160;
    346   1.1       cgd 		if (tsize == 0)
    347   1.1       cgd 			tsize = cartridge ? 1700L*120L : 2300L*120L;
    348   1.1       cgd 	}
    349   1.1       cgd 
    350   1.4   mycroft 	if (strchr(tape, ':')) {
    351   1.1       cgd 		host = tape;
    352   1.4   mycroft 		tape = strchr(host, ':');
    353   1.3   mycroft 		*tape++ = '\0';
    354   1.1       cgd #ifdef RDUMP
    355   1.1       cgd 		if (rmthost(host) == 0)
    356  1.48     lukem 			exit(X_STARTUP);
    357   1.1       cgd #else
    358   1.3   mycroft 		(void)fprintf(stderr, "remote dump not enabled\n");
    359  1.48     lukem 		exit(X_STARTUP);
    360   1.1       cgd #endif
    361   1.1       cgd 	}
    362   1.1       cgd 
    363   1.1       cgd 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
    364   1.1       cgd 		signal(SIGHUP, sig);
    365   1.1       cgd 	if (signal(SIGTRAP, SIG_IGN) != SIG_IGN)
    366   1.1       cgd 		signal(SIGTRAP, sig);
    367   1.1       cgd 	if (signal(SIGFPE, SIG_IGN) != SIG_IGN)
    368   1.1       cgd 		signal(SIGFPE, sig);
    369   1.1       cgd 	if (signal(SIGBUS, SIG_IGN) != SIG_IGN)
    370   1.1       cgd 		signal(SIGBUS, sig);
    371  1.55      fvdl #if 0
    372   1.1       cgd 	if (signal(SIGSEGV, SIG_IGN) != SIG_IGN)
    373   1.1       cgd 		signal(SIGSEGV, sig);
    374  1.55      fvdl #endif
    375   1.1       cgd 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
    376   1.1       cgd 		signal(SIGTERM, sig);
    377   1.1       cgd 	if (signal(SIGINT, interrupt) == SIG_IGN)
    378   1.1       cgd 		signal(SIGINT, SIG_IGN);
    379  1.14     lukem 
    380   1.1       cgd 	/*
    381  1.33     lukem 	 *	disk can be either the full special file name, or
    382  1.33     lukem 	 *	the file system name.
    383   1.1       cgd 	 */
    384  1.33     lukem 	mountpoint = NULL;
    385  1.33     lukem 	if ((dt = fstabsearch(disk)) != NULL) {
    386   1.1       cgd 		disk = rawname(dt->fs_spec);
    387  1.33     lukem 		mountpoint = dt->fs_file;
    388  1.33     lukem 		msg("Found %s on %s in %s\n", disk, mountpoint, _PATH_FSTAB);
    389  1.33     lukem 	} else if ((mntinfo = mntinfosearch(disk)) != NULL) {
    390  1.33     lukem 		disk = rawname(mntinfo->f_mntfromname);
    391  1.33     lukem 		mountpoint = mntinfo->f_mntonname;
    392  1.33     lukem 		msg("Found %s on %s in mount table\n", disk, mountpoint);
    393  1.33     lukem 	}
    394  1.33     lukem 	if (mountpoint != NULL) {
    395  1.33     lukem 		if (dirc != 0)
    396  1.51    itojun 			(void)snprintf(spcl.c_filesys, sizeof(spcl.c_filesys),
    397  1.33     lukem 			    "a subset of %s", mountpoint);
    398  1.14     lukem 		else
    399  1.51    itojun 			(void)strlcpy(spcl.c_filesys, mountpoint,
    400  1.51    itojun 			    sizeof(spcl.c_filesys));
    401  1.38     lukem 	} else if (Fflag) {
    402  1.51    itojun 		(void)strlcpy(spcl.c_filesys, "a file system image",
    403  1.51    itojun 		    sizeof(spcl.c_filesys));
    404   1.1       cgd 	} else {
    405  1.51    itojun 		(void)strlcpy(spcl.c_filesys, "an unlisted file system",
    406  1.51    itojun 		    sizeof(spcl.c_filesys));
    407   1.1       cgd 	}
    408  1.51    itojun 	(void)strlcpy(spcl.c_dev, disk, sizeof(spcl.c_dev));
    409  1.51    itojun 	(void)strlcpy(spcl.c_label, labelstr, sizeof(spcl.c_label));
    410  1.51    itojun 	(void)gethostname(spcl.c_host, sizeof(spcl.c_host));
    411  1.19       mrg 	spcl.c_host[sizeof(spcl.c_host) - 1] = '\0';
    412  1.18    bouyer 
    413  1.18    bouyer 	if ((diskfd = open(disk, O_RDONLY)) < 0) {
    414  1.18    bouyer 		msg("Cannot open %s\n", disk);
    415  1.48     lukem 		exit(X_STARTUP);
    416  1.18    bouyer 	}
    417  1.18    bouyer 	sync();
    418  1.25  perseant 
    419  1.25  perseant 	needswap = fs_read_sblock(sblock_buf);
    420  1.18    bouyer 
    421  1.18    bouyer 	spcl.c_level = iswap32(level - '0');
    422  1.18    bouyer 	spcl.c_type = iswap32(TS_TAPE);
    423  1.18    bouyer 	spcl.c_date = iswap32(spcl.c_date);
    424  1.18    bouyer 	spcl.c_ddate = iswap32(spcl.c_ddate);
    425   1.1       cgd 	if (!Tflag)
    426  1.44     lukem 		getdumptime();		/* /etc/dumpdates snarfed */
    427   1.1       cgd 
    428  1.18    bouyer 	date = iswap32(spcl.c_date);
    429   1.1       cgd 	msg("Date of this level %c dump: %s", level,
    430  1.18    bouyer 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    431  1.18    bouyer 	date = iswap32(spcl.c_ddate);
    432   1.1       cgd  	msg("Date of last level %c dump: %s", lastlevel,
    433  1.18    bouyer 		spcl.c_ddate == 0 ? "the epoch\n" : ctime(&date));
    434  1.17     lukem 	msg("Dumping ");
    435  1.33     lukem 	if (dirc != 0)
    436  1.17     lukem 		msgtail("a subset of ");
    437  1.17     lukem 	msgtail("%s (%s) ", disk, spcl.c_filesys);
    438   1.1       cgd 	if (host)
    439   1.1       cgd 		msgtail("to %s on host %s\n", tape, host);
    440   1.1       cgd 	else
    441   1.1       cgd 		msgtail("to %s\n", tape);
    442  1.21     lukem 	msg("Label: %s\n", labelstr);
    443   1.1       cgd 
    444  1.25  perseant 	ufsib = fs_parametrize();
    445  1.25  perseant 
    446   1.1       cgd 	dev_bshift = ffs(dev_bsize) - 1;
    447   1.1       cgd 	if (dev_bsize != (1 << dev_bshift))
    448  1.26    briggs 		quit("dev_bsize (%ld) is not a power of 2", dev_bsize);
    449   1.1       cgd 	tp_bshift = ffs(TP_BSIZE) - 1;
    450   1.1       cgd 	if (TP_BSIZE != (1 << tp_bshift))
    451   1.1       cgd 		quit("TP_BSIZE (%d) is not a power of 2", TP_BSIZE);
    452  1.25  perseant 	maxino = fs_maxino();
    453   1.3   mycroft 	mapsize = roundup(howmany(maxino, NBBY), TP_BSIZE);
    454  1.33     lukem 	usedinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    455  1.33     lukem 	dumpdirmap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    456  1.33     lukem 	dumpinomap = (char *)xcalloc((unsigned) mapsize, sizeof(char));
    457   1.1       cgd 	tapesize = 3 * (howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    458   1.1       cgd 
    459  1.18    bouyer 	nonodump = iswap32(spcl.c_level) < honorlevel;
    460   1.3   mycroft 
    461  1.22    bouyer 	initcache(readcache, readblksize);
    462  1.45     lukem 
    463  1.14     lukem 	(void)signal(SIGINFO, statussig);
    464  1.14     lukem 
    465   1.1       cgd 	msg("mapping (Pass I) [regular files]\n");
    466  1.33     lukem 	anydirskipped = mapfiles(maxino, &tapesize, mountpoint,
    467  1.33     lukem 	    (dirc ? argv : NULL));
    468   1.1       cgd 
    469   1.1       cgd 	msg("mapping (Pass II) [directories]\n");
    470   1.1       cgd 	while (anydirskipped) {
    471   1.1       cgd 		anydirskipped = mapdirs(maxino, &tapesize);
    472   1.1       cgd 	}
    473   1.1       cgd 
    474  1.49     lukem 	if (pipeout || unlimited) {
    475   1.1       cgd 		tapesize += 10;	/* 10 trailer blocks */
    476  1.55      fvdl 		msg("estimated %llu tape blocks.\n",
    477  1.55      fvdl 		    (unsigned long long)tapesize);
    478   1.3   mycroft 	} else {
    479   1.3   mycroft 		double fetapes;
    480   1.3   mycroft 
    481   1.1       cgd 		if (blocksperfile)
    482   1.3   mycroft 			fetapes = (double) tapesize / blocksperfile;
    483   1.1       cgd 		else if (cartridge) {
    484   1.1       cgd 			/* Estimate number of tapes, assuming streaming stops at
    485   1.1       cgd 			   the end of each block written, and not in mid-block.
    486   1.1       cgd 			   Assume no erroneous blocks; this can be compensated
    487   1.1       cgd 			   for with an artificially low tape size. */
    488  1.45     lukem 			fetapes =
    489  1.48     lukem 			(	  (double) tapesize	/* blocks */
    490   1.1       cgd 				* TP_BSIZE	/* bytes/block */
    491   1.1       cgd 				* (1.0/density)	/* 0.1" / byte */
    492   1.1       cgd 			  +
    493  1.48     lukem 				  (double) tapesize	/* blocks */
    494   1.1       cgd 				* (1.0/ntrec)	/* streaming-stops per block */
    495   1.1       cgd 				* 15.48		/* 0.1" / streaming-stop */
    496   1.1       cgd 			) * (1.0 / tsize );	/* tape / 0.1" */
    497   1.1       cgd 		} else {
    498   1.1       cgd 			/* Estimate number of tapes, for old fashioned 9-track
    499   1.1       cgd 			   tape */
    500   1.1       cgd 			int tenthsperirg = (density == 625) ? 3 : 7;
    501   1.1       cgd 			fetapes =
    502   1.1       cgd 			(	  tapesize	/* blocks */
    503   1.1       cgd 				* TP_BSIZE	/* bytes / block */
    504   1.1       cgd 				* (1.0/density)	/* 0.1" / byte */
    505   1.1       cgd 			  +
    506   1.1       cgd 				  tapesize	/* blocks */
    507   1.1       cgd 				* (1.0/ntrec)	/* IRG's / block */
    508   1.1       cgd 				* tenthsperirg	/* 0.1" / IRG */
    509   1.1       cgd 			) * (1.0 / tsize );	/* tape / 0.1" */
    510   1.1       cgd 		}
    511   1.1       cgd 		etapes = fetapes;		/* truncating assignment */
    512   1.1       cgd 		etapes++;
    513   1.1       cgd 		/* count the dumped inodes map on each additional tape */
    514   1.1       cgd 		tapesize += (etapes - 1) *
    515   1.1       cgd 			(howmany(mapsize * sizeof(char), TP_BSIZE) + 1);
    516   1.1       cgd 		tapesize += etapes + 10;	/* headers + 10 trailer blks */
    517  1.55      fvdl 		msg("estimated %llu tape blocks on %3.2f tape(s).\n",
    518  1.55      fvdl 		    (unsigned long long)tapesize, fetapes);
    519   1.3   mycroft 	}
    520  1.17     lukem 	/*
    521  1.17     lukem 	 * If the user only wants an estimate of the number of
    522  1.17     lukem 	 * tapes, exit now.
    523  1.17     lukem 	 */
    524  1.17     lukem 	if (just_estimate)
    525  1.48     lukem 		exit(X_FINOK);
    526   1.1       cgd 
    527   1.1       cgd 	/*
    528   1.3   mycroft 	 * Allocate tape buffer.
    529   1.1       cgd 	 */
    530   1.1       cgd 	if (!alloctape())
    531   1.1       cgd 		quit("can't allocate tape buffers - try a smaller blocking factor.\n");
    532   1.1       cgd 
    533   1.1       cgd 	startnewtape(1);
    534   1.3   mycroft 	(void)time((time_t *)&(tstart_writing));
    535  1.10     lukem 	xferrate = 0;
    536   1.3   mycroft 	dumpmap(usedinomap, TS_CLRI, maxino - 1);
    537   1.1       cgd 
    538   1.1       cgd 	msg("dumping (Pass III) [directories]\n");
    539   1.3   mycroft 	dirty = 0;		/* XXX just to get gcc to shut up */
    540   1.3   mycroft 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    541   1.3   mycroft 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    542   1.1       cgd 			dirty = *map++;
    543   1.1       cgd 		else
    544   1.1       cgd 			dirty >>= 1;
    545   1.1       cgd 		if ((dirty & 1) == 0)
    546   1.1       cgd 			continue;
    547   1.1       cgd 		/*
    548   1.1       cgd 		 * Skip directory inodes deleted and maybe reallocated
    549   1.1       cgd 		 */
    550   1.1       cgd 		dp = getino(ino);
    551  1.55      fvdl 		if ((DIP(dp, mode) & IFMT) != IFDIR)
    552   1.1       cgd 			continue;
    553   1.3   mycroft 		(void)dumpino(dp, ino);
    554   1.1       cgd 	}
    555   1.1       cgd 
    556   1.1       cgd 	msg("dumping (Pass IV) [regular files]\n");
    557   1.3   mycroft 	for (map = dumpinomap, ino = 1; ino < maxino; ino++) {
    558   1.3   mycroft 		int mode;
    559   1.3   mycroft 
    560   1.3   mycroft 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    561   1.1       cgd 			dirty = *map++;
    562   1.1       cgd 		else
    563   1.1       cgd 			dirty >>= 1;
    564   1.1       cgd 		if ((dirty & 1) == 0)
    565   1.1       cgd 			continue;
    566   1.1       cgd 		/*
    567   1.1       cgd 		 * Skip inodes deleted and reallocated as directories.
    568   1.1       cgd 		 */
    569   1.1       cgd 		dp = getino(ino);
    570  1.55      fvdl 		mode = DIP(dp, mode) & IFMT;
    571   1.3   mycroft 		if (mode == IFDIR)
    572   1.1       cgd 			continue;
    573   1.3   mycroft 		(void)dumpino(dp, ino);
    574   1.1       cgd 	}
    575   1.1       cgd 
    576  1.18    bouyer 	spcl.c_type = iswap32(TS_END);
    577   1.1       cgd 	for (i = 0; i < ntrec; i++)
    578   1.3   mycroft 		writeheader(maxino - 1);
    579   1.1       cgd 	if (pipeout)
    580  1.26    briggs 		msg("%d tape blocks\n",iswap32(spcl.c_tapea));
    581   1.1       cgd 	else
    582  1.26    briggs 		msg("%d tape blocks on %d volume%s\n",
    583  1.18    bouyer 		    iswap32(spcl.c_tapea), iswap32(spcl.c_volume),
    584  1.18    bouyer 		    (iswap32(spcl.c_volume) == 1) ? "" : "s");
    585  1.10     lukem 	tnow = do_stats();
    586  1.18    bouyer 	date = iswap32(spcl.c_date);
    587  1.10     lukem 	msg("Date of this level %c dump: %s", level,
    588  1.18    bouyer 		spcl.c_date == 0 ? "the epoch\n" : ctime(&date));
    589  1.10     lukem 	msg("Date this dump completed:  %s", ctime(&tnow));
    590  1.26    briggs 	msg("Average transfer rate: %d KB/s\n", xferrate / tapeno);
    591   1.1       cgd 	putdumptime();
    592  1.29      tron 	trewind(0);
    593  1.48     lukem 	broadcast("DUMP IS DONE!\a\a\n");
    594   1.1       cgd 	msg("DUMP IS DONE\n");
    595   1.1       cgd 	Exit(X_FINOK);
    596   1.1       cgd 	/* NOTREACHED */
    597  1.15     lukem 	exit(X_FINOK);		/* XXX: to satisfy gcc */
    598   1.1       cgd }
    599   1.1       cgd 
    600   1.5   mycroft static void
    601  1.31     lukem usage(void)
    602   1.5   mycroft {
    603  1.49     lukem 	const char *prog = getprogname();
    604   1.5   mycroft 
    605  1.49     lukem 	(void)fprintf(stderr,
    606  1.50    bouyer "usage: %s [-0123456789aceFnStu] [-B records] [-b blocksize]\n"
    607  1.49     lukem "            [-d density] [-f file] [-h level] [-k read-blocksize]\n"
    608  1.50    bouyer "            [-L label] [-l timeout] [-r read-cache] [-s feet] [-T date] files-to-dump\n"
    609  1.49     lukem "       %s [-W | -w]\n", prog, prog);
    610  1.48     lukem 	exit(X_STARTUP);
    611   1.5   mycroft }
    612   1.5   mycroft 
    613   1.3   mycroft /*
    614   1.3   mycroft  * Pick up a numeric argument.  It must be nonnegative and in the given
    615   1.3   mycroft  * range (except that a vmax of 0 means unlimited).
    616   1.3   mycroft  */
    617   1.3   mycroft static long
    618  1.32     lukem numarg(char *meaning, long vmin, long vmax)
    619   1.3   mycroft {
    620   1.5   mycroft 	char *p;
    621   1.3   mycroft 	long val;
    622   1.3   mycroft 
    623   1.5   mycroft 	val = strtol(optarg, &p, 10);
    624   1.5   mycroft 	if (*p)
    625  1.48     lukem 		errx(X_STARTUP, "illegal %s -- %s", meaning, optarg);
    626   1.3   mycroft 	if (val < vmin || (vmax && val > vmax))
    627  1.48     lukem 		errx(X_STARTUP, "%s must be between %ld and %ld",
    628  1.48     lukem 		    meaning, vmin, vmax);
    629   1.3   mycroft 	return (val);
    630   1.3   mycroft }
    631   1.3   mycroft 
    632   1.1       cgd void
    633  1.32     lukem sig(int signo)
    634   1.1       cgd {
    635  1.32     lukem 
    636   1.1       cgd 	switch(signo) {
    637   1.1       cgd 	case SIGALRM:
    638   1.1       cgd 	case SIGBUS:
    639   1.1       cgd 	case SIGFPE:
    640   1.1       cgd 	case SIGHUP:
    641   1.1       cgd 	case SIGTERM:
    642   1.1       cgd 	case SIGTRAP:
    643   1.1       cgd 		if (pipeout)
    644   1.1       cgd 			quit("Signal on pipe: cannot recover\n");
    645  1.35       mrg 		msg("Rewriting attempted as response to signal %s.\n", sys_siglist[signo]);
    646   1.3   mycroft 		(void)fflush(stderr);
    647   1.3   mycroft 		(void)fflush(stdout);
    648   1.1       cgd 		close_rewind();
    649   1.3   mycroft 		exit(X_REWRITE);
    650   1.1       cgd 		/* NOTREACHED */
    651   1.1       cgd 	case SIGSEGV:
    652   1.1       cgd 		msg("SIGSEGV: ABORTING!\n");
    653   1.3   mycroft 		(void)signal(SIGSEGV, SIG_DFL);
    654   1.3   mycroft 		(void)kill(0, SIGSEGV);
    655   1.1       cgd 		/* NOTREACHED */
    656   1.1       cgd 	}
    657   1.1       cgd }
    658   1.1       cgd 
    659   1.1       cgd char *
    660  1.32     lukem rawname(char *cp)
    661   1.1       cgd {
    662   1.1       cgd 	static char rawbuf[MAXPATHLEN];
    663   1.4   mycroft 	char *dp = strrchr(cp, '/');
    664   1.1       cgd 
    665   1.3   mycroft 	if (dp == NULL)
    666   1.3   mycroft 		return (NULL);
    667   1.3   mycroft 	*dp = '\0';
    668  1.12       mrg 	(void)snprintf(rawbuf, sizeof rawbuf, "%s/r%s", cp, dp + 1);
    669   1.1       cgd 	*dp = '/';
    670   1.1       cgd 	return (rawbuf);
    671   1.1       cgd }
    672   1.1       cgd 
    673   1.5   mycroft /*
    674   1.5   mycroft  * obsolete --
    675   1.5   mycroft  *	Change set of key letters and ordered arguments into something
    676   1.5   mycroft  *	getopt(3) will like.
    677   1.5   mycroft  */
    678   1.5   mycroft static void
    679  1.32     lukem obsolete(int *argcp, char **argvp[])
    680   1.1       cgd {
    681   1.5   mycroft 	int argc, flags;
    682   1.5   mycroft 	char *ap, **argv, *flagsp, **nargv, *p;
    683   1.5   mycroft 
    684   1.5   mycroft 	/* Setup. */
    685   1.5   mycroft 	argv = *argvp;
    686   1.5   mycroft 	argc = *argcp;
    687   1.5   mycroft 
    688   1.5   mycroft 	/* Return if no arguments or first argument has leading dash. */
    689   1.5   mycroft 	ap = argv[1];
    690   1.5   mycroft 	if (argc == 1 || *ap == '-')
    691   1.5   mycroft 		return;
    692   1.5   mycroft 
    693   1.5   mycroft 	/* Allocate space for new arguments. */
    694  1.33     lukem 	*argvp = nargv = xmalloc((argc + 1) * sizeof(char *));
    695  1.33     lukem 	p = flagsp = xmalloc(strlen(ap) + 2);
    696   1.5   mycroft 
    697   1.5   mycroft 	*nargv++ = *argv;
    698   1.5   mycroft 	argv += 2;
    699   1.5   mycroft 
    700   1.5   mycroft 	for (flags = 0; *ap; ++ap) {
    701   1.5   mycroft 		switch (*ap) {
    702   1.5   mycroft 		case 'B':
    703   1.5   mycroft 		case 'b':
    704   1.5   mycroft 		case 'd':
    705   1.5   mycroft 		case 'f':
    706   1.5   mycroft 		case 'h':
    707   1.5   mycroft 		case 's':
    708   1.5   mycroft 		case 'T':
    709   1.5   mycroft 			if (*argv == NULL) {
    710   1.5   mycroft 				warnx("option requires an argument -- %c", *ap);
    711   1.5   mycroft 				usage();
    712   1.5   mycroft 			}
    713  1.33     lukem 			nargv[0] = xmalloc(strlen(*argv) + 2 + 1);
    714   1.5   mycroft 			nargv[0][0] = '-';
    715   1.5   mycroft 			nargv[0][1] = *ap;
    716  1.17     lukem 			(void)strcpy(&nargv[0][2], *argv); /* XXX safe strcpy */
    717   1.5   mycroft 			++argv;
    718   1.5   mycroft 			++nargv;
    719   1.5   mycroft 			break;
    720   1.5   mycroft 		default:
    721   1.5   mycroft 			if (!flags) {
    722   1.5   mycroft 				*p++ = '-';
    723   1.5   mycroft 				flags = 1;
    724   1.5   mycroft 			}
    725   1.5   mycroft 			*p++ = *ap;
    726   1.5   mycroft 			break;
    727   1.5   mycroft 		}
    728   1.5   mycroft 	}
    729   1.1       cgd 
    730   1.5   mycroft 	/* Terminate flags. */
    731   1.5   mycroft 	if (flags) {
    732   1.5   mycroft 		*p = '\0';
    733   1.5   mycroft 		*nargv++ = flagsp;
    734   1.5   mycroft 	}
    735   1.5   mycroft 
    736   1.5   mycroft 	/* Copy remaining arguments. */
    737  1.14     lukem 	while ((*nargv++ = *argv++) != NULL)
    738  1.14     lukem 		;
    739   1.5   mycroft 
    740   1.5   mycroft 	/* Update argument count. */
    741   1.5   mycroft 	*argcp = nargv - *argvp - 1;
    742  1.33     lukem }
    743  1.33     lukem 
    744  1.33     lukem 
    745  1.33     lukem void *
    746  1.33     lukem xcalloc(size_t number, size_t size)
    747  1.33     lukem {
    748  1.33     lukem 	void *p;
    749  1.33     lukem 
    750  1.33     lukem 	p = calloc(number, size);
    751  1.33     lukem 	if (p == NULL)
    752  1.33     lukem 		quit("%s\n", strerror(errno));
    753  1.33     lukem 	return (p);
    754  1.33     lukem }
    755  1.33     lukem 
    756  1.33     lukem void *
    757  1.33     lukem xmalloc(size_t size)
    758  1.33     lukem {
    759  1.33     lukem 	void *p;
    760  1.33     lukem 
    761  1.33     lukem 	p = malloc(size);
    762  1.33     lukem 	if (p == NULL)
    763  1.33     lukem 		quit("%s\n", strerror(errno));
    764  1.33     lukem 	return (p);
    765  1.33     lukem }
    766  1.33     lukem 
    767  1.33     lukem char *
    768  1.33     lukem xstrdup(const char *str)
    769  1.33     lukem {
    770  1.33     lukem 	char *p;
    771  1.33     lukem 
    772  1.33     lukem 	p = strdup(str);
    773  1.33     lukem 	if (p == NULL)
    774  1.33     lukem 		quit("%s\n", strerror(errno));
    775  1.33     lukem 	return (p);
    776   1.1       cgd }
    777