xref: /freebsd/sbin/dump/tape.c (revision 0c577f4f80ac0ee8970277d06c43b8aea834a66f)
18fae3551SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1980, 1991, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes  * are met:
108fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes  *    without specific prior written permission.
188fae3551SRodney W. Grimes  *
198fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes  * SUCH DAMAGE.
308fae3551SRodney W. Grimes  */
318fae3551SRodney W. Grimes 
328fae3551SRodney W. Grimes #ifndef lint
337580ffbbSPhilippe Charnier #if 0
34a37c38b8SPeter Wemm static char sccsid[] = "@(#)tape.c	8.4 (Berkeley) 5/1/95";
357580ffbbSPhilippe Charnier #endif
367580ffbbSPhilippe Charnier static const char rcsid[] =
377f3dea24SPeter Wemm   "$FreeBSD$";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #include <sys/param.h>
418fae3551SRodney W. Grimes #include <sys/socket.h>
428fae3551SRodney W. Grimes #include <sys/wait.h>
43ff5e109eSIan Dowse #include <sys/stat.h>
448fae3551SRodney W. Grimes 
458fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
46a37c38b8SPeter Wemm #include <ufs/ffs/fs.h>
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes #include <protocols/dumprestore.h>
498fae3551SRodney W. Grimes 
502d518c65SWarner Losh #include <assert.h>
518fae3551SRodney W. Grimes #include <errno.h>
528fae3551SRodney W. Grimes #include <fcntl.h>
5389fdc4e1SMike Barcroft #include <limits.h>
548fae3551SRodney W. Grimes #include <setjmp.h>
558fae3551SRodney W. Grimes #include <signal.h>
568fae3551SRodney W. Grimes #include <stdio.h>
578fae3551SRodney W. Grimes #include <stdlib.h>
588fae3551SRodney W. Grimes #include <string.h>
597649cb00SKirk McKusick #include <time.h>
608fae3551SRodney W. Grimes #include <unistd.h>
618fae3551SRodney W. Grimes 
628fae3551SRodney W. Grimes #include "dump.h"
638fae3551SRodney W. Grimes 
6433ceb489SKirk McKusick ino_t	curino;			/* current inumber; used globally */
6533ceb489SKirk McKusick int	newtape;		/* new tape flag */
6633ceb489SKirk McKusick union	u_spcl u_spcl;		/* mapping of variables in a control block */
6733ceb489SKirk McKusick 
6833ceb489SKirk McKusick static	int tapefd;		/* tape file descriptor */
6933ceb489SKirk McKusick static	long asize;		/* number of 0.1" units written on cur tape */
7033ceb489SKirk McKusick static	int writesize;		/* size of malloc()ed buffer for tape */
7133ceb489SKirk McKusick static	int64_t lastspclrec = -1; /* tape block number of last written header */
7233ceb489SKirk McKusick static	int trecno = 0;		/* next record to write in current block */
7333ceb489SKirk McKusick static	long blocksthisvol;	/* number of blocks on current output file */
7433ceb489SKirk McKusick static	char *nexttape;
7533ceb489SKirk McKusick static	FILE *popenfp = NULL;
768fae3551SRodney W. Grimes 
772db673abSWarner Losh static	int atomic(ssize_t (*)(), int, char *, int);
78a74534b1SConrad Meyer static	void worker(int, int);
79*0c577f4fSColin Percival static	void create_workers(void);
802db673abSWarner Losh static	void flushtape(void);
812db673abSWarner Losh static	void killall(void);
822db673abSWarner Losh static	void rollforward(void);
838fae3551SRodney W. Grimes 
848fae3551SRodney W. Grimes /*
858fae3551SRodney W. Grimes  * Concurrent dump mods (Caltech) - disk block reading and tape writing
86a74534b1SConrad Meyer  * are exported to several worker processes.  While one worker writes the
878fae3551SRodney W. Grimes  * tape, the others read disk blocks; they pass control of the tape in
888fae3551SRodney W. Grimes  * a ring via signals. The parent process traverses the file system and
89a74534b1SConrad Meyer  * sends writeheader()'s and lists of daddr's to the workers via pipes.
90a74534b1SConrad Meyer  * The following structure defines the instruction packets sent to workers.
918fae3551SRodney W. Grimes  */
928fae3551SRodney W. Grimes struct req {
931c85e6a3SKirk McKusick 	ufs2_daddr_t dblk;
948fae3551SRodney W. Grimes 	int count;
958fae3551SRodney W. Grimes };
9633ceb489SKirk McKusick static int reqsiz;
978fae3551SRodney W. Grimes 
98a74534b1SConrad Meyer #define WORKERS 3		/* 1 worker writing, 1 reading, 1 for slack */
99a74534b1SConrad Meyer static struct worker {
1001c85e6a3SKirk McKusick 	int64_t tapea;		/* header number at start of this chunk */
1011c85e6a3SKirk McKusick 	int64_t firstrec;	/* record number of this block */
1028fae3551SRodney W. Grimes 	int count;		/* count to next header (used for TS_TAPE */
1038fae3551SRodney W. Grimes 				/* after EOT) */
1048fae3551SRodney W. Grimes 	int inode;		/* inode that we are currently dealing with */
105a74534b1SConrad Meyer 	int fd;			/* FD for this worker */
106a74534b1SConrad Meyer 	int pid;		/* PID for this worker */
107a74534b1SConrad Meyer 	int sent;		/* 1 == we've sent this worker requests */
1088fae3551SRodney W. Grimes 	char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
1098fae3551SRodney W. Grimes 	struct req *req;	/* buffer for requests */
110a74534b1SConrad Meyer } workers[WORKERS+1];
111*0c577f4fSColin Percival static struct worker *wp;
1128fae3551SRodney W. Grimes 
11333ceb489SKirk McKusick static char	(*nextblock)[TP_BSIZE];
1148fae3551SRodney W. Grimes 
11533ceb489SKirk McKusick static int master;	/* pid of master, for sending error signals */
11633ceb489SKirk McKusick static int tenths;	/* length of tape used per block written */
117890005bfSIan Dowse static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
118890005bfSIan Dowse static volatile sig_atomic_t ready; /* reached the lock point without having */
119a74534b1SConrad Meyer 			/* received the SIGUSR2 signal from the prev worker? */
1208fae3551SRodney W. Grimes static jmp_buf jmpbuf;	/* where to jump to if we are ready when the */
121a74534b1SConrad Meyer 			/* SIGUSR2 arrives from the previous worker */
1228fae3551SRodney W. Grimes 
1238fae3551SRodney W. Grimes int
1242db673abSWarner Losh alloctape(void)
1258fae3551SRodney W. Grimes {
1268fae3551SRodney W. Grimes 	int pgoff = getpagesize() - 1;
1278fae3551SRodney W. Grimes 	char *buf;
1288fae3551SRodney W. Grimes 	int i;
1298fae3551SRodney W. Grimes 
1308fae3551SRodney W. Grimes 	writesize = ntrec * TP_BSIZE;
1318fae3551SRodney W. Grimes 	reqsiz = (ntrec + 1) * sizeof(struct req);
1328fae3551SRodney W. Grimes 	/*
1338fae3551SRodney W. Grimes 	 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
1348fae3551SRodney W. Grimes 	 * (see DEC TU80 User's Guide).  The shorter gaps of 6250-bpi require
1358fae3551SRodney W. Grimes 	 * repositioning after stopping, i.e, streaming mode, where the gap is
1368fae3551SRodney W. Grimes 	 * variable, 0.30" to 0.45".  The gap is maximal when the tape stops.
1378fae3551SRodney W. Grimes 	 */
138af00957eSJoerg Wunsch 	if (blocksperfile == 0 && !unlimited)
1398fae3551SRodney W. Grimes 		tenths = writesize / density +
1408fae3551SRodney W. Grimes 		    (cartridge ? 16 : density == 625 ? 5 : 8);
1418fae3551SRodney W. Grimes 	/*
1428fae3551SRodney W. Grimes 	 * Allocate tape buffer contiguous with the array of instruction
1438fae3551SRodney W. Grimes 	 * packets, so flushtape() can write them together with one write().
1448fae3551SRodney W. Grimes 	 * Align tape buffer on page boundary to speed up tape write().
1458fae3551SRodney W. Grimes 	 */
146a74534b1SConrad Meyer 	for (i = 0; i <= WORKERS; i++) {
1478fae3551SRodney W. Grimes 		buf = (char *)
1488fae3551SRodney W. Grimes 		    malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
1498fae3551SRodney W. Grimes 		if (buf == NULL)
1508fae3551SRodney W. Grimes 			return(0);
151a74534b1SConrad Meyer 		workers[i].tblock = (char (*)[TP_BSIZE])
1528fae3551SRodney W. Grimes 		    (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
153a74534b1SConrad Meyer 		workers[i].req = (struct req *)workers[i].tblock - ntrec - 1;
1548fae3551SRodney W. Grimes 	}
155*0c577f4fSColin Percival 	wp = &workers[0];
156*0c577f4fSColin Percival 	wp->count = 1;
157*0c577f4fSColin Percival 	wp->tapea = 0;
158*0c577f4fSColin Percival 	wp->firstrec = 0;
159*0c577f4fSColin Percival 	nextblock = wp->tblock;
1608fae3551SRodney W. Grimes 	return(1);
1618fae3551SRodney W. Grimes }
1628fae3551SRodney W. Grimes 
1638fae3551SRodney W. Grimes void
1642db673abSWarner Losh writerec(char *dp, int isspcl)
1658fae3551SRodney W. Grimes {
1668fae3551SRodney W. Grimes 
167*0c577f4fSColin Percival 	wp->req[trecno].dblk = (ufs2_daddr_t)0;
168*0c577f4fSColin Percival 	wp->req[trecno].count = 1;
1692db673abSWarner Losh 	/* Can't do a structure assignment due to alignment problems */
1709b979919SMatt Jacob 	bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
1718fae3551SRodney W. Grimes 	if (isspcl)
1728fae3551SRodney W. Grimes 		lastspclrec = spcl.c_tapea;
1738fae3551SRodney W. Grimes 	trecno++;
1748fae3551SRodney W. Grimes 	spcl.c_tapea++;
1758fae3551SRodney W. Grimes 	if (trecno >= ntrec)
1768fae3551SRodney W. Grimes 		flushtape();
1778fae3551SRodney W. Grimes }
1788fae3551SRodney W. Grimes 
1798fae3551SRodney W. Grimes void
1801c85e6a3SKirk McKusick dumpblock(ufs2_daddr_t blkno, int size)
1818fae3551SRodney W. Grimes {
1821c85e6a3SKirk McKusick 	int avail, tpblks;
1831c85e6a3SKirk McKusick 	ufs2_daddr_t dblkno;
1848fae3551SRodney W. Grimes 
1858fae3551SRodney W. Grimes 	dblkno = fsbtodb(sblock, blkno);
1868fae3551SRodney W. Grimes 	tpblks = size >> tp_bshift;
1878fae3551SRodney W. Grimes 	while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
188*0c577f4fSColin Percival 		wp->req[trecno].dblk = dblkno;
189*0c577f4fSColin Percival 		wp->req[trecno].count = avail;
1908fae3551SRodney W. Grimes 		trecno += avail;
1918fae3551SRodney W. Grimes 		spcl.c_tapea += avail;
1928fae3551SRodney W. Grimes 		if (trecno >= ntrec)
1938fae3551SRodney W. Grimes 			flushtape();
1948fae3551SRodney W. Grimes 		dblkno += avail << (tp_bshift - dev_bshift);
1958fae3551SRodney W. Grimes 		tpblks -= avail;
1968fae3551SRodney W. Grimes 	}
1978fae3551SRodney W. Grimes }
1988fae3551SRodney W. Grimes 
1998fae3551SRodney W. Grimes int	nogripe = 0;
2008fae3551SRodney W. Grimes 
2018fae3551SRodney W. Grimes void
2022db673abSWarner Losh tperror(int signo __unused)
2038fae3551SRodney W. Grimes {
2048fae3551SRodney W. Grimes 
2058fae3551SRodney W. Grimes 	if (pipeout) {
2068fae3551SRodney W. Grimes 		msg("write error on %s\n", tape);
2078fae3551SRodney W. Grimes 		quit("Cannot recover\n");
2088fae3551SRodney W. Grimes 		/* NOTREACHED */
2098fae3551SRodney W. Grimes 	}
210325167c3SIan Dowse 	msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
2118fae3551SRodney W. Grimes 	broadcast("DUMP WRITE ERROR!\n");
2128fae3551SRodney W. Grimes 	if (!query("Do you want to restart?"))
2138fae3551SRodney W. Grimes 		dumpabort(0);
2148fae3551SRodney W. Grimes 	msg("Closing this volume.  Prepare to restart with new media;\n");
2158fae3551SRodney W. Grimes 	msg("this dump volume will be rewritten.\n");
2168fae3551SRodney W. Grimes 	killall();
2178fae3551SRodney W. Grimes 	nogripe = 1;
2188fae3551SRodney W. Grimes 	close_rewind();
2198fae3551SRodney W. Grimes 	Exit(X_REWRITE);
2208fae3551SRodney W. Grimes }
2218fae3551SRodney W. Grimes 
2228fae3551SRodney W. Grimes void
2232db673abSWarner Losh sigpipe(int signo __unused)
2248fae3551SRodney W. Grimes {
2258fae3551SRodney W. Grimes 
2268fae3551SRodney W. Grimes 	quit("Broken pipe\n");
2278fae3551SRodney W. Grimes }
2288fae3551SRodney W. Grimes 
2298fae3551SRodney W. Grimes static void
2302db673abSWarner Losh flushtape(void)
2318fae3551SRodney W. Grimes {
2328fae3551SRodney W. Grimes 	int i, blks, got;
2331c85e6a3SKirk McKusick 	int64_t lastfirstrec;
2348fae3551SRodney W. Grimes 
235*0c577f4fSColin Percival 	int siz = (char *)nextblock - (char *)wp->req;
2368fae3551SRodney W. Grimes 
237*0c577f4fSColin Percival 	wp->req[trecno].count = 0;			/* Sentinel */
2388fae3551SRodney W. Grimes 
239*0c577f4fSColin Percival 	if (atomic(write, wp->fd, (char *)wp->req, siz) != siz)
2408fae3551SRodney W. Grimes 		quit("error writing command pipe: %s\n", strerror(errno));
241*0c577f4fSColin Percival 	wp->sent = 1; /* we sent a request, read the response later */
2428fae3551SRodney W. Grimes 
243*0c577f4fSColin Percival 	lastfirstrec = wp->firstrec;
2448fae3551SRodney W. Grimes 
245*0c577f4fSColin Percival 	if (++wp >= &workers[WORKERS])
246*0c577f4fSColin Percival 		wp = &workers[0];
2478fae3551SRodney W. Grimes 
248a74534b1SConrad Meyer 	/* Read results back from next worker */
249*0c577f4fSColin Percival 	if (wp->sent) {
250*0c577f4fSColin Percival 		if (atomic(read, wp->fd, (char *)&got, sizeof got)
2518fae3551SRodney W. Grimes 		    != sizeof got) {
2528fae3551SRodney W. Grimes 			perror("  DUMP: error reading command pipe in master");
2538fae3551SRodney W. Grimes 			dumpabort(0);
2548fae3551SRodney W. Grimes 		}
255*0c577f4fSColin Percival 		wp->sent = 0;
2568fae3551SRodney W. Grimes 
2578fae3551SRodney W. Grimes 		/* Check for end of tape */
2588fae3551SRodney W. Grimes 		if (got < writesize) {
2598fae3551SRodney W. Grimes 			msg("End of tape detected\n");
2608fae3551SRodney W. Grimes 
2618fae3551SRodney W. Grimes 			/*
2628fae3551SRodney W. Grimes 			 * Drain the results, don't care what the values were.
2638fae3551SRodney W. Grimes 			 * If we read them here then trewind won't...
2648fae3551SRodney W. Grimes 			 */
265a74534b1SConrad Meyer 			for (i = 0; i < WORKERS; i++) {
266a74534b1SConrad Meyer 				if (workers[i].sent) {
267a74534b1SConrad Meyer 					if (atomic(read, workers[i].fd,
2688fae3551SRodney W. Grimes 					    (char *)&got, sizeof got)
2698fae3551SRodney W. Grimes 					    != sizeof got) {
2708fae3551SRodney W. Grimes 						perror("  DUMP: error reading command pipe in master");
2718fae3551SRodney W. Grimes 						dumpabort(0);
2728fae3551SRodney W. Grimes 					}
273a74534b1SConrad Meyer 					workers[i].sent = 0;
2748fae3551SRodney W. Grimes 				}
2758fae3551SRodney W. Grimes 			}
2768fae3551SRodney W. Grimes 
2778fae3551SRodney W. Grimes 			close_rewind();
2788fae3551SRodney W. Grimes 			rollforward();
2798fae3551SRodney W. Grimes 			return;
2808fae3551SRodney W. Grimes 		}
2818fae3551SRodney W. Grimes 	}
2828fae3551SRodney W. Grimes 
2838fae3551SRodney W. Grimes 	blks = 0;
284b9c7cba5SWarner Losh 	if (spcl.c_type != TS_END && spcl.c_type != TS_CLRI &&
285b9c7cba5SWarner Losh 	    spcl.c_type != TS_BITS) {
2862d518c65SWarner Losh 		assert(spcl.c_count <= TP_NINDIR);
2878fae3551SRodney W. Grimes 		for (i = 0; i < spcl.c_count; i++)
2888fae3551SRodney W. Grimes 			if (spcl.c_addr[i] != 0)
2898fae3551SRodney W. Grimes 				blks++;
2908fae3551SRodney W. Grimes 	}
291*0c577f4fSColin Percival 	wp->count = lastspclrec + blks + 1 - spcl.c_tapea;
292*0c577f4fSColin Percival 	wp->tapea = spcl.c_tapea;
293*0c577f4fSColin Percival 	wp->firstrec = lastfirstrec + ntrec;
294*0c577f4fSColin Percival 	wp->inode = curino;
295*0c577f4fSColin Percival 	nextblock = wp->tblock;
2968fae3551SRodney W. Grimes 	trecno = 0;
2978fae3551SRodney W. Grimes 	asize += tenths;
2988fae3551SRodney W. Grimes 	blockswritten += ntrec;
2998fae3551SRodney W. Grimes 	blocksthisvol += ntrec;
300af00957eSJoerg Wunsch 	if (!pipeout && !unlimited && (blocksperfile ?
3018fae3551SRodney W. Grimes 	    (blocksthisvol >= blocksperfile) : (asize > tsize))) {
3028fae3551SRodney W. Grimes 		close_rewind();
3038fae3551SRodney W. Grimes 		startnewtape(0);
3048fae3551SRodney W. Grimes 	}
3058fae3551SRodney W. Grimes 	timeest();
3068fae3551SRodney W. Grimes }
3078fae3551SRodney W. Grimes 
3088fae3551SRodney W. Grimes void
3092db673abSWarner Losh trewind(void)
3108fae3551SRodney W. Grimes {
311ff5e109eSIan Dowse 	struct stat sb;
3128fae3551SRodney W. Grimes 	int f;
3138fae3551SRodney W. Grimes 	int got;
3148fae3551SRodney W. Grimes 
315a74534b1SConrad Meyer 	for (f = 0; f < WORKERS; f++) {
3168fae3551SRodney W. Grimes 		/*
3178fae3551SRodney W. Grimes 		 * Drain the results, but unlike EOT we DO (or should) care
3188fae3551SRodney W. Grimes 		 * what the return values were, since if we detect EOT after
3198fae3551SRodney W. Grimes 		 * we think we've written the last blocks to the tape anyway,
3208fae3551SRodney W. Grimes 		 * we have to replay those blocks with rollforward.
3218fae3551SRodney W. Grimes 		 *
3228fae3551SRodney W. Grimes 		 * fixme: punt for now.
3238fae3551SRodney W. Grimes 		 */
324a74534b1SConrad Meyer 		if (workers[f].sent) {
325a74534b1SConrad Meyer 			if (atomic(read, workers[f].fd, (char *)&got, sizeof got)
3268fae3551SRodney W. Grimes 			    != sizeof got) {
3278fae3551SRodney W. Grimes 				perror("  DUMP: error reading command pipe in master");
3288fae3551SRodney W. Grimes 				dumpabort(0);
3298fae3551SRodney W. Grimes 			}
330a74534b1SConrad Meyer 			workers[f].sent = 0;
3318fae3551SRodney W. Grimes 			if (got != writesize) {
3328fae3551SRodney W. Grimes 				msg("EOT detected in last 2 tape records!\n");
3338fae3551SRodney W. Grimes 				msg("Use a longer tape, decrease the size estimate\n");
3348fae3551SRodney W. Grimes 				quit("or use no size estimate at all.\n");
3358fae3551SRodney W. Grimes 			}
3368fae3551SRodney W. Grimes 		}
337a74534b1SConrad Meyer 		(void) close(workers[f].fd);
3388fae3551SRodney W. Grimes 	}
339a74534b1SConrad Meyer 	while (wait((int *)NULL) >= 0)	/* wait for any signals from workers */
3408fae3551SRodney W. Grimes 		/* void */;
3418fae3551SRodney W. Grimes 
3428fae3551SRodney W. Grimes 	if (pipeout)
3438fae3551SRodney W. Grimes 		return;
3448fae3551SRodney W. Grimes 
3458fae3551SRodney W. Grimes 	msg("Closing %s\n", tape);
3468fae3551SRodney W. Grimes 
347c51d70c6SBrian Feldman 	if (popenout) {
348c51d70c6SBrian Feldman 		tapefd = -1;
349c51d70c6SBrian Feldman 		(void)pclose(popenfp);
350c51d70c6SBrian Feldman 		popenfp = NULL;
351c51d70c6SBrian Feldman 		return;
352c51d70c6SBrian Feldman 	}
3538fae3551SRodney W. Grimes #ifdef RDUMP
3548fae3551SRodney W. Grimes 	if (host) {
3558fae3551SRodney W. Grimes 		rmtclose();
3568fae3551SRodney W. Grimes 		while (rmtopen(tape, 0) < 0)
3578fae3551SRodney W. Grimes 			sleep(10);
3588fae3551SRodney W. Grimes 		rmtclose();
3598fae3551SRodney W. Grimes 		return;
3608fae3551SRodney W. Grimes 	}
3618fae3551SRodney W. Grimes #endif
362ff5e109eSIan Dowse 	if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
363ff5e109eSIan Dowse 		(void)close(tapefd);
364ff5e109eSIan Dowse 		return;
365ff5e109eSIan Dowse 	}
3668fae3551SRodney W. Grimes 	(void) close(tapefd);
3678fae3551SRodney W. Grimes 	while ((f = open(tape, 0)) < 0)
3688fae3551SRodney W. Grimes 		sleep (10);
3698fae3551SRodney W. Grimes 	(void) close(f);
3708fae3551SRodney W. Grimes }
3718fae3551SRodney W. Grimes 
3728fae3551SRodney W. Grimes void
3738fae3551SRodney W. Grimes close_rewind()
3748fae3551SRodney W. Grimes {
375d6038ed6SJoerg Wunsch 	time_t tstart_changevol, tend_changevol;
376d6038ed6SJoerg Wunsch 
3778fae3551SRodney W. Grimes 	trewind();
3788fae3551SRodney W. Grimes 	if (nexttape)
3798fae3551SRodney W. Grimes 		return;
380d6038ed6SJoerg Wunsch 	(void)time((time_t *)&(tstart_changevol));
3818fae3551SRodney W. Grimes 	if (!nogripe) {
3828fae3551SRodney W. Grimes 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
383bfa0b298SPoul-Henning Kamp 		broadcast("CHANGE DUMP VOLUMES!\a\a\n");
3848fae3551SRodney W. Grimes 	}
3858fae3551SRodney W. Grimes 	while (!query("Is the new volume mounted and ready to go?"))
3868fae3551SRodney W. Grimes 		if (query("Do you want to abort?")) {
3878fae3551SRodney W. Grimes 			dumpabort(0);
3888fae3551SRodney W. Grimes 			/*NOTREACHED*/
3898fae3551SRodney W. Grimes 		}
390d6038ed6SJoerg Wunsch 	(void)time((time_t *)&(tend_changevol));
391d6038ed6SJoerg Wunsch 	if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
392d6038ed6SJoerg Wunsch 		tstart_writing += (tend_changevol - tstart_changevol);
3938fae3551SRodney W. Grimes }
3948fae3551SRodney W. Grimes 
3958fae3551SRodney W. Grimes void
3962db673abSWarner Losh rollforward(void)
3978fae3551SRodney W. Grimes {
398d2334e27SIan Dowse 	struct req *p, *q, *prev;
399*0c577f4fSColin Percival 	struct worker *twp;
4001c85e6a3SKirk McKusick 	int i, size, got;
4011c85e6a3SKirk McKusick 	int64_t savedtapea;
4028fae3551SRodney W. Grimes 	union u_spcl *ntb, *otb;
403*0c577f4fSColin Percival 	twp = &workers[WORKERS];
404*0c577f4fSColin Percival 	ntb = (union u_spcl *)twp->tblock[1];
4058fae3551SRodney W. Grimes 
4068fae3551SRodney W. Grimes 	/*
407a74534b1SConrad Meyer 	 * Each of the N workers should have requests that need to
408a74534b1SConrad Meyer 	 * be replayed on the next tape.  Use the extra worker buffers
409a74534b1SConrad Meyer 	 * (workers[WORKERS]) to construct request lists to be sent to
410a74534b1SConrad Meyer 	 * each worker in turn.
4118fae3551SRodney W. Grimes 	 */
412a74534b1SConrad Meyer 	for (i = 0; i < WORKERS; i++) {
413*0c577f4fSColin Percival 		q = &twp->req[1];
414*0c577f4fSColin Percival 		otb = (union u_spcl *)wp->tblock;
4158fae3551SRodney W. Grimes 
4168fae3551SRodney W. Grimes 		/*
417*0c577f4fSColin Percival 		 * For each request in the current worker, copy it to twp.
4188fae3551SRodney W. Grimes 		 */
4198fae3551SRodney W. Grimes 
4208fae3551SRodney W. Grimes 		prev = NULL;
421*0c577f4fSColin Percival 		for (p = wp->req; p->count > 0; p += p->count) {
4228fae3551SRodney W. Grimes 			*q = *p;
4238fae3551SRodney W. Grimes 			if (p->dblk == 0)
4248fae3551SRodney W. Grimes 				*ntb++ = *otb++; /* copy the datablock also */
4258fae3551SRodney W. Grimes 			prev = q;
4268fae3551SRodney W. Grimes 			q += q->count;
4278fae3551SRodney W. Grimes 		}
4288fae3551SRodney W. Grimes 		if (prev == NULL)
4298fae3551SRodney W. Grimes 			quit("rollforward: protocol botch");
4308fae3551SRodney W. Grimes 		if (prev->dblk != 0)
4318fae3551SRodney W. Grimes 			prev->count -= 1;
4328fae3551SRodney W. Grimes 		else
4338fae3551SRodney W. Grimes 			ntb--;
4348fae3551SRodney W. Grimes 		q -= 1;
4358fae3551SRodney W. Grimes 		q->count = 0;
436*0c577f4fSColin Percival 		q = &twp->req[0];
4378fae3551SRodney W. Grimes 		if (i == 0) {
4388fae3551SRodney W. Grimes 			q->dblk = 0;
4398fae3551SRodney W. Grimes 			q->count = 1;
4408fae3551SRodney W. Grimes 			trecno = 0;
441*0c577f4fSColin Percival 			nextblock = twp->tblock;
4428fae3551SRodney W. Grimes 			savedtapea = spcl.c_tapea;
443*0c577f4fSColin Percival 			spcl.c_tapea = wp->tapea;
4448fae3551SRodney W. Grimes 			startnewtape(0);
4458fae3551SRodney W. Grimes 			spcl.c_tapea = savedtapea;
4468fae3551SRodney W. Grimes 			lastspclrec = savedtapea - 1;
4478fae3551SRodney W. Grimes 		}
4488fae3551SRodney W. Grimes 		size = (char *)ntb - (char *)q;
449*0c577f4fSColin Percival 		if (atomic(write, wp->fd, (char *)q, size) != size) {
4508fae3551SRodney W. Grimes 			perror("  DUMP: error writing command pipe");
4518fae3551SRodney W. Grimes 			dumpabort(0);
4528fae3551SRodney W. Grimes 		}
453*0c577f4fSColin Percival 		wp->sent = 1;
454*0c577f4fSColin Percival 		if (++wp >= &workers[WORKERS])
455*0c577f4fSColin Percival 			wp = &workers[0];
4568fae3551SRodney W. Grimes 
4578fae3551SRodney W. Grimes 		q->count = 1;
4588fae3551SRodney W. Grimes 
4598fae3551SRodney W. Grimes 		if (prev->dblk != 0) {
4608fae3551SRodney W. Grimes 			/*
4618fae3551SRodney W. Grimes 			 * If the last one was a disk block, make the
4628fae3551SRodney W. Grimes 			 * first of this one be the last bit of that disk
4638fae3551SRodney W. Grimes 			 * block...
4648fae3551SRodney W. Grimes 			 */
4658fae3551SRodney W. Grimes 			q->dblk = prev->dblk +
4668fae3551SRodney W. Grimes 				prev->count * (TP_BSIZE / DEV_BSIZE);
467*0c577f4fSColin Percival 			ntb = (union u_spcl *)twp->tblock;
4688fae3551SRodney W. Grimes 		} else {
4698fae3551SRodney W. Grimes 			/*
4708fae3551SRodney W. Grimes 			 * It wasn't a disk block.  Copy the data to its
4718fae3551SRodney W. Grimes 			 * new location in the buffer.
4728fae3551SRodney W. Grimes 			 */
4738fae3551SRodney W. Grimes 			q->dblk = 0;
474*0c577f4fSColin Percival 			*((union u_spcl *)twp->tblock) = *ntb;
475*0c577f4fSColin Percival 			ntb = (union u_spcl *)twp->tblock[1];
4768fae3551SRodney W. Grimes 		}
4778fae3551SRodney W. Grimes 	}
478*0c577f4fSColin Percival 	wp->req[0] = *q;
479*0c577f4fSColin Percival 	nextblock = wp->tblock;
4808fae3551SRodney W. Grimes 	if (q->dblk == 0)
4818fae3551SRodney W. Grimes 		nextblock++;
4828fae3551SRodney W. Grimes 	trecno = 1;
4838fae3551SRodney W. Grimes 
4848fae3551SRodney W. Grimes 	/*
485a74534b1SConrad Meyer 	 * Clear the first workers' response.  One hopes that it
4868fae3551SRodney W. Grimes 	 * worked ok, otherwise the tape is much too short!
4878fae3551SRodney W. Grimes 	 */
488*0c577f4fSColin Percival 	if (wp->sent) {
489*0c577f4fSColin Percival 		if (atomic(read, wp->fd, (char *)&got, sizeof got)
4908fae3551SRodney W. Grimes 		    != sizeof got) {
4918fae3551SRodney W. Grimes 			perror("  DUMP: error reading command pipe in master");
4928fae3551SRodney W. Grimes 			dumpabort(0);
4938fae3551SRodney W. Grimes 		}
494*0c577f4fSColin Percival 		wp->sent = 0;
4958fae3551SRodney W. Grimes 
4968fae3551SRodney W. Grimes 		if (got != writesize) {
4978fae3551SRodney W. Grimes 			quit("EOT detected at start of the tape!\n");
4988fae3551SRodney W. Grimes 		}
4998fae3551SRodney W. Grimes 	}
5008fae3551SRodney W. Grimes }
5018fae3551SRodney W. Grimes 
5028fae3551SRodney W. Grimes /*
5038fae3551SRodney W. Grimes  * We implement taking and restoring checkpoints on the tape level.
5048fae3551SRodney W. Grimes  * When each tape is opened, a new process is created by forking; this
5058fae3551SRodney W. Grimes  * saves all of the necessary context in the parent.  The child
5068fae3551SRodney W. Grimes  * continues the dump; the parent waits around, saving the context.
5078fae3551SRodney W. Grimes  * If the child returns X_REWRITE, then it had problems writing that tape;
5088fae3551SRodney W. Grimes  * this causes the parent to fork again, duplicating the context, and
5098fae3551SRodney W. Grimes  * everything continues as if nothing had happened.
5108fae3551SRodney W. Grimes  */
5118fae3551SRodney W. Grimes void
5122db673abSWarner Losh startnewtape(int top)
5138fae3551SRodney W. Grimes {
5148fae3551SRodney W. Grimes 	int	parentpid;
5158fae3551SRodney W. Grimes 	int	childpid;
5168fae3551SRodney W. Grimes 	int	status;
5178fae3551SRodney W. Grimes 	char	*p;
5188fae3551SRodney W. Grimes 	sig_t	interrupt_save;
5198fae3551SRodney W. Grimes 
5208fae3551SRodney W. Grimes 	interrupt_save = signal(SIGINT, SIG_IGN);
5218fae3551SRodney W. Grimes 	parentpid = getpid();
5228fae3551SRodney W. Grimes 
5238fae3551SRodney W. Grimes restore_check_point:
5248fae3551SRodney W. Grimes 	(void)signal(SIGINT, interrupt_save);
5258fae3551SRodney W. Grimes 	/*
5268fae3551SRodney W. Grimes 	 *	All signals are inherited...
5278fae3551SRodney W. Grimes 	 */
5282bb823d2SIan Dowse 	setproctitle(NULL);	/* Restore the proctitle. */
5298fae3551SRodney W. Grimes 	childpid = fork();
5308fae3551SRodney W. Grimes 	if (childpid < 0) {
5318fae3551SRodney W. Grimes 		msg("Context save fork fails in parent %d\n", parentpid);
5328fae3551SRodney W. Grimes 		Exit(X_ABORT);
5338fae3551SRodney W. Grimes 	}
5348fae3551SRodney W. Grimes 	if (childpid != 0) {
5358fae3551SRodney W. Grimes 		/*
5368fae3551SRodney W. Grimes 		 *	PARENT:
5378fae3551SRodney W. Grimes 		 *	save the context by waiting
5388fae3551SRodney W. Grimes 		 *	until the child doing all of the work returns.
5398fae3551SRodney W. Grimes 		 *	don't catch the interrupt
5408fae3551SRodney W. Grimes 		 */
5418fae3551SRodney W. Grimes 		signal(SIGINT, SIG_IGN);
5428fae3551SRodney W. Grimes #ifdef TDEBUG
5438fae3551SRodney W. Grimes 		msg("Tape: %d; parent process: %d child process %d\n",
5448fae3551SRodney W. Grimes 			tapeno+1, parentpid, childpid);
5458fae3551SRodney W. Grimes #endif /* TDEBUG */
546c51d70c6SBrian Feldman 		if (waitpid(childpid, &status, 0) == -1)
547c51d70c6SBrian Feldman 			msg("Waiting for child %d: %s\n", childpid,
548c51d70c6SBrian Feldman 			    strerror(errno));
5498fae3551SRodney W. Grimes 		if (status & 0xFF) {
5508fae3551SRodney W. Grimes 			msg("Child %d returns LOB status %o\n",
5518fae3551SRodney W. Grimes 				childpid, status&0xFF);
5528fae3551SRodney W. Grimes 		}
5538fae3551SRodney W. Grimes 		status = (status >> 8) & 0xFF;
5548fae3551SRodney W. Grimes #ifdef TDEBUG
5558fae3551SRodney W. Grimes 		switch(status) {
5568fae3551SRodney W. Grimes 			case X_FINOK:
5578fae3551SRodney W. Grimes 				msg("Child %d finishes X_FINOK\n", childpid);
5588fae3551SRodney W. Grimes 				break;
5598fae3551SRodney W. Grimes 			case X_ABORT:
5608fae3551SRodney W. Grimes 				msg("Child %d finishes X_ABORT\n", childpid);
5618fae3551SRodney W. Grimes 				break;
5628fae3551SRodney W. Grimes 			case X_REWRITE:
5638fae3551SRodney W. Grimes 				msg("Child %d finishes X_REWRITE\n", childpid);
5648fae3551SRodney W. Grimes 				break;
5658fae3551SRodney W. Grimes 			default:
5668fae3551SRodney W. Grimes 				msg("Child %d finishes unknown %d\n",
5678fae3551SRodney W. Grimes 					childpid, status);
5688fae3551SRodney W. Grimes 				break;
5698fae3551SRodney W. Grimes 		}
5708fae3551SRodney W. Grimes #endif /* TDEBUG */
5718fae3551SRodney W. Grimes 		switch(status) {
5728fae3551SRodney W. Grimes 			case X_FINOK:
5738fae3551SRodney W. Grimes 				Exit(X_FINOK);
5748fae3551SRodney W. Grimes 			case X_ABORT:
5758fae3551SRodney W. Grimes 				Exit(X_ABORT);
5768fae3551SRodney W. Grimes 			case X_REWRITE:
5778fae3551SRodney W. Grimes 				goto restore_check_point;
5788fae3551SRodney W. Grimes 			default:
5798fae3551SRodney W. Grimes 				msg("Bad return code from dump: %d\n", status);
5808fae3551SRodney W. Grimes 				Exit(X_ABORT);
5818fae3551SRodney W. Grimes 		}
5828fae3551SRodney W. Grimes 		/*NOTREACHED*/
5838fae3551SRodney W. Grimes 	} else {	/* we are the child; just continue */
5848fae3551SRodney W. Grimes #ifdef TDEBUG
5858fae3551SRodney W. Grimes 		sleep(4);	/* allow time for parent's message to get out */
5868fae3551SRodney W. Grimes 		msg("Child on Tape %d has parent %d, my pid = %d\n",
5878fae3551SRodney W. Grimes 			tapeno+1, parentpid, getpid());
5888fae3551SRodney W. Grimes #endif /* TDEBUG */
5898fae3551SRodney W. Grimes 		/*
5908fae3551SRodney W. Grimes 		 * If we have a name like "/dev/rmt0,/dev/rmt1",
5918fae3551SRodney W. Grimes 		 * use the name before the comma first, and save
5928fae3551SRodney W. Grimes 		 * the remaining names for subsequent volumes.
5938fae3551SRodney W. Grimes 		 */
5948fae3551SRodney W. Grimes 		tapeno++;               /* current tape sequence */
595a37c38b8SPeter Wemm 		if (nexttape || strchr(tape, ',')) {
5968fae3551SRodney W. Grimes 			if (nexttape && *nexttape)
5978fae3551SRodney W. Grimes 				tape = nexttape;
598a37c38b8SPeter Wemm 			if ((p = strchr(tape, ',')) != NULL) {
5998fae3551SRodney W. Grimes 				*p = '\0';
6008fae3551SRodney W. Grimes 				nexttape = p + 1;
6018fae3551SRodney W. Grimes 			} else
6028fae3551SRodney W. Grimes 				nexttape = NULL;
6038fae3551SRodney W. Grimes 			msg("Dumping volume %d on %s\n", tapeno, tape);
6048fae3551SRodney W. Grimes 		}
605c51d70c6SBrian Feldman 		if (pipeout) {
606c51d70c6SBrian Feldman 			tapefd = STDOUT_FILENO;
607c51d70c6SBrian Feldman 		} else if (popenout) {
608c51d70c6SBrian Feldman 			char volno[sizeof("2147483647")];
609c51d70c6SBrian Feldman 
610c51d70c6SBrian Feldman 			(void)sprintf(volno, "%d", spcl.c_volume + 1);
611c51d70c6SBrian Feldman 			if (setenv("DUMP_VOLUME", volno, 1) == -1) {
612c51d70c6SBrian Feldman 				msg("Cannot set $DUMP_VOLUME.\n");
613c51d70c6SBrian Feldman 				dumpabort(0);
614c51d70c6SBrian Feldman 			}
615c51d70c6SBrian Feldman 			popenfp = popen(popenout, "w");
616c51d70c6SBrian Feldman 			if (popenfp == NULL) {
617c51d70c6SBrian Feldman 				msg("Cannot open output pipeline \"%s\".\n",
618c51d70c6SBrian Feldman 				    popenout);
619c51d70c6SBrian Feldman 				dumpabort(0);
620c51d70c6SBrian Feldman 			}
621c51d70c6SBrian Feldman 			tapefd = fileno(popenfp);
622c51d70c6SBrian Feldman 		} else {
6238fae3551SRodney W. Grimes #ifdef RDUMP
6248fae3551SRodney W. Grimes 			while ((tapefd = (host ? rmtopen(tape, 2) :
6258fae3551SRodney W. Grimes 				open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
626c51d70c6SBrian Feldman #else
627c51d70c6SBrian Feldman 			while ((tapefd =
628c51d70c6SBrian Feldman 			    open(tape, O_WRONLY|O_CREAT, 0666)) < 0)
6298fae3551SRodney W. Grimes #endif
6308fae3551SRodney W. Grimes 			    {
6318fae3551SRodney W. Grimes 				msg("Cannot open output \"%s\".\n", tape);
6328fae3551SRodney W. Grimes 				if (!query("Do you want to retry the open?"))
6338fae3551SRodney W. Grimes 					dumpabort(0);
6348fae3551SRodney W. Grimes 			}
635c51d70c6SBrian Feldman 		}
6368fae3551SRodney W. Grimes 
637*0c577f4fSColin Percival 		create_workers();  /* Share open tape file descriptor with workers */
638c51d70c6SBrian Feldman 		if (popenout)
639c51d70c6SBrian Feldman 			close(tapefd);	/* Give up our copy of it. */
64019f8080eSIan Dowse 		signal(SIGINFO, infosch);
6418fae3551SRodney W. Grimes 
6428fae3551SRodney W. Grimes 		asize = 0;
6438fae3551SRodney W. Grimes 		blocksthisvol = 0;
6448fae3551SRodney W. Grimes 		if (top)
6458fae3551SRodney W. Grimes 			newtape++;		/* new tape signal */
646*0c577f4fSColin Percival 		spcl.c_count = wp->count;
6478fae3551SRodney W. Grimes 		/*
6488fae3551SRodney W. Grimes 		 * measure firstrec in TP_BSIZE units since restore doesn't
6498fae3551SRodney W. Grimes 		 * know the correct ntrec value...
6508fae3551SRodney W. Grimes 		 */
651*0c577f4fSColin Percival 		spcl.c_firstrec = wp->firstrec;
6528fae3551SRodney W. Grimes 		spcl.c_volume++;
6538fae3551SRodney W. Grimes 		spcl.c_type = TS_TAPE;
654*0c577f4fSColin Percival 		writeheader((ino_t)wp->inode);
6558fae3551SRodney W. Grimes 		if (tapeno > 1)
6568fae3551SRodney W. Grimes 			msg("Volume %d begins with blocks from inode %d\n",
657*0c577f4fSColin Percival 				tapeno, wp->inode);
6588fae3551SRodney W. Grimes 	}
6598fae3551SRodney W. Grimes }
6608fae3551SRodney W. Grimes 
6618fae3551SRodney W. Grimes void
6622db673abSWarner Losh dumpabort(int signo __unused)
6638fae3551SRodney W. Grimes {
6648fae3551SRodney W. Grimes 
6658fae3551SRodney W. Grimes 	if (master != 0 && master != getpid())
6668fae3551SRodney W. Grimes 		/* Signals master to call dumpabort */
6678fae3551SRodney W. Grimes 		(void) kill(master, SIGTERM);
6688fae3551SRodney W. Grimes 	else {
6698fae3551SRodney W. Grimes 		killall();
6708fae3551SRodney W. Grimes 		msg("The ENTIRE dump is aborted.\n");
6718fae3551SRodney W. Grimes 	}
6728fae3551SRodney W. Grimes #ifdef RDUMP
6738fae3551SRodney W. Grimes 	rmtclose();
6748fae3551SRodney W. Grimes #endif
6758fae3551SRodney W. Grimes 	Exit(X_ABORT);
6768fae3551SRodney W. Grimes }
6778fae3551SRodney W. Grimes 
678eaa86f9dSBruce Evans void
6798fae3551SRodney W. Grimes Exit(status)
6808fae3551SRodney W. Grimes 	int status;
6818fae3551SRodney W. Grimes {
6828fae3551SRodney W. Grimes 
6838fae3551SRodney W. Grimes #ifdef TDEBUG
6848fae3551SRodney W. Grimes 	msg("pid = %d exits with status %d\n", getpid(), status);
6858fae3551SRodney W. Grimes #endif /* TDEBUG */
6868fae3551SRodney W. Grimes 	exit(status);
6878fae3551SRodney W. Grimes }
6888fae3551SRodney W. Grimes 
6898fae3551SRodney W. Grimes /*
690a74534b1SConrad Meyer  * proceed - handler for SIGUSR2, used to synchronize IO between the workers.
6918fae3551SRodney W. Grimes  */
6928fae3551SRodney W. Grimes void
6932db673abSWarner Losh proceed(int signo __unused)
6948fae3551SRodney W. Grimes {
6958fae3551SRodney W. Grimes 
6968fae3551SRodney W. Grimes 	if (ready)
6978fae3551SRodney W. Grimes 		longjmp(jmpbuf, 1);
6988fae3551SRodney W. Grimes 	caught++;
6998fae3551SRodney W. Grimes }
7008fae3551SRodney W. Grimes 
7018fae3551SRodney W. Grimes void
702*0c577f4fSColin Percival create_workers(void)
7038fae3551SRodney W. Grimes {
7048fae3551SRodney W. Grimes 	int cmd[2];
705d2334e27SIan Dowse 	int i, j;
7068fae3551SRodney W. Grimes 
7078fae3551SRodney W. Grimes 	master = getpid();
7088fae3551SRodney W. Grimes 
709a74534b1SConrad Meyer 	signal(SIGTERM, dumpabort);  /* Worker sends SIGTERM on dumpabort() */
7108fae3551SRodney W. Grimes 	signal(SIGPIPE, sigpipe);
711a74534b1SConrad Meyer 	signal(SIGUSR1, tperror);    /* Worker sends SIGUSR1 on tape errors */
712a74534b1SConrad Meyer 	signal(SIGUSR2, proceed);    /* Worker sends SIGUSR2 to next worker */
7138fae3551SRodney W. Grimes 
714a74534b1SConrad Meyer 	for (i = 0; i < WORKERS; i++) {
715*0c577f4fSColin Percival 		if (i == wp - &workers[0]) {
7168fae3551SRodney W. Grimes 			caught = 1;
7178fae3551SRodney W. Grimes 		} else {
7188fae3551SRodney W. Grimes 			caught = 0;
7198fae3551SRodney W. Grimes 		}
7208fae3551SRodney W. Grimes 
7218fae3551SRodney W. Grimes 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
722a74534b1SConrad Meyer 		    (workers[i].pid = fork()) < 0)
723a74534b1SConrad Meyer 			quit("too many workers, %d (recompile smaller): %s\n",
7248fae3551SRodney W. Grimes 			    i, strerror(errno));
7258fae3551SRodney W. Grimes 
726a74534b1SConrad Meyer 		workers[i].fd = cmd[1];
727a74534b1SConrad Meyer 		workers[i].sent = 0;
728a74534b1SConrad Meyer 		if (workers[i].pid == 0) { 	    /* Worker starts up here */
7298fae3551SRodney W. Grimes 			for (j = 0; j <= i; j++)
730a74534b1SConrad Meyer 			        (void) close(workers[j].fd);
7318fae3551SRodney W. Grimes 			signal(SIGINT, SIG_IGN);    /* Master handles this */
732a74534b1SConrad Meyer 			worker(cmd[0], i);
7338fae3551SRodney W. Grimes 			Exit(X_FINOK);
7348fae3551SRodney W. Grimes 		}
7358fae3551SRodney W. Grimes 	}
7368fae3551SRodney W. Grimes 
737a74534b1SConrad Meyer 	for (i = 0; i < WORKERS; i++)
738a74534b1SConrad Meyer 		(void) atomic(write, workers[i].fd,
739a74534b1SConrad Meyer 			      (char *) &workers[(i + 1) % WORKERS].pid,
740a74534b1SConrad Meyer 		              sizeof workers[0].pid);
7418fae3551SRodney W. Grimes 
7428fae3551SRodney W. Grimes 	master = 0;
7438fae3551SRodney W. Grimes }
7448fae3551SRodney W. Grimes 
7458fae3551SRodney W. Grimes void
7462db673abSWarner Losh killall(void)
7478fae3551SRodney W. Grimes {
748d2334e27SIan Dowse 	int i;
7498fae3551SRodney W. Grimes 
750a74534b1SConrad Meyer 	for (i = 0; i < WORKERS; i++)
751a74534b1SConrad Meyer 		if (workers[i].pid > 0) {
752a74534b1SConrad Meyer 			(void) kill(workers[i].pid, SIGKILL);
753a74534b1SConrad Meyer 			workers[i].sent = 0;
754ee359f48SJoerg Wunsch 		}
7558fae3551SRodney W. Grimes }
7568fae3551SRodney W. Grimes 
7578fae3551SRodney W. Grimes /*
7588fae3551SRodney W. Grimes  * Synchronization - each process has a lockfile, and shares file
7598fae3551SRodney W. Grimes  * descriptors to the following process's lockfile.  When our write
7608fae3551SRodney W. Grimes  * completes, we release our lock on the following process's lock-
7618fae3551SRodney W. Grimes  * file, allowing the following process to lock it and proceed. We
7628fae3551SRodney W. Grimes  * get the lock back for the next cycle by swapping descriptors.
7638fae3551SRodney W. Grimes  */
7648fae3551SRodney W. Grimes static void
765a74534b1SConrad Meyer worker(int cmd, int worker_number)
7668fae3551SRodney W. Grimes {
767d2334e27SIan Dowse 	int nread;
768a74534b1SConrad Meyer 	int nextworker, size, wrote, eot_count;
7698fae3551SRodney W. Grimes 
7708fae3551SRodney W. Grimes 	/*
7718fae3551SRodney W. Grimes 	 * Need our own seek pointer.
7728fae3551SRodney W. Grimes 	 */
7738fae3551SRodney W. Grimes 	(void) close(diskfd);
7748fae3551SRodney W. Grimes 	if ((diskfd = open(disk, O_RDONLY)) < 0)
775a74534b1SConrad Meyer 		quit("worker couldn't reopen disk: %s\n", strerror(errno));
7768fae3551SRodney W. Grimes 
7778fae3551SRodney W. Grimes 	/*
778a74534b1SConrad Meyer 	 * Need the pid of the next worker in the loop...
7798fae3551SRodney W. Grimes 	 */
780a74534b1SConrad Meyer 	if ((nread = atomic(read, cmd, (char *)&nextworker, sizeof nextworker))
781a74534b1SConrad Meyer 	    != sizeof nextworker) {
782a74534b1SConrad Meyer 		quit("master/worker protocol botched - didn't get pid of next worker.\n");
7838fae3551SRodney W. Grimes 	}
7848fae3551SRodney W. Grimes 
7858fae3551SRodney W. Grimes 	/*
7868fae3551SRodney W. Grimes 	 * Get list of blocks to dump, read the blocks into tape buffer
7878fae3551SRodney W. Grimes 	 */
788*0c577f4fSColin Percival 	while ((nread = atomic(read, cmd, (char *)wp->req, reqsiz)) == reqsiz) {
789*0c577f4fSColin Percival 		struct req *p = wp->req;
7908fae3551SRodney W. Grimes 
7918fae3551SRodney W. Grimes 		for (trecno = 0; trecno < ntrec;
7928fae3551SRodney W. Grimes 		     trecno += p->count, p += p->count) {
7938fae3551SRodney W. Grimes 			if (p->dblk) {
794*0c577f4fSColin Percival 				blkread(p->dblk, wp->tblock[trecno],
7958fae3551SRodney W. Grimes 					p->count * TP_BSIZE);
7968fae3551SRodney W. Grimes 			} else {
7978fae3551SRodney W. Grimes 				if (p->count != 1 || atomic(read, cmd,
798*0c577f4fSColin Percival 				    (char *)wp->tblock[trecno],
7998fae3551SRodney W. Grimes 				    TP_BSIZE) != TP_BSIZE)
800a74534b1SConrad Meyer 				       quit("master/worker protocol botched.\n");
8018fae3551SRodney W. Grimes 			}
8028fae3551SRodney W. Grimes 		}
8038fae3551SRodney W. Grimes 		if (setjmp(jmpbuf) == 0) {
8048fae3551SRodney W. Grimes 			ready = 1;
8058fae3551SRodney W. Grimes 			if (!caught)
8068fae3551SRodney W. Grimes 				(void) pause();
8078fae3551SRodney W. Grimes 		}
8088fae3551SRodney W. Grimes 		ready = 0;
8098fae3551SRodney W. Grimes 		caught = 0;
8108fae3551SRodney W. Grimes 
8118fae3551SRodney W. Grimes 		/* Try to write the data... */
8128fae3551SRodney W. Grimes 		eot_count = 0;
8138fae3551SRodney W. Grimes 		size = 0;
8148fae3551SRodney W. Grimes 
815617dbd3cSIan Dowse 		wrote = 0;
8168fae3551SRodney W. Grimes 		while (eot_count < 10 && size < writesize) {
8178fae3551SRodney W. Grimes #ifdef RDUMP
8188fae3551SRodney W. Grimes 			if (host)
819*0c577f4fSColin Percival 				wrote = rmtwrite(wp->tblock[0]+size,
8208fae3551SRodney W. Grimes 				    writesize-size);
8218fae3551SRodney W. Grimes 			else
8228fae3551SRodney W. Grimes #endif
823*0c577f4fSColin Percival 				wrote = write(tapefd, wp->tblock[0]+size,
8248fae3551SRodney W. Grimes 				    writesize-size);
8258fae3551SRodney W. Grimes #ifdef WRITEDEBUG
826a74534b1SConrad Meyer 			printf("worker %d wrote %d\n", worker_number, wrote);
8278fae3551SRodney W. Grimes #endif
8288fae3551SRodney W. Grimes 			if (wrote < 0)
8298fae3551SRodney W. Grimes 				break;
8308fae3551SRodney W. Grimes 			if (wrote == 0)
8318fae3551SRodney W. Grimes 				eot_count++;
8328fae3551SRodney W. Grimes 			size += wrote;
8338fae3551SRodney W. Grimes 		}
8348fae3551SRodney W. Grimes 
8358fae3551SRodney W. Grimes #ifdef WRITEDEBUG
8368fae3551SRodney W. Grimes 		if (size != writesize)
837a74534b1SConrad Meyer 		 printf("worker %d only wrote %d out of %d bytes and gave up.\n",
838a74534b1SConrad Meyer 		     worker_number, size, writesize);
8398fae3551SRodney W. Grimes #endif
8408fae3551SRodney W. Grimes 
84149fccf2aSJustin T. Gibbs 		/*
84249fccf2aSJustin T. Gibbs 		 * Handle ENOSPC as an EOT condition.
84349fccf2aSJustin T. Gibbs 		 */
84449fccf2aSJustin T. Gibbs 		if (wrote < 0 && errno == ENOSPC) {
84549fccf2aSJustin T. Gibbs 			wrote = 0;
84649fccf2aSJustin T. Gibbs 			eot_count++;
84749fccf2aSJustin T. Gibbs 		}
84849fccf2aSJustin T. Gibbs 
8498fae3551SRodney W. Grimes 		if (eot_count > 0)
8508fae3551SRodney W. Grimes 			size = 0;
8518fae3551SRodney W. Grimes 
852ee359f48SJoerg Wunsch 		if (wrote < 0) {
8538fae3551SRodney W. Grimes 			(void) kill(master, SIGUSR1);
8548fae3551SRodney W. Grimes 			for (;;)
8558fae3551SRodney W. Grimes 				(void) sigpause(0);
8568fae3551SRodney W. Grimes 		} else {
8578fae3551SRodney W. Grimes 			/*
8588fae3551SRodney W. Grimes 			 * pass size of write back to master
8598fae3551SRodney W. Grimes 			 * (for EOT handling)
8608fae3551SRodney W. Grimes 			 */
8618fae3551SRodney W. Grimes 			(void) atomic(write, cmd, (char *)&size, sizeof size);
8628fae3551SRodney W. Grimes 		}
8638fae3551SRodney W. Grimes 
8648fae3551SRodney W. Grimes 		/*
865a74534b1SConrad Meyer 		 * If partial write, don't want next worker to go.
8668fae3551SRodney W. Grimes 		 * Also jolts him awake.
8678fae3551SRodney W. Grimes 		 */
868a74534b1SConrad Meyer 		(void) kill(nextworker, SIGUSR2);
8698fae3551SRodney W. Grimes 	}
8708fae3551SRodney W. Grimes 	if (nread != 0)
8718fae3551SRodney W. Grimes 		quit("error reading command pipe: %s\n", strerror(errno));
8728fae3551SRodney W. Grimes }
8738fae3551SRodney W. Grimes 
8748fae3551SRodney W. Grimes /*
8758fae3551SRodney W. Grimes  * Since a read from a pipe may not return all we asked for,
8768fae3551SRodney W. Grimes  * or a write may not write all we ask if we get a signal,
8778fae3551SRodney W. Grimes  * loop until the count is satisfied (or error).
8788fae3551SRodney W. Grimes  */
8798fae3551SRodney W. Grimes static int
8802db673abSWarner Losh atomic(ssize_t (*func)(), int fd, char *buf, int count)
8818fae3551SRodney W. Grimes {
8828fae3551SRodney W. Grimes 	int got, need = count;
8838fae3551SRodney W. Grimes 
8848fae3551SRodney W. Grimes 	while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
8858fae3551SRodney W. Grimes 		buf += got;
8868fae3551SRodney W. Grimes 	return (got < 0 ? got : count - need);
8878fae3551SRodney W. Grimes }
888