Home | History | Annotate | Line # | Download | only in restore
tape.c revision 1.56
      1 /*	$NetBSD: tape.c,v 1.56 2006/10/16 03:12:23 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)tape.c	8.9 (Berkeley) 5/1/95";
     41 #else
     42 __RCSID("$NetBSD: tape.c,v 1.56 2006/10/16 03:12:23 christos Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <sys/param.h>
     47 #include <sys/file.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/mtio.h>
     50 #include <sys/stat.h>
     51 
     52 #include <ufs/ufs/dinode.h>
     53 #include <protocols/dumprestore.h>
     54 
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <paths.h>
     58 #include <setjmp.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <time.h>
     63 #include <unistd.h>
     64 
     65 #include <md5.h>
     66 #include <crypto/rmd160.h>
     67 #include <sha1.h>
     68 
     69 #include "restore.h"
     70 #include "extern.h"
     71 
     72 static u_int32_t fssize = MAXBSIZE;
     73 static int	mt = -1;
     74 static int	pipein = 0;
     75 static char	magtape[BUFSIZ];
     76 static int	blkcnt;
     77 static int	numtrec;
     78 static char	*tapebuf;
     79 static union	u_spcl endoftapemark;
     80 static int	blksread;		/* blocks read since last header */
     81 static int	tpblksread = 0;		/* TP_BSIZE blocks read */
     82 static int	tapesread;
     83 static jmp_buf	restart;
     84 static int	gettingfile = 0;	/* restart has a valid frame */
     85 #ifdef RRESTORE
     86 static const char *host = NULL;
     87 #endif
     88 
     89 static int	ofile;
     90 static char	*map;
     91 static char	lnkbuf[MAXPATHLEN + 1];
     92 static int	pathlen;
     93 
     94 int		oldinofmt;	/* old inode format conversion required */
     95 int		Bcvt;		/* Swap Bytes (for CCI or sun) */
     96 
     97 const struct digest_desc *ddesc;
     98 const struct digest_desc digest_descs[] = {
     99 	{ "MD5",
    100 	  (void (*)(void *))MD5Init,
    101 	  (void (*)(void *, const u_char *, u_int))MD5Update,
    102 	  (char *(*)(void *, void *))MD5End, },
    103 	{ "SHA1",
    104 	  (void (*)(void *))SHA1Init,
    105 	  (void (*)(void *, const u_char *, u_int))SHA1Update,
    106 	  (char *(*)(void *, void *))SHA1End, },
    107 	{ "RMD160",
    108 	  (void (*)(void *))RMD160Init,
    109 	  (void (*)(void *, const u_char *, u_int))RMD160Update,
    110 	  (char *(*)(void *, void *))RMD160End, },
    111 	{ .dd_name = NULL },
    112 };
    113 
    114 static union digest_context {
    115 	MD5_CTX dc_md5;
    116 	SHA1_CTX dc_sha1;
    117 	RMD160_CTX dc_rmd160;
    118 } dcontext;
    119 
    120 union digest_buffer {
    121 	char db_md5[32 + 1];
    122 	char db_sha1[40 + 1];
    123 	char db_rmd160[40 + 1];
    124 };
    125 
    126 #define	FLUSHTAPEBUF()	blkcnt = ntrec + 1
    127 
    128 union u_ospcl {
    129 	char dummy[TP_BSIZE];
    130 	struct	s_ospcl {
    131 		int32_t   c_type;
    132 		int32_t   c_date;
    133 		int32_t   c_ddate;
    134 		int32_t   c_volume;
    135 		int32_t   c_tapea;
    136 		u_int16_t c_inumber;
    137 		int32_t   c_magic;
    138 		int32_t   c_checksum;
    139 		struct odinode {
    140 			unsigned short odi_mode;
    141 			u_int16_t odi_nlink;
    142 			u_int16_t odi_uid;
    143 			u_int16_t odi_gid;
    144 			int32_t   odi_size;
    145 			int32_t   odi_rdev;
    146 			char      odi_addr[36];
    147 			int32_t   odi_atime;
    148 			int32_t   odi_mtime;
    149 			int32_t   odi_ctime;
    150 		} c_odinode;
    151 		int32_t c_count;
    152 		char    c_addr[256];
    153 	} s_ospcl;
    154 };
    155 
    156 static void	 accthdr(struct s_spcl *);
    157 static int	 checksum(int *);
    158 static void	 findinode(struct s_spcl *);
    159 static void	 findtapeblksize(void);
    160 static int	 gethead(struct s_spcl *);
    161 static void	 readtape(char *);
    162 static void	 setdumpnum(void);
    163 static void	 terminateinput(void);
    164 static void	 xtrfile(char *, long);
    165 static void	 xtrlnkfile(char *, long);
    166 static void	 xtrlnkskip(char *, long);
    167 static void	 xtrmap(char *, long);
    168 static void	 xtrmapskip(char *, long);
    169 static void	 xtrskip(char *, long);
    170 static void	 swap_header(struct s_spcl *);
    171 static void	 swap_old_header(struct s_ospcl *);
    172 
    173 const struct digest_desc *
    174 digest_lookup(const char *name)
    175 {
    176 	const struct digest_desc *dd;
    177 
    178 	for (dd = digest_descs; dd->dd_name != NULL; dd++)
    179 		if (strcasecmp(dd->dd_name, name) == 0)
    180 			return (dd);
    181 
    182 	return (NULL);
    183 }
    184 
    185 /*
    186  * Set up an input source
    187  */
    188 void
    189 setinput(const char *source)
    190 {
    191 	char *cp;
    192 	FLUSHTAPEBUF();
    193 	if (bflag)
    194 		newtapebuf(ntrec);
    195 	else
    196 		newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC);
    197 	terminal = stdin;
    198 
    199 #ifdef RRESTORE
    200 	if ((cp = strchr(source, ':')) != NULL) {
    201 		host = source;
    202 		/* Ok, because const strings don't have : */
    203 		*cp++ = '\0';
    204 		source = cp;
    205 		if (rmthost(host) == 0)
    206 			exit(1);
    207 	} else
    208 #endif
    209 	if (strcmp(source, "-") == 0) {
    210 		/*
    211 		 * Since input is coming from a pipe we must establish
    212 		 * our own connection to the terminal.
    213 		 */
    214 		terminal = fopen(_PATH_TTY, "r");
    215 		if (terminal == NULL) {
    216 			(void)fprintf(stderr, "cannot open %s: %s\n",
    217 			    _PATH_TTY, strerror(errno));
    218 			terminal = fopen(_PATH_DEVNULL, "r");
    219 			if (terminal == NULL) {
    220 				(void)fprintf(stderr, "cannot open %s: %s\n",
    221 				    _PATH_DEVNULL, strerror(errno));
    222 				exit(1);
    223 			}
    224 		}
    225 		pipein++;
    226 	}
    227 	(void) strcpy(magtape, source);
    228 }
    229 
    230 void
    231 newtapebuf(long size)
    232 {
    233 	static int tapebufsize = -1;
    234 
    235 	ntrec = size;
    236 	if (size <= tapebufsize)
    237 		return;
    238 	if (tapebuf != NULL)
    239 		free(tapebuf);
    240 	tapebuf = malloc(size * TP_BSIZE);
    241 	if (tapebuf == NULL) {
    242 		fprintf(stderr, "Cannot allocate space for tape buffer\n");
    243 		exit(1);
    244 	}
    245 	tapebufsize = size;
    246 }
    247 
    248 /*
    249  * Verify that the tape drive can be accessed and
    250  * that it actually is a dump tape.
    251  */
    252 void
    253 setup(void)
    254 {
    255 	int i, j, *ip;
    256 	struct stat stbuf;
    257 
    258 	vprintf(stdout, "Verify tape and initialize maps\n");
    259 #ifdef RRESTORE
    260 	if (host)
    261 		mt = rmtopen(magtape, 0);
    262 	else
    263 #endif
    264 	if (pipein)
    265 		mt = 0;
    266 	else
    267 		mt = open(magtape, O_RDONLY, 0);
    268 	if (mt < 0) {
    269 		fprintf(stderr, "%s: %s\n", magtape, strerror(errno));
    270 		exit(1);
    271 	}
    272 	volno = 1;
    273 	setdumpnum();
    274 	FLUSHTAPEBUF();
    275 	if (!pipein && !bflag)
    276 		findtapeblksize();
    277 	if (gethead(&spcl) == FAIL) {
    278 		blkcnt--; /* push back this block */
    279 		blksread--;
    280 		tpblksread--;
    281 		cvtflag++;
    282 		if (gethead(&spcl) == FAIL) {
    283 			fprintf(stderr, "Tape is not a dump tape\n");
    284 			exit(1);
    285 		}
    286 		fprintf(stderr, "Converting to new file system format.\n");
    287 	}
    288 	if (pipein) {
    289 		endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC :
    290 		    FS_UFS2_MAGIC;
    291 		endoftapemark.s_spcl.c_type = TS_END;
    292 		ip = (int *)&endoftapemark;
    293 		j = sizeof(union u_spcl) / sizeof(int);
    294 		i = 0;
    295 		do
    296 			i += *ip++;
    297 		while (--j);
    298 		endoftapemark.s_spcl.c_checksum = CHECKSUM - i;
    299 	}
    300 	if (vflag || command == 't')
    301 		printdumpinfo();
    302 	dumptime = spcl.c_ddate;
    303 	dumpdate = spcl.c_date;
    304 	if (stat(".", &stbuf) < 0) {
    305 		fprintf(stderr, "cannot stat .: %s\n", strerror(errno));
    306 		exit(1);
    307 	}
    308 	if (stbuf.st_blksize >= TP_BSIZE && stbuf.st_blksize <= MAXBSIZE)
    309 		fssize = stbuf.st_blksize;
    310 	if (((fssize - 1) & fssize) != 0) {
    311 		fprintf(stderr, "bad block size %d\n", fssize);
    312 		exit(1);
    313 	}
    314 	if (spcl.c_volume != 1) {
    315 		fprintf(stderr, "Tape is not volume 1 of the dump\n");
    316 		exit(1);
    317 	}
    318 	if (gethead(&spcl) == FAIL) {
    319 		dprintf(stdout, "header read failed at %d blocks\n", blksread);
    320 		panic("no header after volume mark!\n");
    321 	}
    322 	findinode(&spcl);
    323 	if (spcl.c_type != TS_CLRI) {
    324 		fprintf(stderr, "Cannot find file removal list\n");
    325 		exit(1);
    326 	}
    327 	maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1;
    328 	dprintf(stdout, "maxino = %llu\n", (unsigned long long)maxino);
    329 	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
    330 	if (map == NULL)
    331 		panic("no memory for active inode map\n");
    332 	usedinomap = map;
    333 	curfile.action = USING;
    334 	getfile(xtrmap, xtrmapskip);
    335 	if (spcl.c_type != TS_BITS) {
    336 		fprintf(stderr, "Cannot find file dump list\n");
    337 		exit(1);
    338 	}
    339 	map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY));
    340 	if (map == (char *)NULL)
    341 		panic("no memory for file dump list\n");
    342 	dumpmap = map;
    343 	curfile.action = USING;
    344 	getfile(xtrmap, xtrmapskip);
    345 	/*
    346 	 * If there may be whiteout entries on the tape, pretend that the
    347 	 * whiteout inode exists, so that the whiteout entries can be
    348 	 * extracted.
    349 	 */
    350 	if (oldinofmt == 0)
    351 		SETINO(WINO, dumpmap);
    352 }
    353 
    354 /*
    355  * Prompt user to load a new dump volume.
    356  * "Nextvol" is the next suggested volume to use.
    357  * This suggested volume is enforced when doing full
    358  * or incremental restores, but can be overrridden by
    359  * the user when only extracting a subset of the files.
    360  */
    361 void
    362 getvol(int nextvol)
    363 {
    364 	int newvol, savecnt, wantnext, i;
    365 	union u_spcl tmpspcl;
    366 #	define tmpbuf tmpspcl.s_spcl
    367 	char buf[TP_BSIZE];
    368 
    369 	newvol = savecnt = wantnext = 0;
    370 	if (nextvol == 1) {
    371 		tapesread = 0;
    372 		gettingfile = 0;
    373 	}
    374 	if (pipein) {
    375 		if (nextvol != 1)
    376 			panic("Changing volumes on pipe input?\n");
    377 		if (volno == 1)
    378 			return;
    379 		goto gethdr;
    380 	}
    381 	savecnt = blksread;
    382 again:
    383 	if (pipein)
    384 		exit(1); /* pipes do not get a second chance */
    385 	if (command == 'R' || command == 'r' || curfile.action != SKIP) {
    386 		newvol = nextvol;
    387 		wantnext = 1;
    388 	} else {
    389 		newvol = 0;
    390 		wantnext = 0;
    391 	}
    392 	while (newvol <= 0) {
    393 		if (tapesread == 0) {
    394 			fprintf(stderr, "%s%s%s%s%s",
    395 			    "You have not read any tapes yet.\n",
    396 			    "Unless you know which volume your",
    397 			    " file(s) are on you should start\n",
    398 			    "with the last volume and work",
    399 			    " towards the first.\n");
    400 			fprintf(stderr,
    401 			    "(Use 1 for the first volume/tape, etc.)\n");
    402 		} else {
    403 			fprintf(stderr, "You have read volumes");
    404 			strcpy(buf, ": ");
    405 			for (i = 1; i < 32; i++)
    406 				if (tapesread & (1 << i)) {
    407 					fprintf(stderr, "%s%d", buf, i);
    408 					strcpy(buf, ", ");
    409 				}
    410 			fprintf(stderr, "\n");
    411 		}
    412 		do	{
    413 			fprintf(stderr, "Specify next volume #: ");
    414 			(void) fflush(stderr);
    415 			(void) fgets(buf, BUFSIZ, terminal);
    416 		} while (!feof(terminal) && buf[0] == '\n');
    417 		if (feof(terminal))
    418 			exit(1);
    419 		newvol = atoi(buf);
    420 		if (newvol <= 0) {
    421 			fprintf(stderr,
    422 			    "Volume numbers are positive numerics\n");
    423 		}
    424 	}
    425 	if (newvol == volno) {
    426 		tapesread |= 1 << volno;
    427 		return;
    428 	}
    429 	closemt();
    430 	fprintf(stderr, "Mount tape volume %d\n", newvol);
    431 	fprintf(stderr, "Enter ``none'' if there are no more tapes\n");
    432 	fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape);
    433 	(void) fflush(stderr);
    434 	(void) fgets(buf, BUFSIZ, terminal);
    435 	if (feof(terminal))
    436 		exit(1);
    437 	if (!strcmp(buf, "none\n")) {
    438 		terminateinput();
    439 		return;
    440 	}
    441 	if (buf[0] != '\n') {
    442 		(void) strcpy(magtape, buf);
    443 		magtape[strlen(magtape) - 1] = '\0';
    444 	}
    445 #ifdef RRESTORE
    446 	if (host)
    447 		mt = rmtopen(magtape, 0);
    448 	else
    449 #endif
    450 		mt = open(magtape, O_RDONLY, 0);
    451 
    452 	if (mt == -1) {
    453 		fprintf(stderr, "Cannot open %s\n", magtape);
    454 		volno = -1;
    455 		goto again;
    456 	}
    457 gethdr:
    458 	volno = newvol;
    459 	setdumpnum();
    460 	FLUSHTAPEBUF();
    461 	if (gethead(&tmpbuf) == FAIL) {
    462 		dprintf(stdout, "header read failed at %d blocks\n", blksread);
    463 		fprintf(stderr, "tape is not dump tape\n");
    464 		volno = 0;
    465 		goto again;
    466 	}
    467 	if (tmpbuf.c_volume != volno) {
    468 	  	fprintf(stderr,
    469 		"Volume mismatch: expecting %d, tape header claims it is %d\n",
    470 		    volno, tmpbuf.c_volume);
    471 		volno = 0;
    472 		goto again;
    473 	}
    474 	if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) {
    475 		time_t ttime = tmpbuf.c_date;
    476 		fprintf(stderr, "Wrong dump date\n\tgot: %s",
    477 			ctime(&ttime));
    478 		fprintf(stderr, "\twanted: %s", ctime(&dumpdate));
    479 		volno = 0;
    480 		goto again;
    481 	}
    482 	tapesread |= 1 << volno;
    483 	blksread = savecnt;
    484  	/*
    485  	 * If continuing from the previous volume, skip over any
    486  	 * blocks read already at the end of the previous volume.
    487  	 *
    488  	 * If coming to this volume at random, skip to the beginning
    489  	 * of the next record.
    490  	 */
    491 	dprintf(stdout, "read %ld recs, tape starts with %ld\n",
    492 		(long)tpblksread, (long)tmpbuf.c_firstrec);
    493  	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) {
    494  		if (!wantnext) {
    495  			tpblksread = tmpbuf.c_firstrec;
    496  			for (i = tmpbuf.c_count; i > 0; i--)
    497  				readtape(buf);
    498  		} else if (tmpbuf.c_firstrec > 0 &&
    499 			   tmpbuf.c_firstrec < tpblksread - 1) {
    500 			/*
    501 			 * -1 since we've read the volume header
    502 			 */
    503  			i = tpblksread - tmpbuf.c_firstrec - 1;
    504 			dprintf(stderr, "Skipping %d duplicate record%s.\n",
    505 				i, i > 1 ? "s" : "");
    506  			while (--i >= 0)
    507  				readtape(buf);
    508  		}
    509  	}
    510 	if (curfile.action == USING) {
    511 		if (volno == 1)
    512 			panic("active file into volume 1\n");
    513 		return;
    514 	}
    515 	/*
    516 	 * Skip up to the beginning of the next record
    517 	 */
    518 	if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER))
    519 		for (i = tmpbuf.c_count; i > 0; i--)
    520 			readtape(buf);
    521 	(void) gethead(&spcl);
    522 	findinode(&spcl);
    523 	if (gettingfile) {
    524 		gettingfile = 0;
    525 		longjmp(restart, 1);
    526 	}
    527 }
    528 
    529 /*
    530  * Handle unexpected EOF.
    531  */
    532 static void
    533 terminateinput(void)
    534 {
    535 
    536 	if (gettingfile && curfile.action == USING) {
    537 		printf("Warning: %s %s\n",
    538 		    "End-of-input encountered while extracting", curfile.name);
    539 	}
    540 	curfile.name = "<name unknown>";
    541 	curfile.action = UNKNOWN;
    542 	curfile.mode = 0;
    543 	curfile.ino = maxino;
    544 	if (gettingfile) {
    545 		gettingfile = 0;
    546 		longjmp(restart, 1);
    547 	}
    548 }
    549 
    550 /*
    551  * handle multiple dumps per tape by skipping forward to the
    552  * appropriate one.
    553  */
    554 static void
    555 setdumpnum(void)
    556 {
    557 	struct mtop tcom;
    558 
    559 	if (dumpnum == 1 || volno != 1)
    560 		return;
    561 	if (pipein) {
    562 		fprintf(stderr, "Cannot have multiple dumps on pipe input\n");
    563 		exit(1);
    564 	}
    565 	tcom.mt_op = MTFSF;
    566 	tcom.mt_count = dumpnum - 1;
    567 #ifdef RRESTORE
    568 	if (host)
    569 		rmtioctl(MTFSF, dumpnum - 1);
    570 	else
    571 #endif
    572 		if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0)
    573 			fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno));
    574 }
    575 
    576 void
    577 printdumpinfo(void)
    578 {
    579 	time_t ttime;
    580 
    581 	ttime = spcl.c_date;
    582 	fprintf(stdout, "Dump   date: %s", ctime(&ttime));
    583 	ttime = spcl.c_ddate;
    584 	fprintf(stdout, "Dumped from: %s",
    585 	    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&ttime));
    586 	fprintf(stderr, "Level %d dump of %s on %s:%s\n",
    587 		spcl.c_level, spcl.c_filesys,
    588 		*spcl.c_host? spcl.c_host: "[unknown]", spcl.c_dev);
    589 	fprintf(stderr, "Label: %s\n", spcl.c_label);
    590 
    591 	if (Mtreefile) {
    592 		ttime = spcl.c_date;
    593 		fprintf(Mtreefile, "#Dump   date: %s", ctime(&ttime));
    594 		ttime = spcl.c_ddate;
    595 		fprintf(Mtreefile, "#Dumped from: %s",
    596 		    (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&ttime));
    597 		fprintf(Mtreefile, "#Level %d dump of %s on %s:%s\n",
    598 			spcl.c_level, spcl.c_filesys,
    599 			*spcl.c_host? spcl.c_host: "[unknown]", spcl.c_dev);
    600 		fprintf(Mtreefile, "#Label: %s\n", spcl.c_label);
    601 		fprintf(Mtreefile, "/set uname=root gname=wheel\n");
    602 		if (ferror(Mtreefile))
    603 			err(1, "error writing to mtree file");
    604 	}
    605 }
    606 
    607 int
    608 extractfile(char *name)
    609 {
    610 	union digest_buffer dbuffer;
    611 	int flags;
    612 	uid_t uid;
    613 	gid_t gid;
    614 	mode_t mode;
    615 	struct timeval mtimep[2], ctimep[2];
    616 	struct entry *ep;
    617 	int setbirth;
    618 
    619 	curfile.name = name;
    620 	curfile.action = USING;
    621 	mtimep[0].tv_sec = curfile.atime_sec;
    622 	mtimep[0].tv_usec = curfile.atime_nsec / 1000;
    623 	mtimep[1].tv_sec = curfile.mtime_sec;
    624 	mtimep[1].tv_usec = curfile.mtime_nsec / 1000;
    625 
    626 	setbirth = curfile.birthtime_sec != 0;
    627 
    628 	if (setbirth) {
    629 		ctimep[0].tv_sec = curfile.atime_sec;
    630 		ctimep[0].tv_usec = curfile.atime_nsec / 1000;
    631 		ctimep[1].tv_sec = curfile.birthtime_sec;
    632 		ctimep[1].tv_usec = curfile.birthtime_nsec / 1000;
    633 	}
    634 	uid = curfile.uid;
    635 	gid = curfile.gid;
    636 	mode = curfile.mode;
    637 	flags = curfile.file_flags;
    638 	switch (mode & IFMT) {
    639 
    640 	default:
    641 		fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode);
    642 		skipfile();
    643 		return (FAIL);
    644 
    645 	case IFSOCK:
    646 		vprintf(stdout, "skipped socket %s\n", name);
    647 		skipfile();
    648 		return (GOOD);
    649 
    650 	case IFDIR:
    651 		if (mflag) {
    652 			ep = lookupname(name);
    653 			if (ep == NULL || ep->e_flags & EXTRACT)
    654 				panic("unextracted directory %s\n", name);
    655 			skipfile();
    656 			return (GOOD);
    657 		}
    658 		vprintf(stdout, "extract file %s\n", name);
    659 		return (genliteraldir(name, curfile.ino));
    660 
    661 	case IFLNK:
    662 		lnkbuf[0] = '\0';
    663 		pathlen = 0;
    664 		getfile(xtrlnkfile, xtrlnkskip);
    665 		if (pathlen == 0) {
    666 			vprintf(stdout,
    667 			    "%s: zero length symbolic link (ignored)\n", name);
    668 			return (GOOD);
    669 		}
    670 		if (uflag)
    671 			(void) unlink(name);
    672 		if (linkit(lnkbuf, name, SYMLINK) == GOOD) {
    673 			if (setbirth)
    674 				(void) lutimes(name, ctimep);
    675 			(void) lutimes(name, mtimep);
    676 			(void) lchown(name, uid, gid);
    677 			(void) lchmod(name, mode);
    678 			if (Mtreefile) {
    679 				writemtree(name, "link",
    680 				    uid, gid, mode, flags);
    681 			} else
    682 				(void) lchflags(name, flags);
    683 			return (GOOD);
    684 		}
    685 		return (FAIL);
    686 
    687 	case IFCHR:
    688 	case IFBLK:
    689 		vprintf(stdout, "extract special file %s\n", name);
    690 		if (Nflag) {
    691 			skipfile();
    692 			return (GOOD);
    693 		}
    694 		if (uflag)
    695 			(void) unlink(name);
    696 		if (mknod(name, (mode & (IFCHR | IFBLK)) | 0600,
    697 		    (int)curfile.rdev) < 0) {
    698 			fprintf(stderr, "%s: cannot create special file: %s\n",
    699 			    name, strerror(errno));
    700 			skipfile();
    701 			return (FAIL);
    702 		}
    703 		skipfile();
    704 		if (setbirth)
    705 			(void) utimes(name, ctimep);
    706 		(void) utimes(name, mtimep);
    707 		(void) chown(name, uid, gid);
    708 		(void) chmod(name, mode);
    709 		if (Mtreefile) {
    710 			writemtree(name,
    711 			    ((mode & (S_IFBLK | IFCHR)) == IFBLK) ?
    712 			    "block" : "char",
    713 			    uid, gid, mode, flags);
    714 		} else
    715 			(void) chflags(name, flags);
    716 		return (GOOD);
    717 
    718 	case IFIFO:
    719 		vprintf(stdout, "extract fifo %s\n", name);
    720 		if (Nflag) {
    721 			skipfile();
    722 			return (GOOD);
    723 		}
    724 		if (uflag)
    725 			(void) unlink(name);
    726 		if (mkfifo(name, 0600) < 0) {
    727 			fprintf(stderr, "%s: cannot create fifo: %s\n",
    728 			    name, strerror(errno));
    729 			skipfile();
    730 			return (FAIL);
    731 		}
    732 		skipfile();
    733 		if (setbirth)
    734 			(void) utimes(name, ctimep);
    735 		(void) utimes(name, mtimep);
    736 		(void) chown(name, uid, gid);
    737 		(void) chmod(name, mode);
    738 		if (Mtreefile) {
    739 			writemtree(name, "fifo",
    740 			    uid, gid, mode, flags);
    741 		} else
    742 			(void) chflags(name, flags);
    743 		return (GOOD);
    744 
    745 	case IFREG:
    746 		vprintf(stdout, "extract file %s\n", name);
    747 		if (uflag)
    748 			(void) unlink(name);
    749 		if (!Nflag && (ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC,
    750 		    0600)) < 0) {
    751 			fprintf(stderr, "%s: cannot create file: %s\n",
    752 			    name, strerror(errno));
    753 			skipfile();
    754 			return (FAIL);
    755 		}
    756 		if (Dflag)
    757 			(*ddesc->dd_init)(&dcontext);
    758 		getfile(xtrfile, xtrskip);
    759 		if (Dflag) {
    760 			(*ddesc->dd_end)(&dcontext, &dbuffer);
    761 			for (ep = lookupname(name); ep != NULL;
    762 			    ep = ep->e_links)
    763 				fprintf(stdout, "%s (%s) = %s\n",
    764 				    ddesc->dd_name, myname(ep),
    765 				    (char *)&dbuffer);
    766 		}
    767 		if (Nflag)
    768 			return (GOOD);
    769 		if (setbirth)
    770 			(void) futimes(ofile, ctimep);
    771 		(void) futimes(ofile, mtimep);
    772 		(void) fchown(ofile, uid, gid);
    773 		(void) fchmod(ofile, mode);
    774 		if (Mtreefile) {
    775 			writemtree(name, "file",
    776 			    uid, gid, mode, flags);
    777 		} else
    778 			(void) fchflags(ofile, flags);
    779 		(void) close(ofile);
    780 		return (GOOD);
    781 	}
    782 	/* NOTREACHED */
    783 }
    784 
    785 /*
    786  * skip over bit maps on the tape
    787  */
    788 void
    789 skipmaps(void)
    790 {
    791 
    792 	while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI)
    793 		skipfile();
    794 }
    795 
    796 /*
    797  * skip over a file on the tape
    798  */
    799 void
    800 skipfile(void)
    801 {
    802 
    803 	curfile.action = SKIP;
    804 	getfile(xtrnull, xtrnull);
    805 }
    806 
    807 /*
    808  * Extract a file from the tape.
    809  * When an allocated block is found it is passed to the fill function;
    810  * when an unallocated block (hole) is found, a zeroed buffer is passed
    811  * to the skip function.
    812  */
    813 void
    814 getfile(void (*fill)(char *buf, long size),
    815 	void (*skip)(char *buf, long size))
    816 {
    817 	int i;
    818 	int curblk = 0;
    819 	quad_t size = spcl.c_size;
    820 	static char clearedbuf[MAXBSIZE];
    821 	char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE];
    822 	char junk[TP_BSIZE];
    823 
    824 #ifdef __GNUC__			/* XXX: to shut up gcc warnings */
    825 	(void)&curblk;
    826 	(void)&size;
    827 #endif
    828 
    829 	if (spcl.c_type == TS_END)
    830 		panic("ran off end of tape\n");
    831 	if (spcl.c_magic != FS_UFS2_MAGIC)
    832 		panic("not at beginning of a file\n");
    833 	if (!gettingfile && setjmp(restart) != 0)
    834 		return;
    835 	gettingfile++;
    836 loop:
    837 	for (i = 0; i < spcl.c_count; i++) {
    838 		if (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI ||
    839 		    spcl.c_addr[i]) {
    840 			readtape(&buf[curblk++][0]);
    841 			if (curblk == fssize / TP_BSIZE) {
    842 				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
    843 				     fssize : (curblk - 1) * TP_BSIZE + size));
    844 				curblk = 0;
    845 			}
    846 		} else {
    847 			if (curblk > 0) {
    848 				(*fill)((char *)buf, (long)(size > TP_BSIZE ?
    849 				     curblk * TP_BSIZE :
    850 				     (curblk - 1) * TP_BSIZE + size));
    851 				curblk = 0;
    852 			}
    853 			(*skip)(clearedbuf, (long)(size > TP_BSIZE ?
    854 				TP_BSIZE : size));
    855 		}
    856 		if ((size -= TP_BSIZE) <= 0) {
    857 			if (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI) {
    858 				/*
    859 				 * In this case, the following expression
    860 				 * should always be false since the size was
    861 				 * initially set to spcl.c_size and
    862 				 * it is initialized to spcl.c_count * TP_BSIZE
    863 				 * in gethead().
    864 				 */
    865 				if (!(size == 0 && i == spcl.c_count - 1))
    866 					panic("inconsistent map size\n");
    867 			} else {
    868 				for (i++; i < spcl.c_count; i++)
    869 					if (spcl.c_addr[i])
    870 						readtape(junk);
    871 			}
    872 			break;
    873 		}
    874 	}
    875 	if (gethead(&spcl) == GOOD && size > 0) {
    876 		if (spcl.c_type == TS_ADDR)
    877 			goto loop;
    878 		dprintf(stdout,
    879 			"Missing address (header) block for %s at %d blocks\n",
    880 			curfile.name, blksread);
    881 	}
    882 	if (curblk > 0)
    883 		(*fill)((char *)buf, (long)((curblk * TP_BSIZE) + size));
    884 	findinode(&spcl);
    885 	gettingfile = 0;
    886 }
    887 
    888 /*
    889  * Write out the next block of a file.
    890  */
    891 static void
    892 xtrfile(char *buf, long size)
    893 {
    894 
    895 	if (Dflag)
    896 		(*ddesc->dd_update)(&dcontext, buf, size);
    897 	if (Nflag)
    898 		return;
    899 	if (write(ofile, buf, (int) size) == -1) {
    900 		fprintf(stderr,
    901 		    "write error extracting inode %llu, name %s\nwrite: %s\n",
    902 			(unsigned long long)curfile.ino, curfile.name,
    903 			strerror(errno));
    904 		exit(1);
    905 	}
    906 }
    907 
    908 /*
    909  * Skip over a hole in a file.
    910  */
    911 /* ARGSUSED */
    912 static void
    913 xtrskip(char *buf, long size)
    914 {
    915 
    916 	if (Dflag)
    917 		(*ddesc->dd_update)(&dcontext, buf, size);
    918 	if (Nflag)
    919 		return;
    920 	if (lseek(ofile, size, SEEK_CUR) == -1) {
    921 		fprintf(stderr,
    922 		    "seek error extracting inode %llu, name %s\nlseek: %s\n",
    923 			(unsigned long long)curfile.ino, curfile.name,
    924 			strerror(errno));
    925 		exit(1);
    926 	}
    927 }
    928 
    929 /*
    930  * Collect the next block of a symbolic link.
    931  */
    932 static void
    933 xtrlnkfile(char *buf, long size)
    934 {
    935 
    936 	pathlen += size;
    937 	if (pathlen > MAXPATHLEN) {
    938 		fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n",
    939 		    curfile.name, lnkbuf, buf, pathlen);
    940 		exit(1);
    941 	}
    942 	(void) strcat(lnkbuf, buf);
    943 }
    944 
    945 /*
    946  * Skip over a hole in a symbolic link (should never happen).
    947  */
    948 /* ARGSUSED */
    949 static void
    950 xtrlnkskip(char *buf, long size)
    951 {
    952 
    953 	fprintf(stderr, "unallocated block in symbolic link %s\n",
    954 		curfile.name);
    955 	exit(1);
    956 }
    957 
    958 /*
    959  * Collect the next block of a bit map.
    960  */
    961 static void
    962 xtrmap(char *buf, long size)
    963 {
    964 
    965 	memmove(map, buf, size);
    966 	map += size;
    967 }
    968 
    969 /*
    970  * Skip over a hole in a bit map (should never happen).
    971  */
    972 /* ARGSUSED */
    973 static void
    974 xtrmapskip(char *buf, long size)
    975 {
    976 
    977 	panic("hole in map\n");
    978 	map += size;
    979 }
    980 
    981 /*
    982  * Noop, when an extraction function is not needed.
    983  */
    984 /* ARGSUSED */
    985 void
    986 xtrnull(char *buf, long size)
    987 {
    988 
    989 	return;
    990 }
    991 
    992 /*
    993  * Read TP_BSIZE blocks from the input.
    994  * Handle read errors, and end of media.
    995  */
    996 static void
    997 readtape(char *buf)
    998 {
    999 	int rd, newvol, i;
   1000 	int cnt, seek_failed;
   1001 
   1002 	if (blkcnt < numtrec) {
   1003 		memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
   1004 		blksread++;
   1005 		tpblksread++;
   1006 		return;
   1007 	}
   1008 	for (i = 0; i < ntrec; i++)
   1009 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
   1010 	if (numtrec == 0)
   1011 		numtrec = ntrec;
   1012 	cnt = ntrec * TP_BSIZE;
   1013 	rd = 0;
   1014 getmore:
   1015 #ifdef RRESTORE
   1016 	if (host)
   1017 		i = rmtread(&tapebuf[rd], cnt);
   1018 	else
   1019 #endif
   1020 		i = read(mt, &tapebuf[rd], cnt);
   1021 	/*
   1022 	 * Check for mid-tape short read error.
   1023 	 * If found, skip rest of buffer and start with the next.
   1024 	 */
   1025 	if (!pipein && numtrec < ntrec && i > 0) {
   1026 		dprintf(stdout, "mid-media short read error.\n");
   1027 		numtrec = ntrec;
   1028 	}
   1029 	/*
   1030 	 * Handle partial block read.
   1031 	 */
   1032 	if (pipein && i == 0 && rd > 0)
   1033 		i = rd;
   1034 	else if (i > 0 && i != ntrec * TP_BSIZE) {
   1035 		if (pipein) {
   1036 			rd += i;
   1037 			cnt -= i;
   1038 			if (cnt > 0)
   1039 				goto getmore;
   1040 			i = rd;
   1041 		} else {
   1042 			/*
   1043 			 * Short read. Process the blocks read.
   1044 			 */
   1045 			if (i % TP_BSIZE != 0)
   1046 				vprintf(stdout,
   1047 				    "partial block read: %d should be %d\n",
   1048 				    i, ntrec * TP_BSIZE);
   1049 			numtrec = i / TP_BSIZE;
   1050 		}
   1051 	}
   1052 	/*
   1053 	 * Handle read error.
   1054 	 */
   1055 	if (i < 0) {
   1056 		fprintf(stderr, "Tape read error while ");
   1057 		switch (curfile.action) {
   1058 		default:
   1059 			fprintf(stderr, "trying to set up tape\n");
   1060 			break;
   1061 		case UNKNOWN:
   1062 			fprintf(stderr, "trying to resynchronize\n");
   1063 			break;
   1064 		case USING:
   1065 			fprintf(stderr, "restoring %s\n", curfile.name);
   1066 			break;
   1067 		case SKIP:
   1068 			fprintf(stderr, "skipping over inode %llu\n",
   1069 			    (unsigned long long)curfile.ino);
   1070 			break;
   1071 		}
   1072 		if (!yflag && !reply("continue"))
   1073 			exit(1);
   1074 		i = ntrec * TP_BSIZE;
   1075 		memset(tapebuf, 0, i);
   1076 #ifdef RRESTORE
   1077 		if (host)
   1078 			seek_failed = (rmtseek(i, 1) < 0);
   1079 		else
   1080 #endif
   1081 			seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1);
   1082 
   1083 		if (seek_failed) {
   1084 			fprintf(stderr,
   1085 			    "continuation failed: %s\n", strerror(errno));
   1086 			exit(1);
   1087 		}
   1088 	}
   1089 	/*
   1090 	 * Handle end of tape.
   1091 	 */
   1092 	if (i == 0) {
   1093 		vprintf(stdout, "End-of-tape encountered\n");
   1094 		if (!pipein) {
   1095 			newvol = volno + 1;
   1096 			volno = 0;
   1097 			numtrec = 0;
   1098 			getvol(newvol);
   1099 			readtape(buf);
   1100 			return;
   1101 		}
   1102 		if (rd % TP_BSIZE != 0)
   1103 			panic("partial block read: %d should be %d\n",
   1104 				rd, ntrec * TP_BSIZE);
   1105 		terminateinput();
   1106 		memmove(&tapebuf[rd], &endoftapemark, (long)TP_BSIZE);
   1107 	}
   1108 	blkcnt = 0;
   1109 	memmove(buf, &tapebuf[(blkcnt++ * TP_BSIZE)], (long)TP_BSIZE);
   1110 	blksread++;
   1111 	tpblksread++;
   1112 }
   1113 
   1114 static void
   1115 findtapeblksize(void)
   1116 {
   1117 	long i;
   1118 
   1119 	for (i = 0; i < ntrec; i++)
   1120 		((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0;
   1121 	blkcnt = 0;
   1122 #ifdef RRESTORE
   1123 	if (host)
   1124 		i = rmtread(tapebuf, ntrec * TP_BSIZE);
   1125 	else
   1126 #endif
   1127 		i = read(mt, tapebuf, ntrec * TP_BSIZE);
   1128 
   1129 	if (i <= 0) {
   1130 		fprintf(stderr, "tape read error: %s\n", strerror(errno));
   1131 		exit(1);
   1132 	}
   1133 	if (i % TP_BSIZE != 0) {
   1134 		fprintf(stderr, "Tape block size (%ld) %s (%ld)\n",
   1135 			(long)i, "is not a multiple of dump block size",
   1136 			(long)TP_BSIZE);
   1137 		exit(1);
   1138 	}
   1139 	ntrec = i / TP_BSIZE;
   1140 	numtrec = ntrec;
   1141 	vprintf(stdout, "Tape block size is %d\n", ntrec);
   1142 }
   1143 
   1144 void
   1145 closemt(void)
   1146 {
   1147 
   1148 	if (mt < 0)
   1149 		return;
   1150 #ifdef RRESTORE
   1151 	if (host)
   1152 		rmtclose();
   1153 	else
   1154 #endif
   1155 		(void) close(mt);
   1156 }
   1157 
   1158 /*
   1159  * Read the next block from the tape.
   1160  * Check to see if it is one of several vintage headers.
   1161  * If it is an old style header, convert it to a new style header.
   1162  * If it is not any valid header, return an error.
   1163  */
   1164 static int
   1165 gethead(struct s_spcl *buf)
   1166 {
   1167 	union u_ospcl u_ospcl;
   1168 
   1169 	if (!cvtflag) {
   1170 		readtape((char *)buf);
   1171 		if (buf->c_magic != NFS_MAGIC &&
   1172 		    buf->c_magic != FS_UFS2_MAGIC) {
   1173 			if (bswap32(buf->c_magic) != NFS_MAGIC &&
   1174 			    bswap32(buf->c_magic) != FS_UFS2_MAGIC)
   1175 				return (FAIL);
   1176 			if (!Bcvt) {
   1177 				vprintf(stdout, "Note: Doing Byte swapping\n");
   1178 				Bcvt = 1;
   1179 			}
   1180 		}
   1181 		if (checksum((int *)buf) == FAIL)
   1182 			return (FAIL);
   1183 		if (Bcvt)
   1184 			swap_header(buf);
   1185 		goto good;
   1186 	}
   1187 
   1188 	readtape((char *)(&u_ospcl.s_ospcl));
   1189 	if (checksum((int *)(&u_ospcl.s_ospcl)) == FAIL)
   1190 		return (FAIL);
   1191 	if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC) {
   1192 		if (bswap32(u_ospcl.s_ospcl.c_magic) != OFS_MAGIC)
   1193 			return (FAIL);
   1194 		if (!Bcvt) {
   1195 			vprintf(stdout, "Note: Doing Byte swapping\n");
   1196 			Bcvt = 1;
   1197 		}
   1198 		swap_old_header(&u_ospcl.s_ospcl);
   1199 	}
   1200 
   1201 	memset(buf, 0, (long)TP_BSIZE);
   1202 	buf->c_type = u_ospcl.s_ospcl.c_type;
   1203 	buf->c_date = u_ospcl.s_ospcl.c_date;
   1204 	buf->c_ddate = u_ospcl.s_ospcl.c_ddate;
   1205 	buf->c_volume = u_ospcl.s_ospcl.c_volume;
   1206 	buf->c_tapea = u_ospcl.s_ospcl.c_tapea;
   1207 	buf->c_inumber = u_ospcl.s_ospcl.c_inumber;
   1208 	buf->c_checksum = u_ospcl.s_ospcl.c_checksum;
   1209 	buf->c_mode = u_ospcl.s_ospcl.c_odinode.odi_mode;
   1210 	buf->c_uid = u_ospcl.s_ospcl.c_odinode.odi_uid;
   1211 	buf->c_gid = u_ospcl.s_ospcl.c_odinode.odi_gid;
   1212 	buf->c_size = u_ospcl.s_ospcl.c_odinode.odi_size;
   1213 	buf->c_rdev = u_ospcl.s_ospcl.c_odinode.odi_rdev;
   1214 	buf->c_atime = u_ospcl.s_ospcl.c_odinode.odi_atime;
   1215 	buf->c_mtime = u_ospcl.s_ospcl.c_odinode.odi_mtime;
   1216 	buf->c_count = u_ospcl.s_ospcl.c_count;
   1217 	memmove(buf->c_addr, u_ospcl.s_ospcl.c_addr, (long)256);
   1218 	buf->c_magic = FS_UFS2_MAGIC;
   1219 good:
   1220 	switch (buf->c_type) {
   1221 
   1222 	case TS_CLRI:
   1223 	case TS_BITS:
   1224 		/*
   1225 		 * Have to patch up missing information in bit map headers
   1226 		 */
   1227 		buf->c_inumber = 0;
   1228 		buf->c_size = buf->c_count * TP_BSIZE;
   1229 		break;
   1230 
   1231 	case TS_TAPE:
   1232 		if ((buf->c_flags & DR_NEWINODEFMT) == 0)
   1233 			oldinofmt = 1;
   1234 		/* fall through */
   1235 	case TS_END:
   1236 		buf->c_inumber = 0;
   1237 		break;
   1238 
   1239 	case TS_INODE:
   1240 		if (buf->c_magic == NFS_MAGIC) {
   1241 			buf->c_tapea = buf->c_old_tapea;
   1242 			buf->c_firstrec = buf->c_old_firstrec;
   1243 			buf->c_date = buf->c_old_date;
   1244 			buf->c_ddate = buf->c_old_ddate;
   1245 			buf->c_atime = buf->c_old_atime;
   1246 			buf->c_mtime = buf->c_old_mtime;
   1247 			buf->c_birthtime = 0;
   1248 			buf->c_birthtimensec = 0;
   1249 			buf->c_atimensec = buf->c_mtimensec = 0;
   1250 		}
   1251 
   1252 	case TS_ADDR:
   1253 		break;
   1254 
   1255 	default:
   1256 		panic("gethead: unknown inode type %d\n", buf->c_type);
   1257 		break;
   1258 	}
   1259 
   1260 	buf->c_magic = FS_UFS2_MAGIC;
   1261 
   1262 	/*
   1263 	 * If we are restoring a filesystem with old format inodes,
   1264 	 * copy the uid/gid to the new location.
   1265 	 */
   1266 	if (oldinofmt) {
   1267 		buf->c_uid = buf->c_spare1[1];
   1268 		buf->c_gid = buf->c_spare1[2];
   1269 	}
   1270 	if (dflag)
   1271 		accthdr(buf);
   1272 	return(GOOD);
   1273 }
   1274 
   1275 /*
   1276  * Check that a header is where it belongs and predict the next header
   1277  */
   1278 static void
   1279 accthdr(struct s_spcl *header)
   1280 {
   1281 	static ino_t previno = 0x7fffffff;
   1282 	static int prevtype;
   1283 	static long predict;
   1284 	long blks, i;
   1285 
   1286 	if (header->c_type == TS_TAPE) {
   1287 		fprintf(stderr, "Volume header (%s inode format) ",
   1288 		    oldinofmt ? "old" : "new");
   1289  		if (header->c_firstrec)
   1290  			fprintf(stderr, "begins with record %lld",
   1291  				(long long)header->c_firstrec);
   1292  		fprintf(stderr, "\n");
   1293 		previno = 0x7fffffff;
   1294 		return;
   1295 	}
   1296 	if (previno == 0x7fffffff)
   1297 		goto newcalc;
   1298 	switch (prevtype) {
   1299 	case TS_BITS:
   1300 		fprintf(stderr, "Dumped inodes map header");
   1301 		break;
   1302 	case TS_CLRI:
   1303 		fprintf(stderr, "Used inodes map header");
   1304 		break;
   1305 	case TS_INODE:
   1306 		fprintf(stderr, "File header, ino %llu",
   1307 		    (unsigned long long)previno);
   1308 		break;
   1309 	case TS_ADDR:
   1310 		fprintf(stderr, "File continuation header, ino %llu",
   1311 		    (unsigned long long)previno);
   1312 		break;
   1313 	case TS_END:
   1314 		fprintf(stderr, "End of tape header");
   1315 		break;
   1316 	}
   1317 	if (predict != blksread - 1)
   1318 		fprintf(stderr, "; predicted %ld blocks, got %ld blocks",
   1319 			(long)predict, (long)(blksread - 1));
   1320 	fprintf(stderr, "\n");
   1321 newcalc:
   1322 	blks = 0;
   1323 	switch (header->c_type) {
   1324 	case TS_END:
   1325 		break;
   1326 	case TS_CLRI:
   1327 	case TS_BITS:
   1328 		blks = header->c_count;
   1329 		break;
   1330 	default:
   1331 		for (i = 0; i < header->c_count; i++)
   1332 			if (header->c_addr[i] != 0)
   1333 				blks++;
   1334 		break;
   1335 	}
   1336 	predict = blks;
   1337 	blksread = 0;
   1338 	prevtype = header->c_type;
   1339 	previno = header->c_inumber;
   1340 }
   1341 
   1342 /*
   1343  * Find an inode header.
   1344  * Complain if had to skip, and complain is set.
   1345  */
   1346 static void
   1347 findinode(struct s_spcl *header)
   1348 {
   1349 	static long skipcnt = 0;
   1350 	long i;
   1351 	char buf[TP_BSIZE];
   1352 
   1353 	curfile.name = "<name unknown>";
   1354 	curfile.action = UNKNOWN;
   1355 	curfile.mode = 0;
   1356 	curfile.ino = 0;
   1357     top:
   1358 	do {
   1359 		if (header->c_magic != FS_UFS2_MAGIC) {
   1360 			skipcnt++;
   1361 			while (gethead(header) == FAIL ||
   1362 			    header->c_date != dumpdate)
   1363 				skipcnt++;
   1364 		}
   1365 		switch (header->c_type) {
   1366 
   1367 		case TS_ADDR:
   1368 			/*
   1369 			 * Skip up to the beginning of the next record
   1370 			 */
   1371 			for (i = 0; i < header->c_count; i++)
   1372 				if (header->c_addr[i])
   1373 					readtape(buf);
   1374 			while (gethead(header) == FAIL ||
   1375 			    header->c_date != dumpdate)
   1376 				skipcnt++;
   1377 			/* We've read a header; don't drop it. */
   1378 			goto top;
   1379 
   1380 		case TS_INODE:
   1381 			curfile.mode = header->c_mode;
   1382 			curfile.uid = header->c_uid;
   1383 			curfile.gid = header->c_gid;
   1384 			curfile.file_flags = header->c_file_flags;
   1385 			curfile.rdev = header->c_rdev;
   1386 			curfile.atime_sec = header->c_atime;
   1387 			curfile.atime_nsec = header->c_atimensec;
   1388 			curfile.mtime_sec = header->c_mtime;
   1389 			curfile.mtime_nsec = header->c_mtimensec;
   1390 			curfile.birthtime_sec = header->c_birthtime;
   1391 			curfile.birthtime_nsec = header->c_birthtimensec;
   1392 			curfile.size = header->c_size;
   1393 			curfile.ino = header->c_inumber;
   1394 			break;
   1395 
   1396 		case TS_END:
   1397 			curfile.ino = maxino;
   1398 			break;
   1399 
   1400 		case TS_CLRI:
   1401 			curfile.name = "<file removal list>";
   1402 			break;
   1403 
   1404 		case TS_BITS:
   1405 			curfile.name = "<file dump list>";
   1406 			break;
   1407 
   1408 		case TS_TAPE:
   1409 			panic("unexpected tape header\n");
   1410 			break;
   1411 
   1412 		default:
   1413 			panic("unknown tape header type %d\n", spcl.c_type);
   1414 			break;
   1415 
   1416 		}
   1417 	} while (header->c_type == TS_ADDR);
   1418 	if (skipcnt > 0)
   1419 		fprintf(stderr, "resync restore, skipped %ld blocks\n",
   1420 		    (long)skipcnt);
   1421 	skipcnt = 0;
   1422 }
   1423 
   1424 static int
   1425 checksum(int *buf)
   1426 {
   1427 	int i, j;
   1428 
   1429 	j = sizeof(union u_spcl) / sizeof(int);
   1430 	i = 0;
   1431 	if(!Bcvt) {
   1432 		do
   1433 			i += *buf++;
   1434 		while (--j);
   1435 	} else {
   1436 		do
   1437 			i += bswap32(*buf++);
   1438 		while (--j);
   1439 	}
   1440 
   1441 	if (i != CHECKSUM) {
   1442 		fprintf(stderr, "Checksum error %o, inode %llu file %s\n", i,
   1443 		    (unsigned long long)curfile.ino, curfile.name);
   1444 		return(FAIL);
   1445 	}
   1446 	return(GOOD);
   1447 }
   1448 
   1449 #ifdef RRESTORE
   1450 #include <stdarg.h>
   1451 
   1452 void
   1453 msg(const char *fmt, ...)
   1454 {
   1455 	va_list ap;
   1456 
   1457 	va_start(ap, fmt);
   1458 	(void)vfprintf(stderr, fmt, ap);
   1459 	va_end(ap);
   1460 }
   1461 #endif /* RRESTORE */
   1462 
   1463 static void
   1464 swap_header(struct s_spcl *s)
   1465 {
   1466 	s->c_type = bswap32(s->c_type);
   1467 	s->c_old_date = bswap32(s->c_old_date);
   1468 	s->c_old_ddate = bswap32(s->c_old_ddate);
   1469 	s->c_volume = bswap32(s->c_volume);
   1470 	s->c_old_tapea = bswap32(s->c_old_tapea);
   1471 	s->c_inumber = bswap32(s->c_inumber);
   1472 	s->c_magic = bswap32(s->c_magic);
   1473 	s->c_checksum = bswap32(s->c_checksum);
   1474 
   1475 	s->c_mode = bswap16(s->c_mode);
   1476 	s->c_size = bswap64(s->c_size);
   1477 	s->c_old_atime = bswap32(s->c_old_atime);
   1478 	s->c_atimensec = bswap32(s->c_atimensec);
   1479 	s->c_old_mtime = bswap32(s->c_old_mtime);
   1480 	s->c_mtimensec = bswap32(s->c_mtimensec);
   1481 	s->c_rdev = bswap32(s->c_rdev);
   1482 	s->c_birthtimensec = bswap32(s->c_birthtimensec);
   1483 	s->c_birthtime = bswap64(s->c_birthtime);
   1484 	s->c_atime = bswap64(s->c_atime);
   1485 	s->c_mtime = bswap64(s->c_mtime);
   1486 	s->c_file_flags = bswap32(s->c_file_flags);
   1487 	s->c_uid = bswap32(s->c_uid);
   1488 	s->c_gid = bswap32(s->c_gid);
   1489 
   1490 	s->c_count = bswap32(s->c_count);
   1491 	s->c_level = bswap32(s->c_level);
   1492 	s->c_flags = bswap32(s->c_flags);
   1493 	s->c_old_firstrec = bswap32(s->c_old_firstrec);
   1494 
   1495 	s->c_date = bswap64(s->c_date);
   1496 	s->c_ddate = bswap64(s->c_ddate);
   1497 	s->c_tapea = bswap64(s->c_tapea);
   1498 	s->c_firstrec = bswap64(s->c_firstrec);
   1499 
   1500 	/*
   1501 	 * These are ouid and ogid.
   1502 	 */
   1503 	s->c_spare1[1] = bswap16(s->c_spare1[1]);
   1504 	s->c_spare1[2] = bswap16(s->c_spare1[2]);
   1505 }
   1506 
   1507 static void
   1508 swap_old_header(struct s_ospcl *os)
   1509 {
   1510 	os->c_type = bswap32(os->c_type);
   1511 	os->c_date = bswap32(os->c_date);
   1512 	os->c_ddate = bswap32(os->c_ddate);
   1513 	os->c_volume = bswap32(os->c_volume);
   1514 	os->c_tapea = bswap32(os->c_tapea);
   1515 	os->c_inumber = bswap16(os->c_inumber);
   1516 	os->c_magic = bswap32(os->c_magic);
   1517 	os->c_checksum = bswap32(os->c_checksum);
   1518 
   1519 	os->c_odinode.odi_mode = bswap16(os->c_odinode.odi_mode);
   1520 	os->c_odinode.odi_nlink = bswap16(os->c_odinode.odi_nlink);
   1521 	os->c_odinode.odi_uid = bswap16(os->c_odinode.odi_uid);
   1522 	os->c_odinode.odi_gid = bswap16(os->c_odinode.odi_gid);
   1523 
   1524 	os->c_odinode.odi_size = bswap32(os->c_odinode.odi_size);
   1525 	os->c_odinode.odi_rdev = bswap32(os->c_odinode.odi_rdev);
   1526 	os->c_odinode.odi_atime = bswap32(os->c_odinode.odi_atime);
   1527 	os->c_odinode.odi_mtime = bswap32(os->c_odinode.odi_mtime);
   1528 	os->c_odinode.odi_ctime = bswap32(os->c_odinode.odi_ctime);
   1529 
   1530 	os->c_count = bswap32(os->c_count);
   1531 }
   1532