xref: /csrg-svn/sbin/dump/tape.c (revision 50505)
147082Smckusick /*-
247082Smckusick  * Copyright (c) 1980, 1991 The Regents of the University of California.
347082Smckusick  * All rights reserved.
447082Smckusick  *
547082Smckusick  * %sccs.include.redist.c%
622040Sdist  */
722040Sdist 
817527Ssam #ifndef lint
9*50505Smckusick static char sccsid[] = "@(#)tape.c	5.20 (Berkeley) 07/23/91";
1046585Storek #endif /* not lint */
1117527Ssam 
12*50505Smckusick #ifdef sunos
13*50505Smckusick #include <stdio.h>
14*50505Smckusick #include <ctype.h>
15*50505Smckusick #include <sys/stat.h>
16*50505Smckusick #include <sys/time.h>
17*50505Smckusick #include <sys/dir.h>
18*50505Smckusick #include <sys/vnode.h>
19*50505Smckusick #include <ufs/inode.h>
20*50505Smckusick #else
2146795Sbostic #include <sys/param.h>
2246585Storek #include <sys/wait.h>
2346795Sbostic #include <ufs/dinode.h>
24*50505Smckusick #endif
2546795Sbostic #include <ufs/fs.h>
2646795Sbostic #include <signal.h>
2746795Sbostic #include <fcntl.h>
2846795Sbostic #include <protocols/dumprestore.h>
2946585Storek #include <errno.h>
3050504Smckusick #include <setjmp.h>
3146795Sbostic #ifdef __STDC__
3246795Sbostic #include <unistd.h>
3346795Sbostic #include <stdlib.h>
3446795Sbostic #include <string.h>
3546795Sbostic #endif
3650504Smckusick #include <sys/socket.h>
3746795Sbostic #include "dump.h"
3839128Smckusick #include "pathnames.h"
391425Sroot 
4029899Smckusick int	writesize;		/* size of malloc()ed buffer for tape */
4129899Smckusick long	lastspclrec = -1;	/* tape block number of last written header */
4229899Smckusick int	trecno = 0;		/* next record to write in current block */
4346614Smckusick extern	long blocksperfile;	/* number of blocks per output file */
4448621Skarels long	blocksthisvol;		/* number of blocks on current output file */
4548621Skarels extern	int ntrec;		/* blocking factor on tape */
4648621Skarels extern	int cartridge;
4750504Smckusick extern	char *host;
4847056Skarels char	*nexttape;
4925219Smckusick #ifdef RDUMP
5046585Storek int	rmtopen(), rmtwrite();
5146585Storek void	rmtclose();
5225219Smckusick #endif RDUMP
5350504Smckusick void	rollforward();
5446585Storek int	atomic();
5546789Smckusick void	doslave(), enslave(), flushtape(), killall();
5646585Storek 
5710911Ssam /*
5824181Smckusick  * Concurrent dump mods (Caltech) - disk block reading and tape writing
5918012Smckusick  * are exported to several slave processes.  While one slave writes the
6018012Smckusick  * tape, the others read disk blocks; they pass control of the tape in
6150504Smckusick  * a ring via signals. The parent process traverses the filesystem and
6246789Smckusick  * sends writeheader()'s and lists of daddr's to the slaves via pipes.
6350504Smckusick  * The following structure defines the instruction packets sent to slaves.
6410911Ssam  */
6550504Smckusick struct req {
6618012Smckusick 	daddr_t dblk;
6718012Smckusick 	int count;
6850504Smckusick };
6918012Smckusick int reqsiz;
7018012Smckusick 
7124181Smckusick #define SLAVES 3		/* 1 slave writing, 1 reading, 1 for slack */
7250504Smckusick struct slave {
7350504Smckusick 	int tapea;		/* header number at start of this chunk */
7450504Smckusick 	int count;		/* count to next header (used for TS_TAPE */
7550504Smckusick 				/* after EOT) */
7650504Smckusick 	int inode;		/* inode that we are currently dealing with */
7750504Smckusick 	int fd;			/* FD for this slave */
7850504Smckusick 	int pid;		/* PID for this slave */
7950504Smckusick 	int sent;		/* 1 == we've sent this slave requests */
8050504Smckusick 	int firstrec;		/* record number of this block */
8150504Smckusick 	char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
8250504Smckusick 	struct req *req;	/* buffer for requests */
8350504Smckusick } slaves[SLAVES+1];
8450504Smckusick struct slave *slp;
8518012Smckusick 
8650504Smckusick char	(*nextblock)[TP_BSIZE];
8750504Smckusick 
8850504Smckusick int master;		/* pid of master, for sending error signals */
8950504Smckusick int tenths;		/* length of tape used per block written */
9050504Smckusick static int caught;	/* have we caught the signal to proceed? */
9150504Smckusick static int ready;	/* have we reached the lock point without having */
9250504Smckusick 			/* received the SIGUSR2 signal from the prev slave? */
9350504Smckusick static jmp_buf jmpbuf;	/* where to jump to if we are ready when the */
9450504Smckusick 			/* SIGUSR2 arrives from the previous slave */
9550504Smckusick 
9646585Storek int
9710911Ssam alloctape()
9810911Ssam {
9925219Smckusick 	int pgoff = getpagesize() - 1;
10050504Smckusick 	char *buf;
10150504Smckusick 	int i;
10210911Ssam 
10310911Ssam 	writesize = ntrec * TP_BSIZE;
10450504Smckusick 	reqsiz = (ntrec + 1) * sizeof(struct req);
10524181Smckusick 	/*
10625219Smckusick 	 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
10725219Smckusick 	 * (see DEC TU80 User's Guide).  The shorter gaps of 6250-bpi require
10825219Smckusick 	 * repositioning after stopping, i.e, streaming mode, where the gap is
10925219Smckusick 	 * variable, 0.30" to 0.45".  The gap is maximal when the tape stops.
11024181Smckusick 	 */
11147056Skarels 	if (blocksperfile == 0)
11247056Skarels 		tenths = writesize / density +
11347056Skarels 		    (cartridge ? 16 : density == 625 ? 5 : 8);
11425219Smckusick 	/*
11525219Smckusick 	 * Allocate tape buffer contiguous with the array of instruction
11646789Smckusick 	 * packets, so flushtape() can write them together with one write().
11725219Smckusick 	 * Align tape buffer on page boundary to speed up tape write().
11825219Smckusick 	 */
11950504Smckusick 	for (i = 0; i <= SLAVES; i++) {
12050504Smckusick 		buf = (char *) malloc(reqsiz + writesize + pgoff + TP_BSIZE);
12150504Smckusick 		if (buf == NULL)
12250504Smckusick 		  return(0);
12350504Smckusick 		slaves[i].tblock = (char (*)[TP_BSIZE])
12450504Smckusick 		    (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
12550504Smckusick 		slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
12650504Smckusick 	}
12750504Smckusick 	slp = &slaves[0];
12850504Smckusick 	slp->count = 1;
12950504Smckusick 	slp->tapea = 0;
13050504Smckusick 	slp->firstrec = 0;
13150504Smckusick 	nextblock = slp->tblock;
13224181Smckusick 	return(1);
13310911Ssam }
13410911Ssam 
13546585Storek void
13646789Smckusick writerec(dp)
1375329Smckusic 	char *dp;
1381425Sroot {
13950504Smckusick 
14050504Smckusick 	slp->req[trecno].dblk = (daddr_t)0;
14150504Smckusick 	slp->req[trecno].count = 1;
14250504Smckusick 	*(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
14329899Smckusick 	lastspclrec = spcl.c_tapea;
14424181Smckusick 	trecno++;
1451425Sroot 	spcl.c_tapea++;
14646585Storek 	if (trecno >= ntrec)
14746789Smckusick 		flushtape();
1481425Sroot }
1491425Sroot 
15046585Storek void
15146789Smckusick dumpblock(blkno, size)
1524774Smckusic 	daddr_t blkno;
1534774Smckusic 	int size;
1541425Sroot {
15525219Smckusick 	int avail, tpblks, dblkno;
1561425Sroot 
1575329Smckusic 	dblkno = fsbtodb(sblock, blkno);
15846585Storek 	tpblks = size >> tp_bshift;
15918012Smckusick 	while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
16050504Smckusick 		slp->req[trecno].dblk = dblkno;
16150504Smckusick 		slp->req[trecno].count = avail;
16225219Smckusick 		trecno += avail;
1634774Smckusic 		spcl.c_tapea += avail;
16425219Smckusick 		if (trecno >= ntrec)
16546789Smckusick 			flushtape();
16646585Storek 		dblkno += avail << (tp_bshift - dev_bshift);
1675329Smckusic 		tpblks -= avail;
1684774Smckusic 	}
1691425Sroot }
1701425Sroot 
1711425Sroot int	nogripe = 0;
1721425Sroot 
17346585Storek void
17446585Storek tperror()
17546585Storek {
17650504Smckusick 
17718012Smckusick 	if (pipeout) {
17846614Smckusick 		msg("write error on %s\n", tape);
17946585Storek 		quit("Cannot recover\n");
18018012Smckusick 		/* NOTREACHED */
18118012Smckusick 	}
18248621Skarels 	msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno);
18346614Smckusick 	broadcast("DUMP WRITE ERROR!\n");
18418012Smckusick 	if (!query("Do you want to restart?"))
18518012Smckusick 		dumpabort();
18646614Smckusick 	msg("Closing this volume.  Prepare to restart with new media;\n");
18718012Smckusick 	msg("this dump volume will be rewritten.\n");
18824181Smckusick 	killall();
18918012Smckusick 	nogripe = 1;
19018012Smckusick 	close_rewind();
19118012Smckusick 	Exit(X_REWRITE);
19218012Smckusick }
19318012Smckusick 
19446585Storek void
19525219Smckusick sigpipe()
19625219Smckusick {
19725219Smckusick 
19846585Storek 	quit("Broken pipe\n");
19925219Smckusick }
20025219Smckusick 
20146585Storek void
20246789Smckusick flushtape()
20318012Smckusick {
20450504Smckusick 	int i, blks, got;
20550504Smckusick 	long lastfirstrec;
20646795Sbostic #ifndef __STDC__
20750504Smckusick 	int write(), read();
20846795Sbostic #endif
20946795Sbostic 
21050504Smckusick 	int siz = (char *)nextblock - (char *)slp->req;
2111425Sroot 
21250504Smckusick 	slp->req[trecno].count = 0;			/* Sentinel */
21350504Smckusick 
21450504Smckusick 	if (atomic(write, slp->fd, slp->req, siz) != siz)
21546585Storek 		quit("error writing command pipe: %s\n", strerror(errno));
21650504Smckusick 	slp->sent = 1; /* we sent a request, read the response later */
21750504Smckusick 
21850504Smckusick 	lastfirstrec = slp->firstrec;
21950504Smckusick 
22050504Smckusick 	if (++slp >= &slaves[SLAVES])
22150504Smckusick 		slp = &slaves[0];
22250504Smckusick 
22350504Smckusick 	/* Read results back from next slave */
22450504Smckusick 	if (slp->sent) {
22550504Smckusick 		if (atomic(read, slp->fd, &got, sizeof got) != sizeof got) {
22650504Smckusick 			perror("  DUMP: error reading command pipe in master");
22750504Smckusick 			dumpabort();
22850504Smckusick 		}
22950504Smckusick 		slp->sent = 0;
23050504Smckusick 
23150504Smckusick 		/* Check for end of tape */
23250504Smckusick 		if (got < writesize) {
23350504Smckusick 			msg("End of tape detected\n");
23450504Smckusick 
23550504Smckusick 			/*
23650504Smckusick 			 * Drain the results, don't care what the values were.
23750504Smckusick 			 * If we read them here then trewind won't...
23850504Smckusick 			 */
23950504Smckusick 			for (i = 0; i < SLAVES; i++) {
24050504Smckusick 				if (slaves[i].sent) {
24150504Smckusick 					if (atomic(read, slaves[i].fd, &got,
24250504Smckusick 					    sizeof got) != sizeof got) {
24350504Smckusick 						perror("  DUMP: error reading command pipe in master");
24450504Smckusick 						dumpabort();
24550504Smckusick 					}
24650504Smckusick 					slaves[i].sent = 0;
24750504Smckusick 				}
24850504Smckusick 			}
24950504Smckusick 
25050504Smckusick 			close_rewind();
25150504Smckusick 			rollforward();
25250504Smckusick 			return;
25350504Smckusick 		}
25450504Smckusick 	}
25550504Smckusick 
25650504Smckusick 	blks = 0;
25750504Smckusick 	if (spcl.c_type != TS_END) {
25850504Smckusick 		for (i = 0; i < spcl.c_count; i++)
25950504Smckusick 			if (spcl.c_addr[i] != 0)
26050504Smckusick 				blks++;
26150504Smckusick 	}
26250504Smckusick 	slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
26350504Smckusick 	slp->tapea = spcl.c_tapea;
26450504Smckusick 	slp->firstrec = lastfirstrec + ntrec;
26550504Smckusick 	slp->inode = curino;
26650504Smckusick 	nextblock = slp->tblock;
2671425Sroot 	trecno = 0;
26824181Smckusick 	asize += tenths;
26910911Ssam 	blockswritten += ntrec;
27048621Skarels 	blocksthisvol += ntrec;
27146614Smckusick 	if (!pipeout && (blocksperfile ?
27248621Skarels 	    (blocksthisvol >= blocksperfile) : (asize > tsize))) {
2731425Sroot 		close_rewind();
27450504Smckusick 		startnewtape(0);
2751425Sroot 	}
2761425Sroot 	timeest();
2771425Sroot }
2781425Sroot 
27946585Storek void
28046239Storek trewind()
2811425Sroot {
28224181Smckusick 	int f;
28350504Smckusick 	int got;
28412331Smckusick 
28512331Smckusick 	if (pipeout)
28612331Smckusick 		return;
28750504Smckusick 	for (f = 0; f < SLAVES; f++) {
28850504Smckusick 		/*
28950504Smckusick 		 * Drain the results, but unlike EOT we DO (or should) care
29050504Smckusick 		 * what the return values were, since if we detect EOT after
29150504Smckusick 		 * we think we've written the last blocks to the tape anyway,
29250504Smckusick 		 * we have to replay those blocks with rollforward.
29350504Smckusick 		 *
29450504Smckusick 		 * fixme: punt for now.
29550504Smckusick 		 */
29650504Smckusick 		if (slaves[f].sent) {
29750504Smckusick 			if (atomic(read, slaves[f].fd, &got, sizeof got)
29850504Smckusick 			    != sizeof got) {
29950504Smckusick 				perror("  DUMP: error reading command pipe in master");
30050504Smckusick 				dumpabort();
30150504Smckusick 			}
30250504Smckusick 			slaves[f].sent = 0;
30350504Smckusick 			if (got != writesize) {
30450504Smckusick 				msg("EOT detected in last 2 tape records!\n");
30550504Smckusick 				msg("Use a longer tape, decrease the size estimate\n");
30650504Smckusick 				quit("or use no size estimate at all.\n");
30750504Smckusick 			}
30850504Smckusick 		}
30950504Smckusick 		close(slaves[f].fd);
31050504Smckusick 	}
31146585Storek 	while (wait((int *)NULL) >= 0)	/* wait for any signals from slaves */
31246585Storek 		/* void */;
31348621Skarels 	msg("Closing %s\n", tape);
31450504Smckusick 
31518012Smckusick #ifdef RDUMP
31625219Smckusick 	if (host) {
31725219Smckusick 		rmtclose();
31825219Smckusick 		while (rmtopen(tape, 0) < 0)
31925219Smckusick 			sleep(10);
32025219Smckusick 		rmtclose();
32125219Smckusick 		return;
32225219Smckusick 	}
32350504Smckusick #endif
32446789Smckusick 	close(tapefd);
3253214Swnj 	while ((f = open(tape, 0)) < 0)
3263214Swnj 		sleep (10);
3273214Swnj 	close(f);
3281425Sroot }
3291425Sroot 
33046585Storek void
3311425Sroot close_rewind()
3321425Sroot {
33346239Storek 	trewind();
33448621Skarels 	if (nexttape)
33548621Skarels 		return;
33618012Smckusick 	if (!nogripe) {
33746614Smckusick 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
33846614Smckusick 		broadcast("CHANGE DUMP VOLUMES!\7\7\n");
3391425Sroot 	}
34048621Skarels 	while (!query("Is the new volume mounted and ready to go?"))
34125219Smckusick 		if (query("Do you want to abort?")) {
3421425Sroot 			dumpabort();
34325219Smckusick 			/*NOTREACHED*/
34425219Smckusick 		}
3451425Sroot }
3461425Sroot 
34750504Smckusick #ifdef ROLLDEBUG
34850504Smckusick int do_sum(block)
34950504Smckusick      union u_spcl *block;
35050504Smckusick 
35150504Smckusick {
35250504Smckusick 	char sum = 0;
35350504Smckusick 	int i;
35450504Smckusick 
35550504Smckusick 	for (i = 0; i < TP_BSIZE; i++) {
35650504Smckusick 		sum = sum ^ block->dummy[i];
35750504Smckusick 	}
35850504Smckusick 	return(sum);
35950504Smckusick }
36050504Smckusick #endif
36150504Smckusick 
36250504Smckusick void
36350504Smckusick rollforward()
36450504Smckusick {
36550504Smckusick 	register struct req *p, *q, *prev;
36650504Smckusick 	register struct slave *tslp;
36750504Smckusick 	int i, next, size, savedtapea, got;
36850504Smckusick 	union u_spcl *ntb, *otb;
36950504Smckusick #ifdef ROLLDEBUG
37050504Smckusick 	int j;
37150504Smckusick #endif
37250504Smckusick 	tslp = &slaves[SLAVES];
37350504Smckusick 	ntb = (union u_spcl *)tslp->tblock[1];
37450504Smckusick 
37550504Smckusick 	/*
37650504Smckusick 	 * Each of the N slaves should have requests that need to
37750504Smckusick 	 * be replayed on the next tape.  Use the extra slave buffers
37850504Smckusick 	 * (slaves[SLAVES]) to construct request lists to be sent to
37950504Smckusick 	 * each slave in turn.
38050504Smckusick 	 */
38150504Smckusick 	for (i = 0; i < SLAVES; i++) {
38250504Smckusick 		q = &tslp->req[1];
38350504Smckusick 		otb = (union u_spcl *)slp->tblock;
38450504Smckusick 
38550504Smckusick 		/*
38650504Smckusick 		 * For each request in the current slave, copy it to tslp.
38750504Smckusick 		 */
38850504Smckusick #ifdef ROLLDEBUG
38950504Smckusick 		printf("replaying reqs to slave %d (%d)\n", slp - &slaves[0],
39050504Smckusick 		    slp->pid);
39150504Smckusick 		j = 0;
39250504Smckusick #endif
39350504Smckusick 
39450504Smckusick 		for (p = slp->req; p->count > 0; p += p->count) {
39550504Smckusick #ifdef ROLLDEBUG
39650504Smckusick 			printf("    req %d count %d dblk %d\n",
39750504Smckusick 			       j++, p->count, p->dblk);
39850504Smckusick 			if (p->dblk == 0)
39950504Smckusick 				printf("\tsum %x\n", do_sum(otb));
40050504Smckusick #endif
40150504Smckusick 			*q = *p;
40250504Smckusick 			if (p->dblk == 0)
40350504Smckusick 				*ntb++ = *otb++; /* copy the datablock also */
40450504Smckusick 			prev = q;
40550504Smckusick 			q += q->count;
40650504Smckusick 		}
40750504Smckusick 		if (prev->dblk != 0)
40850504Smckusick 			prev->count -= 1;
40950504Smckusick 		else
41050504Smckusick 			ntb--;
41150504Smckusick 		q -= 1;
41250504Smckusick 		q->count = 0;
41350504Smckusick 		q = &tslp->req[0];
41450504Smckusick 		if (i == 0) {
41550504Smckusick 			q->dblk = 0;
41650504Smckusick 			q->count = 1;
41750504Smckusick 			trecno = 0;
41850504Smckusick 			nextblock = tslp->tblock;
41950504Smckusick 			savedtapea = spcl.c_tapea;
42050504Smckusick 			spcl.c_tapea = slp->tapea;
42150504Smckusick 			startnewtape(0);
42250504Smckusick 			spcl.c_tapea = savedtapea;
42350504Smckusick 			lastspclrec = savedtapea - 1;
42450504Smckusick 		}
42550504Smckusick 		size = (char *)ntb - (char *)q;
42650504Smckusick 		if (atomic(write, slp->fd, q, size) != size) {
42750504Smckusick 			perror("  DUMP: error writing command pipe");
42850504Smckusick 			dumpabort();
42950504Smckusick 		}
43050504Smckusick 		slp->sent = 1;
43150504Smckusick #ifdef ROLLDEBUG
43250504Smckusick 		printf("after the shift:\n");
43350504Smckusick 		j = 0;
43450504Smckusick 		for (p = tslp->req; p->count > 0; p += p->count) {
43550504Smckusick 			printf("    req %d count %d dblk %d\n",
43650504Smckusick 			       j++, p->count, p->dblk);
43750504Smckusick 			if (p->dblk == 0) {
43850504Smckusick 				/* dump block also */
43950504Smckusick 			}
44050504Smckusick 		}
44150504Smckusick #endif
44250504Smckusick 		if (++slp >= &slaves[SLAVES])
44350504Smckusick 			slp = &slaves[0];
44450504Smckusick 
44550504Smckusick 		q->count = 1;
44650504Smckusick 
44750504Smckusick 		if (prev->dblk != 0) {
44850504Smckusick 			/*
44950504Smckusick 			 * If the last one was a disk block, make the
45050504Smckusick 			 * first of this one be the last bit of that disk
45150504Smckusick 			 * block...
45250504Smckusick 			 */
45350504Smckusick 			q->dblk = prev->dblk +
45450504Smckusick 				prev->count * (TP_BSIZE / DEV_BSIZE);
45550504Smckusick 			ntb = (union u_spcl *)tslp->tblock;
45650504Smckusick 		} else {
45750504Smckusick 			/*
45850504Smckusick 			 * It wasn't a disk block.  Copy the data to its
45950504Smckusick 			 * new location in the buffer.
46050504Smckusick 			 */
46150504Smckusick 			q->dblk = 0;
46250504Smckusick 			*((union u_spcl *)tslp->tblock) = *ntb;
46350504Smckusick 			ntb = (union u_spcl *)tslp->tblock[1];
46450504Smckusick 		}
46550504Smckusick 	}
46650504Smckusick 	slp->req[0] = *q;
46750504Smckusick 	nextblock = slp->tblock;
46850504Smckusick 	if (q->dblk == 0)
46950504Smckusick 		nextblock++;
47050504Smckusick 	trecno = 1;
47150504Smckusick 
47250504Smckusick 	/*
47350504Smckusick 	 * Clear the first slaves' response.  One hopes that it
47450504Smckusick 	 * worked ok, otherwise the tape is much too short!
47550504Smckusick 	 */
47650504Smckusick 	if (slp->sent) {
47750504Smckusick 		if (atomic(read, slp->fd, &got, sizeof got) != sizeof got) {
47850504Smckusick 			perror("  DUMP: error reading command pipe in master");
47950504Smckusick 			dumpabort();
48050504Smckusick 		}
48150504Smckusick 		slp->sent = 0;
48250504Smckusick 
48350504Smckusick 		if (got != writesize) {
48450504Smckusick 			quit("EOT detected at start of the tape!\n");
48550504Smckusick 		}
48650504Smckusick 	}
48750504Smckusick }
48850504Smckusick 
4891425Sroot /*
49050504Smckusick  * We implement taking and restoring checkpoints on the tape level.
49150504Smckusick  * When each tape is opened, a new process is created by forking; this
49250504Smckusick  * saves all of the necessary context in the parent.  The child
49350504Smckusick  * continues the dump; the parent waits around, saving the context.
49450504Smckusick  * If the child returns X_REWRITE, then it had problems writing that tape;
49550504Smckusick  * this causes the parent to fork again, duplicating the context, and
49650504Smckusick  * everything continues as if nothing had happened.
4971425Sroot  */
49846585Storek void
49950504Smckusick startnewtape(top)
50050504Smckusick 	int top;
5011425Sroot {
5021425Sroot 	int	parentpid;
5031425Sroot 	int	childpid;
5041425Sroot 	int	status;
5051425Sroot 	int	waitpid;
50639164Sbostic 	sig_t	interrupt;
50750504Smckusick 	int	i;
50847056Skarels 	char	*p;
5091425Sroot 
51039164Sbostic 	interrupt = signal(SIGINT, SIG_IGN);
5111425Sroot 	parentpid = getpid();
5121425Sroot 
5131425Sroot     restore_check_point:
51439164Sbostic 	(void)signal(SIGINT, interrupt);
51525219Smckusick 	/*
51625219Smckusick 	 *	All signals are inherited...
51725219Smckusick 	 */
5181425Sroot 	childpid = fork();
51918012Smckusick 	if (childpid < 0) {
5201425Sroot 		msg("Context save fork fails in parent %d\n", parentpid);
5211425Sroot 		Exit(X_ABORT);
5221425Sroot 	}
52318012Smckusick 	if (childpid != 0) {
5241425Sroot 		/*
5251425Sroot 		 *	PARENT:
5261425Sroot 		 *	save the context by waiting
5271425Sroot 		 *	until the child doing all of the work returns.
52818012Smckusick 		 *	don't catch the interrupt
5291425Sroot 		 */
53025219Smckusick 		signal(SIGINT, SIG_IGN);
5311425Sroot #ifdef TDEBUG
5321425Sroot 		msg("Tape: %d; parent process: %d child process %d\n",
5331425Sroot 			tapeno+1, parentpid, childpid);
5341425Sroot #endif TDEBUG
53518012Smckusick 		while ((waitpid = wait(&status)) != childpid)
53618012Smckusick 			msg("Parent %d waiting for child %d has another child %d return\n",
53718012Smckusick 				parentpid, childpid, waitpid);
53818012Smckusick 		if (status & 0xFF) {
5391425Sroot 			msg("Child %d returns LOB status %o\n",
5401425Sroot 				childpid, status&0xFF);
5411425Sroot 		}
5421425Sroot 		status = (status >> 8) & 0xFF;
5431425Sroot #ifdef TDEBUG
54418012Smckusick 		switch(status) {
5451425Sroot 			case X_FINOK:
5461425Sroot 				msg("Child %d finishes X_FINOK\n", childpid);
5471425Sroot 				break;
54850504Smckusick 			case X_ABORT:
5491425Sroot 				msg("Child %d finishes X_ABORT\n", childpid);
5501425Sroot 				break;
5511425Sroot 			case X_REWRITE:
5521425Sroot 				msg("Child %d finishes X_REWRITE\n", childpid);
5531425Sroot 				break;
5541425Sroot 			default:
55518012Smckusick 				msg("Child %d finishes unknown %d\n",
55625219Smckusick 					childpid, status);
5571425Sroot 				break;
5581425Sroot 		}
5591425Sroot #endif TDEBUG
56018012Smckusick 		switch(status) {
5611425Sroot 			case X_FINOK:
5621425Sroot 				Exit(X_FINOK);
5631425Sroot 			case X_ABORT:
5641425Sroot 				Exit(X_ABORT);
5651425Sroot 			case X_REWRITE:
5661425Sroot 				goto restore_check_point;
5671425Sroot 			default:
5681425Sroot 				msg("Bad return code from dump: %d\n", status);
5691425Sroot 				Exit(X_ABORT);
5701425Sroot 		}
5711425Sroot 		/*NOTREACHED*/
5721425Sroot 	} else {	/* we are the child; just continue */
5731425Sroot #ifdef TDEBUG
5741425Sroot 		sleep(4);	/* allow time for parent's message to get out */
5751425Sroot 		msg("Child on Tape %d has parent %d, my pid = %d\n",
5761425Sroot 			tapeno+1, parentpid, getpid());
57725219Smckusick #endif TDEBUG
57847056Skarels 		/*
57947056Skarels 		 * If we have a name like "/dev/rmt0,/dev/rmt1",
58047056Skarels 		 * use the name before the comma first, and save
58148621Skarels 		 * the remaining names for subsequent volumes.
58247056Skarels 		 */
58350504Smckusick 		tapeno++;               /* current tape sequence */
58448621Skarels 		if (nexttape || index(tape, ',')) {
58548621Skarels 			if (nexttape && *nexttape)
58648621Skarels 				tape = nexttape;
58748621Skarels 			if (p = index(tape, ',')) {
58848621Skarels 				*p = '\0';
58948621Skarels 				nexttape = p + 1;
59048621Skarels 			} else
59148621Skarels 				nexttape = NULL;
59248621Skarels 			msg("Dumping volume %d on %s\n", tapeno, tape);
59348621Skarels 		}
59418012Smckusick #ifdef RDUMP
59546789Smckusick 		while ((tapefd = (host ? rmtopen(tape, 2) :
59646789Smckusick 			pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
59750504Smckusick #else
59850504Smckusick 		while ((tapefd = (pipeout ? 1 :
59950504Smckusick 				  open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
60050504Smckusick #endif
60139128Smckusick 		    {
60246614Smckusick 			msg("Cannot open output \"%s\".\n", tape);
60339128Smckusick 			if (!query("Do you want to retry the open?"))
60418012Smckusick 				dumpabort();
60539128Smckusick 		}
6061425Sroot 
60718012Smckusick 		enslave();  /* Share open tape file descriptor with slaves */
60818012Smckusick 
6091425Sroot 		asize = 0;
61048621Skarels 		blocksthisvol = 0;
61150504Smckusick 		if (top)
61250504Smckusick 			newtape++;		/* new tape signal */
61350504Smckusick 		spcl.c_count = slp->count;
61450504Smckusick 		/*
61550504Smckusick 		 * measure firstrec in TP_BSIZE units since restore doesn't
61650504Smckusick 		 * know the correct ntrec value...
61750504Smckusick 		 */
61850504Smckusick 		spcl.c_firstrec = slp->firstrec;
6191425Sroot 		spcl.c_volume++;
6201425Sroot 		spcl.c_type = TS_TAPE;
62130432Smckusick 		spcl.c_flags |= DR_NEWHEADER;
62250504Smckusick 		writeheader(slp->inode);
62330432Smckusick 		spcl.c_flags &=~ DR_NEWHEADER;
6241425Sroot 		if (tapeno > 1)
62548621Skarels 			msg("Volume %d begins with blocks from inode %d\n",
62650504Smckusick 				tapeno, slp->inode);
6271425Sroot 	}
6281425Sroot }
6291425Sroot 
63046585Storek void
6311425Sroot dumpabort()
6321425Sroot {
63350504Smckusick 
63418012Smckusick 	if (master != 0 && master != getpid())
63525219Smckusick 		kill(master, SIGTERM);	/* Signals master to call dumpabort */
63624181Smckusick 	else {
63724181Smckusick 		killall();
63824181Smckusick 		msg("The ENTIRE dump is aborted.\n");
63924181Smckusick 	}
6401425Sroot 	Exit(X_ABORT);
6411425Sroot }
6421425Sroot 
64346585Storek void
6441425Sroot Exit(status)
64546239Storek 	int status;
6461425Sroot {
64750504Smckusick 
6481425Sroot #ifdef TDEBUG
6491425Sroot 	msg("pid = %d exits with status %d\n", getpid(), status);
6501425Sroot #endif TDEBUG
6511925Swnj 	exit(status);
6521425Sroot }
65318012Smckusick 
65424181Smckusick /*
65550504Smckusick  * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
65624181Smckusick  */
65746585Storek void
65850504Smckusick proceed()
65924181Smckusick {
66018012Smckusick 
66150504Smckusick 	if (ready)
66250504Smckusick 		longjmp(jmpbuf, 1);
66350504Smckusick 	caught++;
66424181Smckusick }
66524181Smckusick 
66646585Storek void
66718012Smckusick enslave()
66818012Smckusick {
66950504Smckusick 	int cmd[2];
67024181Smckusick 	register int i, j;
67118012Smckusick 
67218012Smckusick 	master = getpid();
67350504Smckusick 
67450504Smckusick 	signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
67525219Smckusick 	signal(SIGPIPE, sigpipe);
67625219Smckusick 	signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
67750504Smckusick 	signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
67850504Smckusick 
67924181Smckusick 	for (i = 0; i < SLAVES; i++) {
68050504Smckusick 		if (i == slp - &slaves[0]) {
68150504Smckusick 			caught = 1;
68224181Smckusick 		} else {
68350504Smckusick 			caught = 0;
68424181Smckusick 		}
68550504Smckusick 
68650504Smckusick 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
68750504Smckusick 		    (slaves[i].pid = fork()) < 0)
68846585Storek 			quit("too many slaves, %d (recompile smaller): %s\n",
68946585Storek 			    i, strerror(errno));
69050504Smckusick 
69150504Smckusick 		slaves[i].fd = cmd[1];
69250504Smckusick 		slaves[i].sent = 0;
69350504Smckusick 		if (slaves[i].pid == 0) { 	    /* Slave starts up here */
69418012Smckusick 			for (j = 0; j <= i; j++)
69550504Smckusick 			        close(slaves[j].fd);
69625219Smckusick 			signal(SIGINT, SIG_IGN);    /* Master handles this */
69750504Smckusick 			doslave(cmd[0], i);
69818012Smckusick 			Exit(X_FINOK);
69918012Smckusick 		}
70018012Smckusick 	}
70150504Smckusick 
70250504Smckusick 	for (i = 0; i < SLAVES; i++)
70350504Smckusick 		atomic(write, slaves[i].fd, &slaves[(i + 1) % SLAVES].pid,
70450504Smckusick 		       sizeof slaves[0].pid);
70550504Smckusick 
70650504Smckusick 	master = 0;
70718012Smckusick }
70818012Smckusick 
70946585Storek void
71024181Smckusick killall()
71118012Smckusick {
71224181Smckusick 	register int i;
71319982Smckusick 
71424181Smckusick 	for (i = 0; i < SLAVES; i++)
71550504Smckusick 		if (slaves[i].pid > 0)
71650504Smckusick 			kill(slaves[i].pid, SIGKILL);
71718012Smckusick }
71818012Smckusick 
71924181Smckusick /*
72024181Smckusick  * Synchronization - each process has a lockfile, and shares file
72124181Smckusick  * descriptors to the following process's lockfile.  When our write
72224181Smckusick  * completes, we release our lock on the following process's lock-
72324181Smckusick  * file, allowing the following process to lock it and proceed. We
72424181Smckusick  * get the lock back for the next cycle by swapping descriptors.
72524181Smckusick  */
72646585Storek void
72750504Smckusick doslave(cmd, slave_number)
72850504Smckusick 	register int cmd;
72950504Smckusick         int slave_number;
73019982Smckusick {
73150504Smckusick 	register int nread;
73250504Smckusick 	int nextslave, size, wrote, eot_count;
73346795Sbostic #ifndef __STDC__
73446795Sbostic 	int read();
73546795Sbostic #endif
73650504Smckusick #ifdef ROLLDEBUG
73750504Smckusick 	int dodump = 2;
73850504Smckusick 	FILE *out;
73950504Smckusick 	char name[64];
74050504Smckusick #endif
74119982Smckusick 
74246789Smckusick 	/*
74346789Smckusick 	 * Need our own seek pointer.
74446789Smckusick 	 */
74546789Smckusick 	close(diskfd);
74646789Smckusick 	if ((diskfd = open(disk, O_RDONLY)) < 0)
74746585Storek 		quit("slave couldn't reopen disk: %s\n", strerror(errno));
74850504Smckusick 
74924181Smckusick 	/*
75050504Smckusick 	 * Need the pid of the next slave in the loop...
75150504Smckusick 	 */
75250504Smckusick 	if ((nread = atomic(read, cmd, &nextslave, sizeof nextslave))
75350504Smckusick 	    != sizeof nextslave) {
75450504Smckusick 		quit("master/slave protocol botched - didn't get pid of next slave.\n");
75550504Smckusick 	}
75650504Smckusick 
75750504Smckusick #ifdef ROLLDEBUG
75850504Smckusick 	sprintf(name, "slave.%d", slave_number);
75950504Smckusick 	out = fopen(name, "w");
76050504Smckusick #endif
76150504Smckusick 	/*
76225219Smckusick 	 * Get list of blocks to dump, read the blocks into tape buffer
76324181Smckusick 	 */
76450504Smckusick 	while ((nread = atomic(read, cmd, slp->req, reqsiz)) == reqsiz) {
76550504Smckusick 		register struct req *p = slp->req;
76650504Smckusick 		int j;
76750504Smckusick 		struct req *rover;
76850504Smckusick 		char (*orover)[TP_BSIZE];
76950504Smckusick 
77050504Smckusick 		j = 0;
77150504Smckusick 		for (trecno = 0; trecno < ntrec;
77250504Smckusick 		     trecno += p->count, p += p->count) {
77318012Smckusick 			if (p->dblk) {
77450504Smckusick 				bread(p->dblk, slp->tblock[trecno],
77525219Smckusick 					p->count * TP_BSIZE);
77618012Smckusick 			} else {
77725219Smckusick 				if (p->count != 1 || atomic(read, cmd,
77850504Smckusick 				    slp->tblock[trecno], TP_BSIZE) != TP_BSIZE)
77946585Storek 					quit("master/slave protocol botched.\n");
78018012Smckusick 			}
78150504Smckusick #ifdef ROLLDEBUG
78250504Smckusick 			if (dodump) {
78350504Smckusick 				fprintf(out, "    req %d count %d dblk %d\n",
78450504Smckusick 					j++, p->count, p->dblk);
78550504Smckusick 				if (p->dblk == 0) {
78650504Smckusick 					fprintf(out, "\tsum %x\n",
78750504Smckusick 						do_sum(slp->tblock[trecno]));
78850504Smckusick 				}
78950504Smckusick 			}
79050504Smckusick #endif
79118012Smckusick 		}
79250504Smckusick #ifdef ROLLDEBUG
79350504Smckusick 		if (dodump) {
79450504Smckusick 			fprintf(out, "\n");
79550504Smckusick 		}
79650504Smckusick 		if (--dodump == 0) {
79750504Smckusick 			fclose(out);
79850504Smckusick 		}
79950504Smckusick #endif
80050504Smckusick 		if (setjmp(jmpbuf) == 0) {
80150504Smckusick 			ready = 1;
80250504Smckusick 			if (!caught)
80350504Smckusick 				pause();
80450504Smckusick 		}
80550504Smckusick 		ready = 0;
80650504Smckusick 		caught = 0;
80725219Smckusick 
80850504Smckusick 		/* Try to write the data... */
80950504Smckusick 		eot_count = 0;
81050504Smckusick 		size = 0;
81150504Smckusick 
81250504Smckusick 		while (eot_count < 10 && size < writesize) {
81318012Smckusick #ifdef RDUMP
81450504Smckusick 			if (host)
81550504Smckusick 				wrote = rmtwrite(slp->tblock[0]+size,
81650504Smckusick 				    writesize-size);
81748621Skarels 			else
81850504Smckusick #endif
81950504Smckusick 				wrote = write(tapefd, slp->tblock[0]+size,
82050504Smckusick 				    writesize-size);
82150504Smckusick #ifdef WRITEDEBUG
82250504Smckusick 			printf("slave %d wrote %d\n", slave_number, wrote);
82350504Smckusick #endif
82450504Smckusick 			if (wrote < 0)
82550504Smckusick 				break;
82650504Smckusick 			if (wrote == 0)
82750504Smckusick 				eot_count++;
82850504Smckusick 			size += wrote;
82950504Smckusick 		}
83050504Smckusick 
83150504Smckusick #ifdef WRITEDEBUG
83250504Smckusick 		if (size != writesize)
83350504Smckusick 		 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
83450504Smckusick 		     slave_number, size, writesize);
83550504Smckusick #endif
83650504Smckusick 
83750504Smckusick 		if (eot_count > 0)
83850504Smckusick 			size = 0;
83950504Smckusick 
84050504Smckusick 		/*
84150504Smckusick 		 * fixme: Pyramids running OSx return ENOSPC
84250504Smckusick 		 * at EOT on 1/2 inch drives.
84350504Smckusick 		 */
84450504Smckusick 		if (size < 0) {
84525219Smckusick 			kill(master, SIGUSR1);
84625219Smckusick 			for (;;)
84725219Smckusick 				sigpause(0);
84850504Smckusick 		} else {
84950504Smckusick 			/*
85050504Smckusick 			 * pass size of write back to master
85150504Smckusick 			 * (for EOT handling)
85250504Smckusick 			 */
85350504Smckusick 			atomic(write, cmd, &size, sizeof size);
85450504Smckusick 		}
85550504Smckusick 
85650504Smckusick 		/*
85750504Smckusick 		 * If partial write, don't want next slave to go.
85850504Smckusick 		 * Also jolts him awake.
85950504Smckusick 		 */
86050504Smckusick 		kill(nextslave, SIGUSR2);
86150504Smckusick 	}
86246585Storek 	if (nread != 0)
86346585Storek 		quit("error reading command pipe: %s\n", strerror(errno));
86418012Smckusick }
86519947Smckusick 
86619947Smckusick /*
86725219Smckusick  * Since a read from a pipe may not return all we asked for,
86825219Smckusick  * or a write may not write all we ask if we get a signal,
86925219Smckusick  * loop until the count is satisfied (or error).
87019947Smckusick  */
87146585Storek int
87225219Smckusick atomic(func, fd, buf, count)
87325219Smckusick 	int (*func)(), fd, count;
87419947Smckusick 	char *buf;
87519947Smckusick {
87625219Smckusick 	int got, need = count;
87719947Smckusick 
87825219Smckusick 	while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
87919947Smckusick 		buf += got;
88025219Smckusick 	return (got < 0 ? got : count - need);
88119947Smckusick }
882