tape.c revision 8871
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1983, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * (c) UNIX System Laboratories, Inc. 51558Srgrimes * All or some portions of this file are derived from material licensed 61558Srgrimes * to the University of California by American Telephone and Telegraph 71558Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 81558Srgrimes * the permission of UNIX System Laboratories, Inc. 91558Srgrimes * 101558Srgrimes * Redistribution and use in source and binary forms, with or without 111558Srgrimes * modification, are permitted provided that the following conditions 121558Srgrimes * are met: 131558Srgrimes * 1. Redistributions of source code must retain the above copyright 141558Srgrimes * notice, this list of conditions and the following disclaimer. 151558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 161558Srgrimes * notice, this list of conditions and the following disclaimer in the 171558Srgrimes * documentation and/or other materials provided with the distribution. 181558Srgrimes * 3. All advertising materials mentioning features or use of this software 191558Srgrimes * must display the following acknowledgement: 201558Srgrimes * This product includes software developed by the University of 211558Srgrimes * California, Berkeley and its contributors. 221558Srgrimes * 4. Neither the name of the University nor the names of its contributors 231558Srgrimes * may be used to endorse or promote products derived from this software 241558Srgrimes * without specific prior written permission. 251558Srgrimes * 261558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 271558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 281558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 291558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 301558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 311558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 321558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 331558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 341558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 351558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361558Srgrimes * SUCH DAMAGE. 371558Srgrimes */ 381558Srgrimes 391558Srgrimes#ifndef lint 401558Srgrimesstatic char sccsid[] = "@(#)tape.c 8.3 (Berkeley) 4/1/94"; 411558Srgrimes#endif /* not lint */ 421558Srgrimes 431558Srgrimes#include <sys/param.h> 441558Srgrimes#include <sys/file.h> 451558Srgrimes#include <sys/ioctl.h> 461558Srgrimes#include <sys/mtio.h> 471558Srgrimes#include <sys/stat.h> 481558Srgrimes 491558Srgrimes#include <ufs/ufs/dinode.h> 501558Srgrimes#include <protocols/dumprestore.h> 511558Srgrimes 521558Srgrimes#include <errno.h> 531558Srgrimes#include <setjmp.h> 541558Srgrimes#include <stdio.h> 551558Srgrimes#include <stdlib.h> 561558Srgrimes#include <string.h> 571558Srgrimes#include <unistd.h> 581558Srgrimes 591558Srgrimes#include "restore.h" 601558Srgrimes#include "extern.h" 611558Srgrimes#include "pathnames.h" 621558Srgrimes 631558Srgrimesstatic long fssize = MAXBSIZE; 641558Srgrimesstatic int mt = -1; 651558Srgrimesstatic int pipein = 0; 661558Srgrimesstatic char magtape[BUFSIZ]; 671558Srgrimesstatic int blkcnt; 681558Srgrimesstatic int numtrec; 691558Srgrimesstatic char *tapebuf; 701558Srgrimesstatic union u_spcl endoftapemark; 711558Srgrimesstatic long blksread; /* blocks read since last header */ 721558Srgrimesstatic long tpblksread = 0; /* TP_BSIZE blocks read */ 731558Srgrimesstatic long tapesread; 741558Srgrimesstatic jmp_buf restart; 751558Srgrimesstatic int gettingfile = 0; /* restart has a valid frame */ 761558Srgrimesstatic char *host = NULL; 771558Srgrimes 781558Srgrimesstatic int ofile; 791558Srgrimesstatic char *map; 801558Srgrimesstatic char lnkbuf[MAXPATHLEN + 1]; 811558Srgrimesstatic int pathlen; 821558Srgrimes 831558Srgrimesint oldinofmt; /* old inode format conversion required */ 841558Srgrimesint Bcvt; /* Swap Bytes (for CCI or sun) */ 851558Srgrimesstatic int Qcvt; /* Swap quads (for sun) */ 861558Srgrimes 871558Srgrimes#define FLUSHTAPEBUF() blkcnt = ntrec + 1 881558Srgrimes 891558Srgrimesstatic void accthdr __P((struct s_spcl *)); 901558Srgrimesstatic int checksum __P((int *)); 911558Srgrimesstatic void findinode __P((struct s_spcl *)); 921558Srgrimesstatic void findtapeblksize __P((void)); 931558Srgrimesstatic int gethead __P((struct s_spcl *)); 941558Srgrimesstatic void readtape __P((char *)); 951558Srgrimesstatic void setdumpnum __P((void)); 961558Srgrimesstatic u_long swabl __P((u_long)); 971558Srgrimesstatic u_char *swablong __P((u_char *, int)); 981558Srgrimesstatic u_char *swabshort __P((u_char *, int)); 991558Srgrimesstatic void terminateinput __P((void)); 1001558Srgrimesstatic void xtrfile __P((char *, long)); 1011558Srgrimesstatic void xtrlnkfile __P((char *, long)); 1021558Srgrimesstatic void xtrlnkskip __P((char *, long)); 1031558Srgrimesstatic void xtrmap __P((char *, long)); 1041558Srgrimesstatic void xtrmapskip __P((char *, long)); 1051558Srgrimesstatic void xtrskip __P((char *, long)); 1061558Srgrimes 1071558Srgrimes/* 1081558Srgrimes * Set up an input source 1091558Srgrimes */ 1101558Srgrimesvoid 1111558Srgrimessetinput(source) 1121558Srgrimes char *source; 1131558Srgrimes{ 1141558Srgrimes FLUSHTAPEBUF(); 1151558Srgrimes if (bflag) 1161558Srgrimes newtapebuf(ntrec); 1171558Srgrimes else 1181558Srgrimes newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC); 1191558Srgrimes terminal = stdin; 1201558Srgrimes 1211558Srgrimes#ifdef RRESTORE 1221558Srgrimes if (index(source, ':')) { 1231558Srgrimes host = source; 1241558Srgrimes source = index(host, ':'); 1251558Srgrimes *source++ = '\0'; 1261558Srgrimes if (rmthost(host) == 0) 1271558Srgrimes done(1); 1281558Srgrimes } else 1291558Srgrimes#endif 1301558Srgrimes if (strcmp(source, "-") == 0) { 1311558Srgrimes /* 1321558Srgrimes * Since input is coming from a pipe we must establish 1331558Srgrimes * our own connection to the terminal. 1341558Srgrimes */ 1351558Srgrimes terminal = fopen(_PATH_TTY, "r"); 1361558Srgrimes if (terminal == NULL) { 1371558Srgrimes (void)fprintf(stderr, "cannot open %s: %s\n", 1381558Srgrimes _PATH_TTY, strerror(errno)); 1391558Srgrimes terminal = fopen(_PATH_DEVNULL, "r"); 1401558Srgrimes if (terminal == NULL) { 1411558Srgrimes (void)fprintf(stderr, "cannot open %s: %s\n", 1421558Srgrimes _PATH_DEVNULL, strerror(errno)); 1431558Srgrimes done(1); 1441558Srgrimes } 1451558Srgrimes } 1461558Srgrimes pipein++; 1471558Srgrimes } 1481558Srgrimes setuid(getuid()); /* no longer need or want root privileges */ 1491558Srgrimes (void) strcpy(magtape, source); 1501558Srgrimes} 1511558Srgrimes 1521558Srgrimesvoid 1531558Srgrimesnewtapebuf(size) 1541558Srgrimes long size; 1551558Srgrimes{ 1561558Srgrimes static tapebufsize = -1; 1571558Srgrimes 1581558Srgrimes ntrec = size; 1591558Srgrimes if (size <= tapebufsize) 1601558Srgrimes return; 1611558Srgrimes if (tapebuf != NULL) 1621558Srgrimes free(tapebuf); 1631558Srgrimes tapebuf = malloc(size * TP_BSIZE); 1641558Srgrimes if (tapebuf == NULL) { 1651558Srgrimes fprintf(stderr, "Cannot allocate space for tape buffer\n"); 1661558Srgrimes done(1); 1671558Srgrimes } 1681558Srgrimes tapebufsize = size; 1691558Srgrimes} 1701558Srgrimes 1711558Srgrimes/* 1721558Srgrimes * Verify that the tape drive can be accessed and 1731558Srgrimes * that it actually is a dump tape. 1741558Srgrimes */ 1751558Srgrimesvoid 1761558Srgrimessetup() 1771558Srgrimes{ 1781558Srgrimes int i, j, *ip; 1791558Srgrimes struct stat stbuf; 1801558Srgrimes 1811558Srgrimes vprintf(stdout, "Verify tape and initialize maps\n"); 1821558Srgrimes#ifdef RRESTORE 1831558Srgrimes if (host) 1841558Srgrimes mt = rmtopen(magtape, 0); 1851558Srgrimes else 1861558Srgrimes#endif 1871558Srgrimes if (pipein) 1881558Srgrimes mt = 0; 1891558Srgrimes else 1901558Srgrimes mt = open(magtape, O_RDONLY, 0); 1911558Srgrimes if (mt < 0) { 1921558Srgrimes fprintf(stderr, "%s: %s\n", magtape, strerror(errno)); 1931558Srgrimes done(1); 1941558Srgrimes } 1951558Srgrimes volno = 1; 1961558Srgrimes setdumpnum(); 1971558Srgrimes FLUSHTAPEBUF(); 1981558Srgrimes if (!pipein && !bflag) 1991558Srgrimes findtapeblksize(); 2001558Srgrimes if (gethead(&spcl) == FAIL) { 2011558Srgrimes blkcnt--; /* push back this block */ 2021558Srgrimes blksread--; 2031558Srgrimes tpblksread--; 2041558Srgrimes cvtflag++; 2051558Srgrimes if (gethead(&spcl) == FAIL) { 2061558Srgrimes fprintf(stderr, "Tape is not a dump tape\n"); 2071558Srgrimes done(1); 2081558Srgrimes } 2091558Srgrimes fprintf(stderr, "Converting to new file system format.\n"); 2101558Srgrimes } 2111558Srgrimes if (pipein) { 2121558Srgrimes endoftapemark.s_spcl.c_magic = cvtflag ? OFS_MAGIC : NFS_MAGIC; 2131558Srgrimes endoftapemark.s_spcl.c_type = TS_END; 2141558Srgrimes ip = (int *)&endoftapemark; 2151558Srgrimes j = sizeof(union u_spcl) / sizeof(int); 2161558Srgrimes i = 0; 2171558Srgrimes do 2181558Srgrimes i += *ip++; 2191558Srgrimes while (--j); 2201558Srgrimes endoftapemark.s_spcl.c_checksum = CHECKSUM - i; 2211558Srgrimes } 2221558Srgrimes if (vflag || command == 't') 2231558Srgrimes printdumpinfo(); 2241558Srgrimes dumptime = spcl.c_ddate; 2251558Srgrimes dumpdate = spcl.c_date; 2261558Srgrimes if (stat(".", &stbuf) < 0) { 2271558Srgrimes fprintf(stderr, "cannot stat .: %s\n", strerror(errno)); 2281558Srgrimes done(1); 2291558Srgrimes } 2301558Srgrimes if (stbuf.st_blksize > 0 && stbuf.st_blksize <= MAXBSIZE) 2311558Srgrimes fssize = stbuf.st_blksize; 2321558Srgrimes if (((fssize - 1) & fssize) != 0) { 2331558Srgrimes fprintf(stderr, "bad block size %d\n", fssize); 2341558Srgrimes done(1); 2351558Srgrimes } 2361558Srgrimes if (spcl.c_volume != 1) { 2371558Srgrimes fprintf(stderr, "Tape is not volume 1 of the dump\n"); 2381558Srgrimes done(1); 2391558Srgrimes } 2401558Srgrimes if (gethead(&spcl) == FAIL) { 2411558Srgrimes dprintf(stdout, "header read failed at %d blocks\n", blksread); 2421558Srgrimes panic("no header after volume mark!\n"); 2431558Srgrimes } 2441558Srgrimes findinode(&spcl); 2451558Srgrimes if (spcl.c_type != TS_CLRI) { 2461558Srgrimes fprintf(stderr, "Cannot find file removal list\n"); 2471558Srgrimes done(1); 2481558Srgrimes } 2491558Srgrimes maxino = (spcl.c_count * TP_BSIZE * NBBY) + 1; 2501558Srgrimes dprintf(stdout, "maxino = %d\n", maxino); 2511558Srgrimes map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); 2521558Srgrimes if (map == NULL) 2531558Srgrimes panic("no memory for file removal list\n"); 2541558Srgrimes clrimap = map; 2551558Srgrimes curfile.action = USING; 2561558Srgrimes getfile(xtrmap, xtrmapskip); 2571558Srgrimes if (spcl.c_type != TS_BITS) { 2581558Srgrimes fprintf(stderr, "Cannot find file dump list\n"); 2591558Srgrimes done(1); 2601558Srgrimes } 2611558Srgrimes map = calloc((unsigned)1, (unsigned)howmany(maxino, NBBY)); 2621558Srgrimes if (map == (char *)NULL) 2631558Srgrimes panic("no memory for file dump list\n"); 2641558Srgrimes dumpmap = map; 2651558Srgrimes curfile.action = USING; 2661558Srgrimes getfile(xtrmap, xtrmapskip); 2671558Srgrimes} 2681558Srgrimes 2691558Srgrimes/* 2701558Srgrimes * Prompt user to load a new dump volume. 2711558Srgrimes * "Nextvol" is the next suggested volume to use. 2721558Srgrimes * This suggested volume is enforced when doing full 2731558Srgrimes * or incremental restores, but can be overrridden by 2741558Srgrimes * the user when only extracting a subset of the files. 2751558Srgrimes */ 2761558Srgrimesvoid 2771558Srgrimesgetvol(nextvol) 2781558Srgrimes long nextvol; 2791558Srgrimes{ 2801558Srgrimes long newvol, savecnt, wantnext, i; 2811558Srgrimes union u_spcl tmpspcl; 2821558Srgrimes# define tmpbuf tmpspcl.s_spcl 2831558Srgrimes char buf[TP_BSIZE]; 2841558Srgrimes 2851558Srgrimes if (nextvol == 1) { 2861558Srgrimes tapesread = 0; 2871558Srgrimes gettingfile = 0; 2881558Srgrimes } 2891558Srgrimes if (pipein) { 2901558Srgrimes if (nextvol != 1) 2911558Srgrimes panic("Changing volumes on pipe input?\n"); 2921558Srgrimes if (volno == 1) 2931558Srgrimes return; 2941558Srgrimes goto gethdr; 2951558Srgrimes } 2961558Srgrimes savecnt = blksread; 2971558Srgrimesagain: 2981558Srgrimes if (pipein) 2991558Srgrimes done(1); /* pipes do not get a second chance */ 3001558Srgrimes if (command == 'R' || command == 'r' || curfile.action != SKIP) { 3011558Srgrimes newvol = nextvol; 3021558Srgrimes wantnext = 1; 3038871Srgrimes } else { 3041558Srgrimes newvol = 0; 3051558Srgrimes wantnext = 0; 3061558Srgrimes } 3071558Srgrimes while (newvol <= 0) { 3081558Srgrimes if (tapesread == 0) { 3091558Srgrimes fprintf(stderr, "%s%s%s%s%s", 3101558Srgrimes "You have not read any tapes yet.\n", 3111558Srgrimes "Unless you know which volume your", 3121558Srgrimes " file(s) are on you should start\n", 3131558Srgrimes "with the last volume and work", 3141558Srgrimes " towards towards the first.\n"); 3151558Srgrimes } else { 3161558Srgrimes fprintf(stderr, "You have read volumes"); 3171558Srgrimes strcpy(buf, ": "); 3181558Srgrimes for (i = 1; i < 32; i++) 3191558Srgrimes if (tapesread & (1 << i)) { 3201558Srgrimes fprintf(stderr, "%s%d", buf, i); 3211558Srgrimes strcpy(buf, ", "); 3221558Srgrimes } 3231558Srgrimes fprintf(stderr, "\n"); 3241558Srgrimes } 3251558Srgrimes do { 3261558Srgrimes fprintf(stderr, "Specify next volume #: "); 3271558Srgrimes (void) fflush(stderr); 3281558Srgrimes (void) fgets(buf, BUFSIZ, terminal); 3291558Srgrimes } while (!feof(terminal) && buf[0] == '\n'); 3301558Srgrimes if (feof(terminal)) 3311558Srgrimes done(1); 3321558Srgrimes newvol = atoi(buf); 3331558Srgrimes if (newvol <= 0) { 3341558Srgrimes fprintf(stderr, 3351558Srgrimes "Volume numbers are positive numerics\n"); 3361558Srgrimes } 3371558Srgrimes } 3381558Srgrimes if (newvol == volno) { 3391558Srgrimes tapesread |= 1 << volno; 3401558Srgrimes return; 3411558Srgrimes } 3421558Srgrimes closemt(); 3431558Srgrimes fprintf(stderr, "Mount tape volume %d\n", newvol); 3441558Srgrimes fprintf(stderr, "Enter ``none'' if there are no more tapes\n"); 3451558Srgrimes fprintf(stderr, "otherwise enter tape name (default: %s) ", magtape); 3461558Srgrimes (void) fflush(stderr); 3471558Srgrimes (void) fgets(buf, BUFSIZ, terminal); 3481558Srgrimes if (feof(terminal)) 3491558Srgrimes done(1); 3501558Srgrimes if (!strcmp(buf, "none\n")) { 3511558Srgrimes terminateinput(); 3521558Srgrimes return; 3531558Srgrimes } 3541558Srgrimes if (buf[0] != '\n') { 3551558Srgrimes (void) strcpy(magtape, buf); 3561558Srgrimes magtape[strlen(magtape) - 1] = '\0'; 3571558Srgrimes } 3581558Srgrimes#ifdef RRESTORE 3591558Srgrimes if (host) 3601558Srgrimes mt = rmtopen(magtape, 0); 3611558Srgrimes else 3621558Srgrimes#endif 3631558Srgrimes mt = open(magtape, O_RDONLY, 0); 3641558Srgrimes 3651558Srgrimes if (mt == -1) { 3661558Srgrimes fprintf(stderr, "Cannot open %s\n", magtape); 3671558Srgrimes volno = -1; 3681558Srgrimes goto again; 3691558Srgrimes } 3701558Srgrimesgethdr: 3711558Srgrimes volno = newvol; 3721558Srgrimes setdumpnum(); 3731558Srgrimes FLUSHTAPEBUF(); 3741558Srgrimes if (gethead(&tmpbuf) == FAIL) { 3751558Srgrimes dprintf(stdout, "header read failed at %d blocks\n", blksread); 3761558Srgrimes fprintf(stderr, "tape is not dump tape\n"); 3771558Srgrimes volno = 0; 3781558Srgrimes goto again; 3791558Srgrimes } 3801558Srgrimes if (tmpbuf.c_volume != volno) { 3811558Srgrimes fprintf(stderr, "Wrong volume (%d)\n", tmpbuf.c_volume); 3821558Srgrimes volno = 0; 3831558Srgrimes goto again; 3841558Srgrimes } 3851558Srgrimes if (tmpbuf.c_date != dumpdate || tmpbuf.c_ddate != dumptime) { 3861558Srgrimes fprintf(stderr, "Wrong dump date\n\tgot: %s", 3871558Srgrimes ctime(&tmpbuf.c_date)); 3881558Srgrimes fprintf(stderr, "\twanted: %s", ctime(&dumpdate)); 3891558Srgrimes volno = 0; 3901558Srgrimes goto again; 3911558Srgrimes } 3921558Srgrimes tapesread |= 1 << volno; 3931558Srgrimes blksread = savecnt; 3941558Srgrimes /* 3951558Srgrimes * If continuing from the previous volume, skip over any 3961558Srgrimes * blocks read already at the end of the previous volume. 3971558Srgrimes * 3981558Srgrimes * If coming to this volume at random, skip to the beginning 3991558Srgrimes * of the next record. 4001558Srgrimes */ 4018871Srgrimes dprintf(stdout, "read %ld recs, tape starts with %ld\n", 4021558Srgrimes tpblksread, tmpbuf.c_firstrec); 4031558Srgrimes if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) { 4041558Srgrimes if (!wantnext) { 4051558Srgrimes tpblksread = tmpbuf.c_firstrec; 4061558Srgrimes for (i = tmpbuf.c_count; i > 0; i--) 4071558Srgrimes readtape(buf); 4081558Srgrimes } else if (tmpbuf.c_firstrec > 0 && 4091558Srgrimes tmpbuf.c_firstrec < tpblksread - 1) { 4101558Srgrimes /* 4111558Srgrimes * -1 since we've read the volume header 4121558Srgrimes */ 4131558Srgrimes i = tpblksread - tmpbuf.c_firstrec - 1; 4141558Srgrimes dprintf(stderr, "Skipping %d duplicate record%s.\n", 4151558Srgrimes i, i > 1 ? "s" : ""); 4161558Srgrimes while (--i >= 0) 4171558Srgrimes readtape(buf); 4181558Srgrimes } 4191558Srgrimes } 4201558Srgrimes if (curfile.action == USING) { 4211558Srgrimes if (volno == 1) 4221558Srgrimes panic("active file into volume 1\n"); 4231558Srgrimes return; 4241558Srgrimes } 4251558Srgrimes /* 4261558Srgrimes * Skip up to the beginning of the next record 4271558Srgrimes */ 4281558Srgrimes if (tmpbuf.c_type == TS_TAPE && (tmpbuf.c_flags & DR_NEWHEADER)) 4291558Srgrimes for (i = tmpbuf.c_count; i > 0; i--) 4301558Srgrimes readtape(buf); 4311558Srgrimes (void) gethead(&spcl); 4321558Srgrimes findinode(&spcl); 4331558Srgrimes if (gettingfile) { 4341558Srgrimes gettingfile = 0; 4351558Srgrimes longjmp(restart, 1); 4361558Srgrimes } 4371558Srgrimes} 4381558Srgrimes 4391558Srgrimes/* 4401558Srgrimes * Handle unexpected EOF. 4411558Srgrimes */ 4421558Srgrimesstatic void 4431558Srgrimesterminateinput() 4441558Srgrimes{ 4451558Srgrimes 4461558Srgrimes if (gettingfile && curfile.action == USING) { 4471558Srgrimes printf("Warning: %s %s\n", 4481558Srgrimes "End-of-input encountered while extracting", curfile.name); 4491558Srgrimes } 4501558Srgrimes curfile.name = "<name unknown>"; 4511558Srgrimes curfile.action = UNKNOWN; 4521558Srgrimes curfile.dip = NULL; 4531558Srgrimes curfile.ino = maxino; 4541558Srgrimes if (gettingfile) { 4551558Srgrimes gettingfile = 0; 4561558Srgrimes longjmp(restart, 1); 4571558Srgrimes } 4581558Srgrimes} 4591558Srgrimes 4601558Srgrimes/* 4611558Srgrimes * handle multiple dumps per tape by skipping forward to the 4621558Srgrimes * appropriate one. 4631558Srgrimes */ 4641558Srgrimesstatic void 4651558Srgrimessetdumpnum() 4661558Srgrimes{ 4671558Srgrimes struct mtop tcom; 4681558Srgrimes 4691558Srgrimes if (dumpnum == 1 || volno != 1) 4701558Srgrimes return; 4711558Srgrimes if (pipein) { 4721558Srgrimes fprintf(stderr, "Cannot have multiple dumps on pipe input\n"); 4731558Srgrimes done(1); 4741558Srgrimes } 4751558Srgrimes tcom.mt_op = MTFSF; 4761558Srgrimes tcom.mt_count = dumpnum - 1; 4771558Srgrimes#ifdef RRESTORE 4781558Srgrimes if (host) 4791558Srgrimes rmtioctl(MTFSF, dumpnum - 1); 4808871Srgrimes else 4811558Srgrimes#endif 4821558Srgrimes if (ioctl(mt, (int)MTIOCTOP, (char *)&tcom) < 0) 4831558Srgrimes fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno)); 4841558Srgrimes} 4851558Srgrimes 4861558Srgrimesvoid 4871558Srgrimesprintdumpinfo() 4881558Srgrimes{ 4891558Srgrimes fprintf(stdout, "Dump date: %s", ctime(&spcl.c_date)); 4901558Srgrimes fprintf(stdout, "Dumped from: %s", 4911558Srgrimes (spcl.c_ddate == 0) ? "the epoch\n" : ctime(&spcl.c_ddate)); 4921558Srgrimes if (spcl.c_host[0] == '\0') 4931558Srgrimes return; 4941558Srgrimes fprintf(stderr, "Level %d dump of %s on %s:%s\n", 4951558Srgrimes spcl.c_level, spcl.c_filesys, spcl.c_host, spcl.c_dev); 4961558Srgrimes fprintf(stderr, "Label: %s\n", spcl.c_label); 4971558Srgrimes} 4981558Srgrimes 4991558Srgrimesint 5001558Srgrimesextractfile(name) 5011558Srgrimes char *name; 5021558Srgrimes{ 5031558Srgrimes int mode; 5041558Srgrimes struct timeval timep[2]; 5051558Srgrimes struct entry *ep; 5061558Srgrimes 5071558Srgrimes curfile.name = name; 5081558Srgrimes curfile.action = USING; 5091558Srgrimes timep[0].tv_sec = curfile.dip->di_atime.ts_sec; 5101558Srgrimes timep[0].tv_usec = curfile.dip->di_atime.ts_nsec / 1000; 5111558Srgrimes timep[1].tv_sec = curfile.dip->di_mtime.ts_sec; 5121558Srgrimes timep[1].tv_usec = curfile.dip->di_mtime.ts_nsec / 1000; 5131558Srgrimes mode = curfile.dip->di_mode; 5141558Srgrimes switch (mode & IFMT) { 5151558Srgrimes 5161558Srgrimes default: 5171558Srgrimes fprintf(stderr, "%s: unknown file mode 0%o\n", name, mode); 5181558Srgrimes skipfile(); 5191558Srgrimes return (FAIL); 5201558Srgrimes 5211558Srgrimes case IFSOCK: 5221558Srgrimes vprintf(stdout, "skipped socket %s\n", name); 5231558Srgrimes skipfile(); 5241558Srgrimes return (GOOD); 5251558Srgrimes 5261558Srgrimes case IFDIR: 5271558Srgrimes if (mflag) { 5281558Srgrimes ep = lookupname(name); 5291558Srgrimes if (ep == NULL || ep->e_flags & EXTRACT) 5301558Srgrimes panic("unextracted directory %s\n", name); 5311558Srgrimes skipfile(); 5321558Srgrimes return (GOOD); 5331558Srgrimes } 5341558Srgrimes vprintf(stdout, "extract file %s\n", name); 5351558Srgrimes return (genliteraldir(name, curfile.ino)); 5361558Srgrimes 5371558Srgrimes case IFLNK: 5381558Srgrimes lnkbuf[0] = '\0'; 5391558Srgrimes pathlen = 0; 5401558Srgrimes getfile(xtrlnkfile, xtrlnkskip); 5411558Srgrimes if (pathlen == 0) { 5421558Srgrimes vprintf(stdout, 5431558Srgrimes "%s: zero length symbolic link (ignored)\n", name); 5441558Srgrimes return (GOOD); 5451558Srgrimes } 5461558Srgrimes return (linkit(lnkbuf, name, SYMLINK)); 5471558Srgrimes 5486305Smartin case IFIFO: 5496305Smartin if (mkfifo(name, mode) < 0) { 5506305Smartin fprintf(stderr, "%s: cannot create FIFO: %s\n", 5516305Smartin name, strerror(errno)); 5526305Smartin skipfile(); 5536305Smartin return (FAIL); 5546305Smartin } 5556305Smartin (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid); 5566305Smartin (void) chmod(name, mode); 5576305Smartin skipfile(); 5586305Smartin utimes(name, timep); 5596305Smartin return (GOOD); 5606305Smartin 5611558Srgrimes case IFCHR: 5621558Srgrimes case IFBLK: 5631558Srgrimes vprintf(stdout, "extract special file %s\n", name); 5641558Srgrimes if (Nflag) { 5651558Srgrimes skipfile(); 5661558Srgrimes return (GOOD); 5671558Srgrimes } 5681558Srgrimes if (mknod(name, mode, (int)curfile.dip->di_rdev) < 0) { 5691558Srgrimes fprintf(stderr, "%s: cannot create special file: %s\n", 5701558Srgrimes name, strerror(errno)); 5711558Srgrimes skipfile(); 5721558Srgrimes return (FAIL); 5731558Srgrimes } 5741558Srgrimes (void) chown(name, curfile.dip->di_uid, curfile.dip->di_gid); 5751558Srgrimes (void) chmod(name, mode); 5761558Srgrimes skipfile(); 5771558Srgrimes utimes(name, timep); 5781558Srgrimes return (GOOD); 5791558Srgrimes 5801558Srgrimes case IFREG: 5811558Srgrimes vprintf(stdout, "extract file %s\n", name); 5821558Srgrimes if (Nflag) { 5831558Srgrimes skipfile(); 5841558Srgrimes return (GOOD); 5851558Srgrimes } 5861558Srgrimes if ((ofile = creat(name, 0666)) < 0) { 5871558Srgrimes fprintf(stderr, "%s: cannot create file: %s\n", 5881558Srgrimes name, strerror(errno)); 5891558Srgrimes skipfile(); 5901558Srgrimes return (FAIL); 5911558Srgrimes } 5921558Srgrimes (void) fchown(ofile, curfile.dip->di_uid, curfile.dip->di_gid); 5931558Srgrimes (void) fchmod(ofile, mode); 5941558Srgrimes getfile(xtrfile, xtrskip); 5951558Srgrimes (void) close(ofile); 5961558Srgrimes utimes(name, timep); 5971558Srgrimes return (GOOD); 5981558Srgrimes } 5991558Srgrimes /* NOTREACHED */ 6001558Srgrimes} 6011558Srgrimes 6021558Srgrimes/* 6031558Srgrimes * skip over bit maps on the tape 6041558Srgrimes */ 6051558Srgrimesvoid 6061558Srgrimesskipmaps() 6071558Srgrimes{ 6081558Srgrimes 6091558Srgrimes while (spcl.c_type == TS_BITS || spcl.c_type == TS_CLRI) 6101558Srgrimes skipfile(); 6111558Srgrimes} 6121558Srgrimes 6131558Srgrimes/* 6141558Srgrimes * skip over a file on the tape 6151558Srgrimes */ 6161558Srgrimesvoid 6171558Srgrimesskipfile() 6181558Srgrimes{ 6191558Srgrimes 6201558Srgrimes curfile.action = SKIP; 6211558Srgrimes getfile(xtrnull, xtrnull); 6221558Srgrimes} 6231558Srgrimes 6241558Srgrimes/* 6251558Srgrimes * Extract a file from the tape. 6261558Srgrimes * When an allocated block is found it is passed to the fill function; 6271558Srgrimes * when an unallocated block (hole) is found, a zeroed buffer is passed 6281558Srgrimes * to the skip function. 6291558Srgrimes */ 6301558Srgrimesvoid 6311558Srgrimesgetfile(fill, skip) 6321558Srgrimes void (*fill) __P((char *, long)); 6331558Srgrimes void (*skip) __P((char *, long)); 6341558Srgrimes{ 6351558Srgrimes register int i; 6361558Srgrimes int curblk = 0; 6371558Srgrimes long size = spcl.c_dinode.di_size; 6381558Srgrimes static char clearedbuf[MAXBSIZE]; 6391558Srgrimes char buf[MAXBSIZE / TP_BSIZE][TP_BSIZE]; 6401558Srgrimes char junk[TP_BSIZE]; 6411558Srgrimes 6421558Srgrimes if (spcl.c_type == TS_END) 6431558Srgrimes panic("ran off end of tape\n"); 6441558Srgrimes if (spcl.c_magic != NFS_MAGIC) 6451558Srgrimes panic("not at beginning of a file\n"); 6461558Srgrimes if (!gettingfile && setjmp(restart) != 0) 6471558Srgrimes return; 6481558Srgrimes gettingfile++; 6491558Srgrimesloop: 6501558Srgrimes for (i = 0; i < spcl.c_count; i++) { 6511558Srgrimes if (spcl.c_addr[i]) { 6521558Srgrimes readtape(&buf[curblk++][0]); 6531558Srgrimes if (curblk == fssize / TP_BSIZE) { 6541558Srgrimes (*fill)((char *)buf, size > TP_BSIZE ? 6551558Srgrimes (long) (fssize) : 6561558Srgrimes (curblk - 1) * TP_BSIZE + size); 6571558Srgrimes curblk = 0; 6581558Srgrimes } 6591558Srgrimes } else { 6601558Srgrimes if (curblk > 0) { 6611558Srgrimes (*fill)((char *)buf, size > TP_BSIZE ? 6621558Srgrimes (long) (curblk * TP_BSIZE) : 6631558Srgrimes (curblk - 1) * TP_BSIZE + size); 6641558Srgrimes curblk = 0; 6651558Srgrimes } 6661558Srgrimes (*skip)(clearedbuf, size > TP_BSIZE ? 6671558Srgrimes (long) TP_BSIZE : size); 6681558Srgrimes } 6691558Srgrimes if ((size -= TP_BSIZE) <= 0) { 6701558Srgrimes for (i++; i < spcl.c_count; i++) 6711558Srgrimes if (spcl.c_addr[i]) 6721558Srgrimes readtape(junk); 6731558Srgrimes break; 6741558Srgrimes } 6751558Srgrimes } 6761558Srgrimes if (gethead(&spcl) == GOOD && size > 0) { 6771558Srgrimes if (spcl.c_type == TS_ADDR) 6781558Srgrimes goto loop; 6791558Srgrimes dprintf(stdout, 6801558Srgrimes "Missing address (header) block for %s at %d blocks\n", 6811558Srgrimes curfile.name, blksread); 6821558Srgrimes } 6831558Srgrimes if (curblk > 0) 6841558Srgrimes (*fill)((char *)buf, (curblk * TP_BSIZE) + size); 6851558Srgrimes findinode(&spcl); 6861558Srgrimes gettingfile = 0; 6871558Srgrimes} 6881558Srgrimes 6891558Srgrimes/* 6901558Srgrimes * Write out the next block of a file. 6911558Srgrimes */ 6921558Srgrimesstatic void 6931558Srgrimesxtrfile(buf, size) 6941558Srgrimes char *buf; 6951558Srgrimes long size; 6961558Srgrimes{ 6971558Srgrimes 6981558Srgrimes if (Nflag) 6991558Srgrimes return; 7001558Srgrimes if (write(ofile, buf, (int) size) == -1) { 7011558Srgrimes fprintf(stderr, 7021558Srgrimes "write error extracting inode %d, name %s\nwrite: %s\n", 7031558Srgrimes curfile.ino, curfile.name, strerror(errno)); 7041558Srgrimes done(1); 7051558Srgrimes } 7061558Srgrimes} 7071558Srgrimes 7081558Srgrimes/* 7091558Srgrimes * Skip over a hole in a file. 7101558Srgrimes */ 7111558Srgrimes/* ARGSUSED */ 7121558Srgrimesstatic void 7131558Srgrimesxtrskip(buf, size) 7141558Srgrimes char *buf; 7151558Srgrimes long size; 7161558Srgrimes{ 7171558Srgrimes 7181558Srgrimes if (lseek(ofile, size, SEEK_CUR) == -1) { 7191558Srgrimes fprintf(stderr, 7201558Srgrimes "seek error extracting inode %d, name %s\nlseek: %s\n", 7211558Srgrimes curfile.ino, curfile.name, strerror(errno)); 7221558Srgrimes done(1); 7231558Srgrimes } 7241558Srgrimes} 7251558Srgrimes 7261558Srgrimes/* 7271558Srgrimes * Collect the next block of a symbolic link. 7281558Srgrimes */ 7291558Srgrimesstatic void 7301558Srgrimesxtrlnkfile(buf, size) 7311558Srgrimes char *buf; 7321558Srgrimes long size; 7331558Srgrimes{ 7341558Srgrimes 7351558Srgrimes pathlen += size; 7361558Srgrimes if (pathlen > MAXPATHLEN) { 7371558Srgrimes fprintf(stderr, "symbolic link name: %s->%s%s; too long %d\n", 7381558Srgrimes curfile.name, lnkbuf, buf, pathlen); 7391558Srgrimes done(1); 7401558Srgrimes } 7411558Srgrimes (void) strcat(lnkbuf, buf); 7421558Srgrimes} 7431558Srgrimes 7441558Srgrimes/* 7451558Srgrimes * Skip over a hole in a symbolic link (should never happen). 7461558Srgrimes */ 7471558Srgrimes/* ARGSUSED */ 7481558Srgrimesstatic void 7491558Srgrimesxtrlnkskip(buf, size) 7501558Srgrimes char *buf; 7511558Srgrimes long size; 7521558Srgrimes{ 7531558Srgrimes 7541558Srgrimes fprintf(stderr, "unallocated block in symbolic link %s\n", 7551558Srgrimes curfile.name); 7561558Srgrimes done(1); 7571558Srgrimes} 7581558Srgrimes 7591558Srgrimes/* 7601558Srgrimes * Collect the next block of a bit map. 7611558Srgrimes */ 7621558Srgrimesstatic void 7631558Srgrimesxtrmap(buf, size) 7641558Srgrimes char *buf; 7651558Srgrimes long size; 7661558Srgrimes{ 7671558Srgrimes 7681558Srgrimes bcopy(buf, map, size); 7691558Srgrimes map += size; 7701558Srgrimes} 7711558Srgrimes 7721558Srgrimes/* 7731558Srgrimes * Skip over a hole in a bit map (should never happen). 7741558Srgrimes */ 7751558Srgrimes/* ARGSUSED */ 7761558Srgrimesstatic void 7771558Srgrimesxtrmapskip(buf, size) 7781558Srgrimes char *buf; 7791558Srgrimes long size; 7801558Srgrimes{ 7811558Srgrimes 7821558Srgrimes panic("hole in map\n"); 7831558Srgrimes map += size; 7841558Srgrimes} 7851558Srgrimes 7861558Srgrimes/* 7871558Srgrimes * Noop, when an extraction function is not needed. 7881558Srgrimes */ 7891558Srgrimes/* ARGSUSED */ 7901558Srgrimesvoid 7911558Srgrimesxtrnull(buf, size) 7921558Srgrimes char *buf; 7931558Srgrimes long size; 7941558Srgrimes{ 7951558Srgrimes 7961558Srgrimes return; 7971558Srgrimes} 7981558Srgrimes 7991558Srgrimes/* 8001558Srgrimes * Read TP_BSIZE blocks from the input. 8011558Srgrimes * Handle read errors, and end of media. 8021558Srgrimes */ 8031558Srgrimesstatic void 8041558Srgrimesreadtape(buf) 8051558Srgrimes char *buf; 8061558Srgrimes{ 8071558Srgrimes long rd, newvol, i; 8081558Srgrimes int cnt, seek_failed; 8091558Srgrimes 8101558Srgrimes if (blkcnt < numtrec) { 8111558Srgrimes bcopy(&tapebuf[(blkcnt++ * TP_BSIZE)], buf, (long)TP_BSIZE); 8121558Srgrimes blksread++; 8131558Srgrimes tpblksread++; 8141558Srgrimes return; 8151558Srgrimes } 8161558Srgrimes for (i = 0; i < ntrec; i++) 8171558Srgrimes ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; 8181558Srgrimes if (numtrec == 0) 8191558Srgrimes numtrec = ntrec; 8201558Srgrimes cnt = ntrec * TP_BSIZE; 8211558Srgrimes rd = 0; 8221558Srgrimesgetmore: 8231558Srgrimes#ifdef RRESTORE 8241558Srgrimes if (host) 8251558Srgrimes i = rmtread(&tapebuf[rd], cnt); 8261558Srgrimes else 8271558Srgrimes#endif 8281558Srgrimes i = read(mt, &tapebuf[rd], cnt); 8291558Srgrimes /* 8301558Srgrimes * Check for mid-tape short read error. 8311558Srgrimes * If found, skip rest of buffer and start with the next. 8321558Srgrimes */ 8331558Srgrimes if (!pipein && numtrec < ntrec && i > 0) { 8341558Srgrimes dprintf(stdout, "mid-media short read error.\n"); 8351558Srgrimes numtrec = ntrec; 8361558Srgrimes } 8371558Srgrimes /* 8381558Srgrimes * Handle partial block read. 8391558Srgrimes */ 8401558Srgrimes if (pipein && i == 0 && rd > 0) 8411558Srgrimes i = rd; 8421558Srgrimes else if (i > 0 && i != ntrec * TP_BSIZE) { 8431558Srgrimes if (pipein) { 8441558Srgrimes rd += i; 8451558Srgrimes cnt -= i; 8461558Srgrimes if (cnt > 0) 8471558Srgrimes goto getmore; 8481558Srgrimes i = rd; 8491558Srgrimes } else { 8501558Srgrimes /* 8511558Srgrimes * Short read. Process the blocks read. 8521558Srgrimes */ 8531558Srgrimes if (i % TP_BSIZE != 0) 8541558Srgrimes vprintf(stdout, 8551558Srgrimes "partial block read: %d should be %d\n", 8561558Srgrimes i, ntrec * TP_BSIZE); 8571558Srgrimes numtrec = i / TP_BSIZE; 8581558Srgrimes } 8591558Srgrimes } 8601558Srgrimes /* 8611558Srgrimes * Handle read error. 8621558Srgrimes */ 8631558Srgrimes if (i < 0) { 8641558Srgrimes fprintf(stderr, "Tape read error while "); 8651558Srgrimes switch (curfile.action) { 8661558Srgrimes default: 8671558Srgrimes fprintf(stderr, "trying to set up tape\n"); 8681558Srgrimes break; 8691558Srgrimes case UNKNOWN: 8701558Srgrimes fprintf(stderr, "trying to resynchronize\n"); 8711558Srgrimes break; 8721558Srgrimes case USING: 8731558Srgrimes fprintf(stderr, "restoring %s\n", curfile.name); 8741558Srgrimes break; 8751558Srgrimes case SKIP: 8761558Srgrimes fprintf(stderr, "skipping over inode %d\n", 8771558Srgrimes curfile.ino); 8781558Srgrimes break; 8791558Srgrimes } 8801558Srgrimes if (!yflag && !reply("continue")) 8811558Srgrimes done(1); 8821558Srgrimes i = ntrec * TP_BSIZE; 8831558Srgrimes bzero(tapebuf, i); 8841558Srgrimes#ifdef RRESTORE 8851558Srgrimes if (host) 8861558Srgrimes seek_failed = (rmtseek(i, 1) < 0); 8871558Srgrimes else 8881558Srgrimes#endif 8891558Srgrimes seek_failed = (lseek(mt, i, SEEK_CUR) == (off_t)-1); 8901558Srgrimes 8911558Srgrimes if (seek_failed) { 8921558Srgrimes fprintf(stderr, 8931558Srgrimes "continuation failed: %s\n", strerror(errno)); 8941558Srgrimes done(1); 8951558Srgrimes } 8961558Srgrimes } 8971558Srgrimes /* 8981558Srgrimes * Handle end of tape. 8991558Srgrimes */ 9001558Srgrimes if (i == 0) { 9011558Srgrimes vprintf(stdout, "End-of-tape encountered\n"); 9021558Srgrimes if (!pipein) { 9031558Srgrimes newvol = volno + 1; 9041558Srgrimes volno = 0; 9051558Srgrimes numtrec = 0; 9061558Srgrimes getvol(newvol); 9071558Srgrimes readtape(buf); 9081558Srgrimes return; 9091558Srgrimes } 9101558Srgrimes if (rd % TP_BSIZE != 0) 9111558Srgrimes panic("partial block read: %d should be %d\n", 9121558Srgrimes rd, ntrec * TP_BSIZE); 9131558Srgrimes terminateinput(); 9141558Srgrimes bcopy((char *)&endoftapemark, &tapebuf[rd], (long)TP_BSIZE); 9151558Srgrimes } 9161558Srgrimes blkcnt = 0; 9171558Srgrimes bcopy(&tapebuf[(blkcnt++ * TP_BSIZE)], buf, (long)TP_BSIZE); 9181558Srgrimes blksread++; 9191558Srgrimes tpblksread++; 9201558Srgrimes} 9211558Srgrimes 9221558Srgrimesstatic void 9231558Srgrimesfindtapeblksize() 9241558Srgrimes{ 9251558Srgrimes register long i; 9261558Srgrimes 9271558Srgrimes for (i = 0; i < ntrec; i++) 9281558Srgrimes ((struct s_spcl *)&tapebuf[i * TP_BSIZE])->c_magic = 0; 9291558Srgrimes blkcnt = 0; 9301558Srgrimes#ifdef RRESTORE 9311558Srgrimes if (host) 9321558Srgrimes i = rmtread(tapebuf, ntrec * TP_BSIZE); 9331558Srgrimes else 9341558Srgrimes#endif 9351558Srgrimes i = read(mt, tapebuf, ntrec * TP_BSIZE); 9361558Srgrimes 9371558Srgrimes if (i <= 0) { 9381558Srgrimes fprintf(stderr, "tape read error: %s\n", strerror(errno)); 9391558Srgrimes done(1); 9401558Srgrimes } 9411558Srgrimes if (i % TP_BSIZE != 0) { 9421558Srgrimes fprintf(stderr, "Tape block size (%d) %s (%d)\n", 9431558Srgrimes i, "is not a multiple of dump block size", TP_BSIZE); 9441558Srgrimes done(1); 9451558Srgrimes } 9461558Srgrimes ntrec = i / TP_BSIZE; 9471558Srgrimes numtrec = ntrec; 9481558Srgrimes vprintf(stdout, "Tape block size is %d\n", ntrec); 9491558Srgrimes} 9501558Srgrimes 9511558Srgrimesvoid 9521558Srgrimesclosemt() 9531558Srgrimes{ 9541558Srgrimes 9551558Srgrimes if (mt < 0) 9561558Srgrimes return; 9571558Srgrimes#ifdef RRESTORE 9581558Srgrimes if (host) 9591558Srgrimes rmtclose(); 9601558Srgrimes else 9611558Srgrimes#endif 9621558Srgrimes (void) close(mt); 9631558Srgrimes} 9641558Srgrimes 9651558Srgrimes/* 9661558Srgrimes * Read the next block from the tape. 9671558Srgrimes * Check to see if it is one of several vintage headers. 9681558Srgrimes * If it is an old style header, convert it to a new style header. 9691558Srgrimes * If it is not any valid header, return an error. 9701558Srgrimes */ 9711558Srgrimesstatic int 9721558Srgrimesgethead(buf) 9731558Srgrimes struct s_spcl *buf; 9741558Srgrimes{ 9751558Srgrimes long i; 9761558Srgrimes union { 9771558Srgrimes quad_t qval; 9781558Srgrimes long val[2]; 9791558Srgrimes } qcvt; 9801558Srgrimes union u_ospcl { 9811558Srgrimes char dummy[TP_BSIZE]; 9821558Srgrimes struct s_ospcl { 9831558Srgrimes long c_type; 9841558Srgrimes long c_date; 9851558Srgrimes long c_ddate; 9861558Srgrimes long c_volume; 9871558Srgrimes long c_tapea; 9881558Srgrimes u_short c_inumber; 9891558Srgrimes long c_magic; 9901558Srgrimes long c_checksum; 9911558Srgrimes struct odinode { 9921558Srgrimes unsigned short odi_mode; 9931558Srgrimes u_short odi_nlink; 9941558Srgrimes u_short odi_uid; 9951558Srgrimes u_short odi_gid; 9961558Srgrimes long odi_size; 9971558Srgrimes long odi_rdev; 9981558Srgrimes char odi_addr[36]; 9991558Srgrimes long odi_atime; 10001558Srgrimes long odi_mtime; 10011558Srgrimes long odi_ctime; 10021558Srgrimes } c_dinode; 10031558Srgrimes long c_count; 10041558Srgrimes char c_addr[256]; 10051558Srgrimes } s_ospcl; 10061558Srgrimes } u_ospcl; 10071558Srgrimes 10081558Srgrimes if (!cvtflag) { 10091558Srgrimes readtape((char *)buf); 10101558Srgrimes if (buf->c_magic != NFS_MAGIC) { 10111558Srgrimes if (swabl(buf->c_magic) != NFS_MAGIC) 10121558Srgrimes return (FAIL); 10131558Srgrimes if (!Bcvt) { 10141558Srgrimes vprintf(stdout, "Note: Doing Byte swapping\n"); 10151558Srgrimes Bcvt = 1; 10161558Srgrimes } 10171558Srgrimes } 10181558Srgrimes if (checksum((int *)buf) == FAIL) 10191558Srgrimes return (FAIL); 10201558Srgrimes if (Bcvt) 10211558Srgrimes swabst((u_char *)"8l4s31l", (u_char *)buf); 10221558Srgrimes goto good; 10231558Srgrimes } 10241558Srgrimes readtape((char *)(&u_ospcl.s_ospcl)); 10251558Srgrimes bzero((char *)buf, (long)TP_BSIZE); 10261558Srgrimes buf->c_type = u_ospcl.s_ospcl.c_type; 10271558Srgrimes buf->c_date = u_ospcl.s_ospcl.c_date; 10281558Srgrimes buf->c_ddate = u_ospcl.s_ospcl.c_ddate; 10291558Srgrimes buf->c_volume = u_ospcl.s_ospcl.c_volume; 10301558Srgrimes buf->c_tapea = u_ospcl.s_ospcl.c_tapea; 10311558Srgrimes buf->c_inumber = u_ospcl.s_ospcl.c_inumber; 10321558Srgrimes buf->c_checksum = u_ospcl.s_ospcl.c_checksum; 10331558Srgrimes buf->c_magic = u_ospcl.s_ospcl.c_magic; 10341558Srgrimes buf->c_dinode.di_mode = u_ospcl.s_ospcl.c_dinode.odi_mode; 10351558Srgrimes buf->c_dinode.di_nlink = u_ospcl.s_ospcl.c_dinode.odi_nlink; 10361558Srgrimes buf->c_dinode.di_uid = u_ospcl.s_ospcl.c_dinode.odi_uid; 10371558Srgrimes buf->c_dinode.di_gid = u_ospcl.s_ospcl.c_dinode.odi_gid; 10381558Srgrimes buf->c_dinode.di_size = u_ospcl.s_ospcl.c_dinode.odi_size; 10391558Srgrimes buf->c_dinode.di_rdev = u_ospcl.s_ospcl.c_dinode.odi_rdev; 10401558Srgrimes buf->c_dinode.di_atime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_atime; 10411558Srgrimes buf->c_dinode.di_mtime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_mtime; 10421558Srgrimes buf->c_dinode.di_ctime.ts_sec = u_ospcl.s_ospcl.c_dinode.odi_ctime; 10431558Srgrimes buf->c_count = u_ospcl.s_ospcl.c_count; 10441558Srgrimes bcopy(u_ospcl.s_ospcl.c_addr, buf->c_addr, (long)256); 10451558Srgrimes if (u_ospcl.s_ospcl.c_magic != OFS_MAGIC || 10461558Srgrimes checksum((int *)(&u_ospcl.s_ospcl)) == FAIL) 10471558Srgrimes return(FAIL); 10481558Srgrimes buf->c_magic = NFS_MAGIC; 10491558Srgrimes 10501558Srgrimesgood: 10511558Srgrimes if ((buf->c_dinode.di_size == 0 || buf->c_dinode.di_size > 0xfffffff) && 10521558Srgrimes (buf->c_dinode.di_mode & IFMT) == IFDIR && Qcvt == 0) { 10531558Srgrimes qcvt.qval = buf->c_dinode.di_size; 10541558Srgrimes if (qcvt.val[0] || qcvt.val[1]) { 10551558Srgrimes printf("Note: Doing Quad swapping\n"); 10561558Srgrimes Qcvt = 1; 10571558Srgrimes } 10581558Srgrimes } 10591558Srgrimes if (Qcvt) { 10601558Srgrimes qcvt.qval = buf->c_dinode.di_size; 10611558Srgrimes i = qcvt.val[1]; 10621558Srgrimes qcvt.val[1] = qcvt.val[0]; 10631558Srgrimes qcvt.val[0] = i; 10641558Srgrimes buf->c_dinode.di_size = qcvt.qval; 10651558Srgrimes } 10661558Srgrimes 10671558Srgrimes switch (buf->c_type) { 10681558Srgrimes 10691558Srgrimes case TS_CLRI: 10701558Srgrimes case TS_BITS: 10711558Srgrimes /* 10721558Srgrimes * Have to patch up missing information in bit map headers 10731558Srgrimes */ 10741558Srgrimes buf->c_inumber = 0; 10751558Srgrimes buf->c_dinode.di_size = buf->c_count * TP_BSIZE; 10761558Srgrimes for (i = 0; i < buf->c_count; i++) 10771558Srgrimes buf->c_addr[i]++; 10781558Srgrimes break; 10791558Srgrimes 10801558Srgrimes case TS_TAPE: 10811558Srgrimes if ((buf->c_flags & DR_NEWINODEFMT) == 0) 10821558Srgrimes oldinofmt = 1; 10831558Srgrimes /* fall through */ 10841558Srgrimes case TS_END: 10851558Srgrimes buf->c_inumber = 0; 10861558Srgrimes break; 10871558Srgrimes 10881558Srgrimes case TS_INODE: 10891558Srgrimes case TS_ADDR: 10901558Srgrimes break; 10911558Srgrimes 10921558Srgrimes default: 10931558Srgrimes panic("gethead: unknown inode type %d\n", buf->c_type); 10941558Srgrimes break; 10951558Srgrimes } 10961558Srgrimes /* 10978871Srgrimes * If we are restoring a filesystem with old format inodes, 10981558Srgrimes * copy the uid/gid to the new location. 10991558Srgrimes */ 11001558Srgrimes if (oldinofmt) { 11011558Srgrimes buf->c_dinode.di_uid = buf->c_dinode.di_ouid; 11021558Srgrimes buf->c_dinode.di_gid = buf->c_dinode.di_ogid; 11031558Srgrimes } 11041558Srgrimes if (dflag) 11051558Srgrimes accthdr(buf); 11061558Srgrimes return(GOOD); 11071558Srgrimes} 11081558Srgrimes 11091558Srgrimes/* 11101558Srgrimes * Check that a header is where it belongs and predict the next header 11111558Srgrimes */ 11121558Srgrimesstatic void 11131558Srgrimesaccthdr(header) 11141558Srgrimes struct s_spcl *header; 11151558Srgrimes{ 11161558Srgrimes static ino_t previno = 0x7fffffff; 11171558Srgrimes static int prevtype; 11181558Srgrimes static long predict; 11191558Srgrimes long blks, i; 11201558Srgrimes 11211558Srgrimes if (header->c_type == TS_TAPE) { 11221558Srgrimes fprintf(stderr, "Volume header (%s inode format) ", 11231558Srgrimes oldinofmt ? "old" : "new"); 11241558Srgrimes if (header->c_firstrec) 11251558Srgrimes fprintf(stderr, "begins with record %d", 11261558Srgrimes header->c_firstrec); 11271558Srgrimes fprintf(stderr, "\n"); 11281558Srgrimes previno = 0x7fffffff; 11291558Srgrimes return; 11301558Srgrimes } 11311558Srgrimes if (previno == 0x7fffffff) 11321558Srgrimes goto newcalc; 11331558Srgrimes switch (prevtype) { 11341558Srgrimes case TS_BITS: 11351558Srgrimes fprintf(stderr, "Dump mask header"); 11361558Srgrimes break; 11371558Srgrimes case TS_CLRI: 11381558Srgrimes fprintf(stderr, "Remove mask header"); 11391558Srgrimes break; 11401558Srgrimes case TS_INODE: 11411558Srgrimes fprintf(stderr, "File header, ino %d", previno); 11421558Srgrimes break; 11431558Srgrimes case TS_ADDR: 11441558Srgrimes fprintf(stderr, "File continuation header, ino %d", previno); 11451558Srgrimes break; 11461558Srgrimes case TS_END: 11471558Srgrimes fprintf(stderr, "End of tape header"); 11481558Srgrimes break; 11491558Srgrimes } 11501558Srgrimes if (predict != blksread - 1) 11511558Srgrimes fprintf(stderr, "; predicted %d blocks, got %d blocks", 11521558Srgrimes predict, blksread - 1); 11531558Srgrimes fprintf(stderr, "\n"); 11541558Srgrimesnewcalc: 11551558Srgrimes blks = 0; 11561558Srgrimes if (header->c_type != TS_END) 11571558Srgrimes for (i = 0; i < header->c_count; i++) 11581558Srgrimes if (header->c_addr[i] != 0) 11591558Srgrimes blks++; 11601558Srgrimes predict = blks; 11611558Srgrimes blksread = 0; 11621558Srgrimes prevtype = header->c_type; 11631558Srgrimes previno = header->c_inumber; 11641558Srgrimes} 11651558Srgrimes 11661558Srgrimes/* 11671558Srgrimes * Find an inode header. 11681558Srgrimes * Complain if had to skip, and complain is set. 11691558Srgrimes */ 11701558Srgrimesstatic void 11711558Srgrimesfindinode(header) 11721558Srgrimes struct s_spcl *header; 11731558Srgrimes{ 11741558Srgrimes static long skipcnt = 0; 11751558Srgrimes long i; 11761558Srgrimes char buf[TP_BSIZE]; 11771558Srgrimes 11781558Srgrimes curfile.name = "<name unknown>"; 11791558Srgrimes curfile.action = UNKNOWN; 11801558Srgrimes curfile.dip = NULL; 11811558Srgrimes curfile.ino = 0; 11821558Srgrimes do { 11831558Srgrimes if (header->c_magic != NFS_MAGIC) { 11841558Srgrimes skipcnt++; 11851558Srgrimes while (gethead(header) == FAIL || 11861558Srgrimes header->c_date != dumpdate) 11871558Srgrimes skipcnt++; 11881558Srgrimes } 11891558Srgrimes switch (header->c_type) { 11901558Srgrimes 11911558Srgrimes case TS_ADDR: 11921558Srgrimes /* 11931558Srgrimes * Skip up to the beginning of the next record 11941558Srgrimes */ 11951558Srgrimes for (i = 0; i < header->c_count; i++) 11961558Srgrimes if (header->c_addr[i]) 11971558Srgrimes readtape(buf); 11981558Srgrimes while (gethead(header) == FAIL || 11991558Srgrimes header->c_date != dumpdate) 12001558Srgrimes skipcnt++; 12011558Srgrimes break; 12021558Srgrimes 12031558Srgrimes case TS_INODE: 12041558Srgrimes curfile.dip = &header->c_dinode; 12051558Srgrimes curfile.ino = header->c_inumber; 12061558Srgrimes break; 12071558Srgrimes 12081558Srgrimes case TS_END: 12091558Srgrimes curfile.ino = maxino; 12101558Srgrimes break; 12111558Srgrimes 12121558Srgrimes case TS_CLRI: 12131558Srgrimes curfile.name = "<file removal list>"; 12141558Srgrimes break; 12151558Srgrimes 12161558Srgrimes case TS_BITS: 12171558Srgrimes curfile.name = "<file dump list>"; 12181558Srgrimes break; 12191558Srgrimes 12201558Srgrimes case TS_TAPE: 12211558Srgrimes panic("unexpected tape header\n"); 12221558Srgrimes /* NOTREACHED */ 12231558Srgrimes 12241558Srgrimes default: 12251558Srgrimes panic("unknown tape header type %d\n", spcl.c_type); 12261558Srgrimes /* NOTREACHED */ 12271558Srgrimes 12281558Srgrimes } 12291558Srgrimes } while (header->c_type == TS_ADDR); 12301558Srgrimes if (skipcnt > 0) 12311558Srgrimes fprintf(stderr, "resync restore, skipped %d blocks\n", skipcnt); 12321558Srgrimes skipcnt = 0; 12331558Srgrimes} 12341558Srgrimes 12351558Srgrimesstatic int 12361558Srgrimeschecksum(buf) 12371558Srgrimes register int *buf; 12381558Srgrimes{ 12391558Srgrimes register int i, j; 12401558Srgrimes 12411558Srgrimes j = sizeof(union u_spcl) / sizeof(int); 12421558Srgrimes i = 0; 12431558Srgrimes if(!Bcvt) { 12441558Srgrimes do 12451558Srgrimes i += *buf++; 12461558Srgrimes while (--j); 12471558Srgrimes } else { 12481558Srgrimes /* What happens if we want to read restore tapes 12491558Srgrimes for a 16bit int machine??? */ 12508871Srgrimes do 12511558Srgrimes i += swabl(*buf++); 12521558Srgrimes while (--j); 12531558Srgrimes } 12548871Srgrimes 12551558Srgrimes if (i != CHECKSUM) { 12561558Srgrimes fprintf(stderr, "Checksum error %o, inode %d file %s\n", i, 12571558Srgrimes curfile.ino, curfile.name); 12581558Srgrimes return(FAIL); 12591558Srgrimes } 12601558Srgrimes return(GOOD); 12611558Srgrimes} 12621558Srgrimes 12631558Srgrimes#ifdef RRESTORE 12641558Srgrimes#if __STDC__ 12651558Srgrimes#include <stdarg.h> 12661558Srgrimes#else 12671558Srgrimes#include <varargs.h> 12681558Srgrimes#endif 12691558Srgrimes 12701558Srgrimesvoid 12711558Srgrimes#if __STDC__ 12721558Srgrimesmsg(const char *fmt, ...) 12731558Srgrimes#else 12741558Srgrimesmsg(fmt, va_alist) 12751558Srgrimes char *fmt; 12761558Srgrimes va_dcl 12771558Srgrimes#endif 12781558Srgrimes{ 12791558Srgrimes va_list ap; 12801558Srgrimes#if __STDC__ 12811558Srgrimes va_start(ap, fmt); 12821558Srgrimes#else 12831558Srgrimes va_start(ap); 12841558Srgrimes#endif 12851558Srgrimes (void)vfprintf(stderr, fmt, ap); 12861558Srgrimes va_end(ap); 12871558Srgrimes} 12881558Srgrimes#endif /* RRESTORE */ 12891558Srgrimes 12901558Srgrimesstatic u_char * 12911558Srgrimesswabshort(sp, n) 12921558Srgrimes register u_char *sp; 12931558Srgrimes register int n; 12941558Srgrimes{ 12951558Srgrimes char c; 12961558Srgrimes 12971558Srgrimes while (--n >= 0) { 12981558Srgrimes c = sp[0]; sp[0] = sp[1]; sp[1] = c; 12991558Srgrimes sp += 2; 13001558Srgrimes } 13011558Srgrimes return (sp); 13021558Srgrimes} 13031558Srgrimes 13041558Srgrimesstatic u_char * 13051558Srgrimesswablong(sp, n) 13061558Srgrimes register u_char *sp; 13071558Srgrimes register int n; 13081558Srgrimes{ 13091558Srgrimes char c; 13101558Srgrimes 13111558Srgrimes while (--n >= 0) { 13121558Srgrimes c = sp[0]; sp[0] = sp[3]; sp[3] = c; 13131558Srgrimes c = sp[2]; sp[2] = sp[1]; sp[1] = c; 13141558Srgrimes sp += 4; 13151558Srgrimes } 13161558Srgrimes return (sp); 13171558Srgrimes} 13181558Srgrimes 13191558Srgrimesvoid 13201558Srgrimesswabst(cp, sp) 13211558Srgrimes register u_char *cp, *sp; 13221558Srgrimes{ 13231558Srgrimes int n = 0; 13241558Srgrimes 13251558Srgrimes while (*cp) { 13261558Srgrimes switch (*cp) { 13271558Srgrimes case '0': case '1': case '2': case '3': case '4': 13281558Srgrimes case '5': case '6': case '7': case '8': case '9': 13291558Srgrimes n = (n * 10) + (*cp++ - '0'); 13301558Srgrimes continue; 13318871Srgrimes 13321558Srgrimes case 's': case 'w': case 'h': 13331558Srgrimes if (n == 0) 13341558Srgrimes n = 1; 13351558Srgrimes sp = swabshort(sp, n); 13361558Srgrimes break; 13371558Srgrimes 13381558Srgrimes case 'l': 13391558Srgrimes if (n == 0) 13401558Srgrimes n = 1; 13411558Srgrimes sp = swablong(sp, n); 13421558Srgrimes break; 13431558Srgrimes 13441558Srgrimes default: /* Any other character, like 'b' counts as byte. */ 13451558Srgrimes if (n == 0) 13461558Srgrimes n = 1; 13471558Srgrimes sp += n; 13481558Srgrimes break; 13491558Srgrimes } 13501558Srgrimes cp++; 13511558Srgrimes n = 0; 13521558Srgrimes } 13531558Srgrimes} 13541558Srgrimes 13551558Srgrimesstatic u_long 13561558Srgrimesswabl(x) 13571558Srgrimes u_long x; 13581558Srgrimes{ 13591558Srgrimes swabst((u_char *)"l", (u_char *)&x); 13601558Srgrimes return (x); 13611558Srgrimes} 1362