Home | History | Annotate | Line # | Download | only in dump
tape.c revision 1.7
      1  1.7  mycroft /*	$NetBSD: tape.c,v 1.7 1995/03/21 18:48:47 mycroft Exp $	*/
      2  1.6      cgd 
      3  1.1      cgd /*-
      4  1.3  mycroft  * Copyright (c) 1980, 1991, 1993
      5  1.3  mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.1      cgd #ifndef lint
     37  1.6      cgd #if 0
     38  1.6      cgd static char sccsid[] = "@(#)tape.c	8.2 (Berkeley) 3/17/94";
     39  1.6      cgd #else
     40  1.7  mycroft static char rcsid[] = "$NetBSD: tape.c,v 1.7 1995/03/21 18:48:47 mycroft Exp $";
     41  1.6      cgd #endif
     42  1.1      cgd #endif /* not lint */
     43  1.1      cgd 
     44  1.3  mycroft #include <sys/param.h>
     45  1.3  mycroft #include <sys/socket.h>
     46  1.3  mycroft #include <sys/time.h>
     47  1.3  mycroft #include <sys/wait.h>
     48  1.1      cgd #ifdef sunos
     49  1.3  mycroft #include <sys/vnode.h>
     50  1.3  mycroft 
     51  1.1      cgd #include <ufs/fs.h>
     52  1.3  mycroft #include <ufs/inode.h>
     53  1.1      cgd #else
     54  1.3  mycroft #include <ufs/ffs/fs.h>
     55  1.3  mycroft #include <ufs/ufs/dinode.h>
     56  1.1      cgd #endif
     57  1.3  mycroft 
     58  1.1      cgd #include <protocols/dumprestore.h>
     59  1.3  mycroft 
     60  1.1      cgd #include <errno.h>
     61  1.3  mycroft #include <fcntl.h>
     62  1.1      cgd #include <setjmp.h>
     63  1.3  mycroft #include <signal.h>
     64  1.3  mycroft #include <stdio.h>
     65  1.1      cgd #ifdef __STDC__
     66  1.1      cgd #include <stdlib.h>
     67  1.1      cgd #include <string.h>
     68  1.3  mycroft #include <unistd.h>
     69  1.3  mycroft #else
     70  1.3  mycroft int	write(), read();
     71  1.1      cgd #endif
     72  1.3  mycroft 
     73  1.1      cgd #include "dump.h"
     74  1.1      cgd #include "pathnames.h"
     75  1.1      cgd 
     76  1.1      cgd int	writesize;		/* size of malloc()ed buffer for tape */
     77  1.1      cgd long	lastspclrec = -1;	/* tape block number of last written header */
     78  1.1      cgd int	trecno = 0;		/* next record to write in current block */
     79  1.1      cgd extern	long blocksperfile;	/* number of blocks per output file */
     80  1.1      cgd long	blocksthisvol;		/* number of blocks on current output file */
     81  1.1      cgd extern	int ntrec;		/* blocking factor on tape */
     82  1.1      cgd extern	int cartridge;
     83  1.1      cgd extern	char *host;
     84  1.1      cgd char	*nexttape;
     85  1.3  mycroft 
     86  1.5      cgd static	ssize_t atomic __P((ssize_t (*)(), int, char *, int));
     87  1.3  mycroft static	void doslave __P((int, int));
     88  1.3  mycroft static	void enslave __P((void));
     89  1.3  mycroft static	void flushtape __P((void));
     90  1.3  mycroft static	void killall __P((void));
     91  1.3  mycroft static	void rollforward __P((void));
     92  1.1      cgd 
     93  1.1      cgd /*
     94  1.1      cgd  * Concurrent dump mods (Caltech) - disk block reading and tape writing
     95  1.1      cgd  * are exported to several slave processes.  While one slave writes the
     96  1.1      cgd  * tape, the others read disk blocks; they pass control of the tape in
     97  1.1      cgd  * a ring via signals. The parent process traverses the filesystem and
     98  1.1      cgd  * sends writeheader()'s and lists of daddr's to the slaves via pipes.
     99  1.1      cgd  * The following structure defines the instruction packets sent to slaves.
    100  1.1      cgd  */
    101  1.1      cgd struct req {
    102  1.1      cgd 	daddr_t dblk;
    103  1.1      cgd 	int count;
    104  1.1      cgd };
    105  1.1      cgd int reqsiz;
    106  1.1      cgd 
    107  1.1      cgd #define SLAVES 3		/* 1 slave writing, 1 reading, 1 for slack */
    108  1.1      cgd struct slave {
    109  1.1      cgd 	int tapea;		/* header number at start of this chunk */
    110  1.1      cgd 	int count;		/* count to next header (used for TS_TAPE */
    111  1.1      cgd 				/* after EOT) */
    112  1.1      cgd 	int inode;		/* inode that we are currently dealing with */
    113  1.1      cgd 	int fd;			/* FD for this slave */
    114  1.1      cgd 	int pid;		/* PID for this slave */
    115  1.1      cgd 	int sent;		/* 1 == we've sent this slave requests */
    116  1.1      cgd 	int firstrec;		/* record number of this block */
    117  1.1      cgd 	char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
    118  1.1      cgd 	struct req *req;	/* buffer for requests */
    119  1.1      cgd } slaves[SLAVES+1];
    120  1.1      cgd struct slave *slp;
    121  1.1      cgd 
    122  1.1      cgd char	(*nextblock)[TP_BSIZE];
    123  1.1      cgd 
    124  1.1      cgd int master;		/* pid of master, for sending error signals */
    125  1.1      cgd int tenths;		/* length of tape used per block written */
    126  1.1      cgd static int caught;	/* have we caught the signal to proceed? */
    127  1.1      cgd static int ready;	/* have we reached the lock point without having */
    128  1.1      cgd 			/* received the SIGUSR2 signal from the prev slave? */
    129  1.1      cgd static jmp_buf jmpbuf;	/* where to jump to if we are ready when the */
    130  1.1      cgd 			/* SIGUSR2 arrives from the previous slave */
    131  1.1      cgd 
    132  1.1      cgd int
    133  1.1      cgd alloctape()
    134  1.1      cgd {
    135  1.1      cgd 	int pgoff = getpagesize() - 1;
    136  1.1      cgd 	char *buf;
    137  1.1      cgd 	int i;
    138  1.1      cgd 
    139  1.1      cgd 	writesize = ntrec * TP_BSIZE;
    140  1.1      cgd 	reqsiz = (ntrec + 1) * sizeof(struct req);
    141  1.1      cgd 	/*
    142  1.1      cgd 	 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
    143  1.1      cgd 	 * (see DEC TU80 User's Guide).  The shorter gaps of 6250-bpi require
    144  1.1      cgd 	 * repositioning after stopping, i.e, streaming mode, where the gap is
    145  1.1      cgd 	 * variable, 0.30" to 0.45".  The gap is maximal when the tape stops.
    146  1.1      cgd 	 */
    147  1.1      cgd 	if (blocksperfile == 0)
    148  1.1      cgd 		tenths = writesize / density +
    149  1.1      cgd 		    (cartridge ? 16 : density == 625 ? 5 : 8);
    150  1.1      cgd 	/*
    151  1.1      cgd 	 * Allocate tape buffer contiguous with the array of instruction
    152  1.1      cgd 	 * packets, so flushtape() can write them together with one write().
    153  1.1      cgd 	 * Align tape buffer on page boundary to speed up tape write().
    154  1.1      cgd 	 */
    155  1.1      cgd 	for (i = 0; i <= SLAVES; i++) {
    156  1.1      cgd 		buf = (char *)
    157  1.1      cgd 		    malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
    158  1.1      cgd 		if (buf == NULL)
    159  1.1      cgd 			return(0);
    160  1.1      cgd 		slaves[i].tblock = (char (*)[TP_BSIZE])
    161  1.1      cgd 		    (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
    162  1.1      cgd 		slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
    163  1.1      cgd 	}
    164  1.1      cgd 	slp = &slaves[0];
    165  1.1      cgd 	slp->count = 1;
    166  1.1      cgd 	slp->tapea = 0;
    167  1.1      cgd 	slp->firstrec = 0;
    168  1.1      cgd 	nextblock = slp->tblock;
    169  1.1      cgd 	return(1);
    170  1.1      cgd }
    171  1.1      cgd 
    172  1.1      cgd void
    173  1.1      cgd writerec(dp, isspcl)
    174  1.1      cgd 	char *dp;
    175  1.1      cgd 	int isspcl;
    176  1.1      cgd {
    177  1.1      cgd 
    178  1.1      cgd 	slp->req[trecno].dblk = (daddr_t)0;
    179  1.1      cgd 	slp->req[trecno].count = 1;
    180  1.1      cgd 	*(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
    181  1.1      cgd 	if (isspcl)
    182  1.1      cgd 		lastspclrec = spcl.c_tapea;
    183  1.1      cgd 	trecno++;
    184  1.1      cgd 	spcl.c_tapea++;
    185  1.1      cgd 	if (trecno >= ntrec)
    186  1.1      cgd 		flushtape();
    187  1.1      cgd }
    188  1.1      cgd 
    189  1.1      cgd void
    190  1.1      cgd dumpblock(blkno, size)
    191  1.1      cgd 	daddr_t blkno;
    192  1.1      cgd 	int size;
    193  1.1      cgd {
    194  1.1      cgd 	int avail, tpblks, dblkno;
    195  1.1      cgd 
    196  1.1      cgd 	dblkno = fsbtodb(sblock, blkno);
    197  1.1      cgd 	tpblks = size >> tp_bshift;
    198  1.1      cgd 	while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
    199  1.1      cgd 		slp->req[trecno].dblk = dblkno;
    200  1.1      cgd 		slp->req[trecno].count = avail;
    201  1.1      cgd 		trecno += avail;
    202  1.1      cgd 		spcl.c_tapea += avail;
    203  1.1      cgd 		if (trecno >= ntrec)
    204  1.1      cgd 			flushtape();
    205  1.1      cgd 		dblkno += avail << (tp_bshift - dev_bshift);
    206  1.1      cgd 		tpblks -= avail;
    207  1.1      cgd 	}
    208  1.1      cgd }
    209  1.1      cgd 
    210  1.1      cgd int	nogripe = 0;
    211  1.1      cgd 
    212  1.1      cgd void
    213  1.1      cgd tperror(signo)
    214  1.1      cgd 	int signo;
    215  1.1      cgd {
    216  1.1      cgd 
    217  1.1      cgd 	if (pipeout) {
    218  1.1      cgd 		msg("write error on %s\n", tape);
    219  1.1      cgd 		quit("Cannot recover\n");
    220  1.1      cgd 		/* NOTREACHED */
    221  1.1      cgd 	}
    222  1.1      cgd 	msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno);
    223  1.1      cgd 	broadcast("DUMP WRITE ERROR!\n");
    224  1.1      cgd 	if (!query("Do you want to restart?"))
    225  1.1      cgd 		dumpabort(0);
    226  1.1      cgd 	msg("Closing this volume.  Prepare to restart with new media;\n");
    227  1.1      cgd 	msg("this dump volume will be rewritten.\n");
    228  1.1      cgd 	killall();
    229  1.1      cgd 	nogripe = 1;
    230  1.1      cgd 	close_rewind();
    231  1.1      cgd 	Exit(X_REWRITE);
    232  1.1      cgd }
    233  1.1      cgd 
    234  1.1      cgd void
    235  1.1      cgd sigpipe(signo)
    236  1.1      cgd 	int signo;
    237  1.1      cgd {
    238  1.1      cgd 
    239  1.1      cgd 	quit("Broken pipe\n");
    240  1.1      cgd }
    241  1.1      cgd 
    242  1.3  mycroft static void
    243  1.1      cgd flushtape()
    244  1.1      cgd {
    245  1.1      cgd 	int i, blks, got;
    246  1.1      cgd 	long lastfirstrec;
    247  1.1      cgd 
    248  1.1      cgd 	int siz = (char *)nextblock - (char *)slp->req;
    249  1.1      cgd 
    250  1.1      cgd 	slp->req[trecno].count = 0;			/* Sentinel */
    251  1.1      cgd 
    252  1.1      cgd 	if (atomic(write, slp->fd, (char *)slp->req, siz) != siz)
    253  1.1      cgd 		quit("error writing command pipe: %s\n", strerror(errno));
    254  1.1      cgd 	slp->sent = 1; /* we sent a request, read the response later */
    255  1.1      cgd 
    256  1.1      cgd 	lastfirstrec = slp->firstrec;
    257  1.1      cgd 
    258  1.1      cgd 	if (++slp >= &slaves[SLAVES])
    259  1.1      cgd 		slp = &slaves[0];
    260  1.1      cgd 
    261  1.1      cgd 	/* Read results back from next slave */
    262  1.1      cgd 	if (slp->sent) {
    263  1.1      cgd 		if (atomic(read, slp->fd, (char *)&got, sizeof got)
    264  1.1      cgd 		    != sizeof got) {
    265  1.1      cgd 			perror("  DUMP: error reading command pipe in master");
    266  1.1      cgd 			dumpabort(0);
    267  1.1      cgd 		}
    268  1.1      cgd 		slp->sent = 0;
    269  1.1      cgd 
    270  1.1      cgd 		/* Check for end of tape */
    271  1.1      cgd 		if (got < writesize) {
    272  1.1      cgd 			msg("End of tape detected\n");
    273  1.1      cgd 
    274  1.1      cgd 			/*
    275  1.1      cgd 			 * Drain the results, don't care what the values were.
    276  1.1      cgd 			 * If we read them here then trewind won't...
    277  1.1      cgd 			 */
    278  1.1      cgd 			for (i = 0; i < SLAVES; i++) {
    279  1.1      cgd 				if (slaves[i].sent) {
    280  1.1      cgd 					if (atomic(read, slaves[i].fd,
    281  1.1      cgd 					    (char *)&got, sizeof got)
    282  1.1      cgd 					    != sizeof got) {
    283  1.1      cgd 						perror("  DUMP: error reading command pipe in master");
    284  1.1      cgd 						dumpabort(0);
    285  1.1      cgd 					}
    286  1.1      cgd 					slaves[i].sent = 0;
    287  1.1      cgd 				}
    288  1.1      cgd 			}
    289  1.1      cgd 
    290  1.1      cgd 			close_rewind();
    291  1.1      cgd 			rollforward();
    292  1.1      cgd 			return;
    293  1.1      cgd 		}
    294  1.1      cgd 	}
    295  1.1      cgd 
    296  1.1      cgd 	blks = 0;
    297  1.1      cgd 	if (spcl.c_type != TS_END) {
    298  1.1      cgd 		for (i = 0; i < spcl.c_count; i++)
    299  1.1      cgd 			if (spcl.c_addr[i] != 0)
    300  1.1      cgd 				blks++;
    301  1.1      cgd 	}
    302  1.1      cgd 	slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
    303  1.1      cgd 	slp->tapea = spcl.c_tapea;
    304  1.1      cgd 	slp->firstrec = lastfirstrec + ntrec;
    305  1.1      cgd 	slp->inode = curino;
    306  1.1      cgd 	nextblock = slp->tblock;
    307  1.1      cgd 	trecno = 0;
    308  1.1      cgd 	asize += tenths;
    309  1.1      cgd 	blockswritten += ntrec;
    310  1.1      cgd 	blocksthisvol += ntrec;
    311  1.1      cgd 	if (!pipeout && (blocksperfile ?
    312  1.1      cgd 	    (blocksthisvol >= blocksperfile) : (asize > tsize))) {
    313  1.1      cgd 		close_rewind();
    314  1.1      cgd 		startnewtape(0);
    315  1.1      cgd 	}
    316  1.1      cgd 	timeest();
    317  1.1      cgd }
    318  1.1      cgd 
    319  1.1      cgd void
    320  1.1      cgd trewind()
    321  1.1      cgd {
    322  1.1      cgd 	int f;
    323  1.1      cgd 	int got;
    324  1.1      cgd 
    325  1.1      cgd 	for (f = 0; f < SLAVES; f++) {
    326  1.1      cgd 		/*
    327  1.1      cgd 		 * Drain the results, but unlike EOT we DO (or should) care
    328  1.1      cgd 		 * what the return values were, since if we detect EOT after
    329  1.1      cgd 		 * we think we've written the last blocks to the tape anyway,
    330  1.1      cgd 		 * we have to replay those blocks with rollforward.
    331  1.1      cgd 		 *
    332  1.1      cgd 		 * fixme: punt for now.
    333  1.1      cgd 		 */
    334  1.1      cgd 		if (slaves[f].sent) {
    335  1.1      cgd 			if (atomic(read, slaves[f].fd, (char *)&got, sizeof got)
    336  1.1      cgd 			    != sizeof got) {
    337  1.1      cgd 				perror("  DUMP: error reading command pipe in master");
    338  1.1      cgd 				dumpabort(0);
    339  1.1      cgd 			}
    340  1.1      cgd 			slaves[f].sent = 0;
    341  1.1      cgd 			if (got != writesize) {
    342  1.1      cgd 				msg("EOT detected in last 2 tape records!\n");
    343  1.1      cgd 				msg("Use a longer tape, decrease the size estimate\n");
    344  1.1      cgd 				quit("or use no size estimate at all.\n");
    345  1.1      cgd 			}
    346  1.1      cgd 		}
    347  1.1      cgd 		(void) close(slaves[f].fd);
    348  1.1      cgd 	}
    349  1.1      cgd 	while (wait((int *)NULL) >= 0)	/* wait for any signals from slaves */
    350  1.1      cgd 		/* void */;
    351  1.1      cgd 
    352  1.1      cgd 	if (pipeout)
    353  1.1      cgd 		return;
    354  1.1      cgd 
    355  1.1      cgd 	msg("Closing %s\n", tape);
    356  1.1      cgd 
    357  1.1      cgd #ifdef RDUMP
    358  1.1      cgd 	if (host) {
    359  1.1      cgd 		rmtclose();
    360  1.1      cgd 		while (rmtopen(tape, 0) < 0)
    361  1.1      cgd 			sleep(10);
    362  1.1      cgd 		rmtclose();
    363  1.1      cgd 		return;
    364  1.1      cgd 	}
    365  1.1      cgd #endif
    366  1.1      cgd 	(void) close(tapefd);
    367  1.1      cgd 	while ((f = open(tape, 0)) < 0)
    368  1.1      cgd 		sleep (10);
    369  1.1      cgd 	(void) close(f);
    370  1.1      cgd }
    371  1.1      cgd 
    372  1.1      cgd void
    373  1.1      cgd close_rewind()
    374  1.1      cgd {
    375  1.1      cgd 	trewind();
    376  1.1      cgd 	if (nexttape)
    377  1.1      cgd 		return;
    378  1.1      cgd 	if (!nogripe) {
    379  1.1      cgd 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
    380  1.1      cgd 		broadcast("CHANGE DUMP VOLUMES!\7\7\n");
    381  1.1      cgd 	}
    382  1.1      cgd 	while (!query("Is the new volume mounted and ready to go?"))
    383  1.1      cgd 		if (query("Do you want to abort?")) {
    384  1.1      cgd 			dumpabort(0);
    385  1.1      cgd 			/*NOTREACHED*/
    386  1.1      cgd 		}
    387  1.1      cgd }
    388  1.1      cgd 
    389  1.1      cgd void
    390  1.1      cgd rollforward()
    391  1.1      cgd {
    392  1.1      cgd 	register struct req *p, *q, *prev;
    393  1.1      cgd 	register struct slave *tslp;
    394  1.1      cgd 	int i, size, savedtapea, got;
    395  1.1      cgd 	union u_spcl *ntb, *otb;
    396  1.1      cgd 	tslp = &slaves[SLAVES];
    397  1.1      cgd 	ntb = (union u_spcl *)tslp->tblock[1];
    398  1.1      cgd 
    399  1.1      cgd 	/*
    400  1.1      cgd 	 * Each of the N slaves should have requests that need to
    401  1.1      cgd 	 * be replayed on the next tape.  Use the extra slave buffers
    402  1.1      cgd 	 * (slaves[SLAVES]) to construct request lists to be sent to
    403  1.1      cgd 	 * each slave in turn.
    404  1.1      cgd 	 */
    405  1.1      cgd 	for (i = 0; i < SLAVES; i++) {
    406  1.1      cgd 		q = &tslp->req[1];
    407  1.1      cgd 		otb = (union u_spcl *)slp->tblock;
    408  1.1      cgd 
    409  1.1      cgd 		/*
    410  1.1      cgd 		 * For each request in the current slave, copy it to tslp.
    411  1.1      cgd 		 */
    412  1.1      cgd 
    413  1.3  mycroft 		prev = NULL;
    414  1.1      cgd 		for (p = slp->req; p->count > 0; p += p->count) {
    415  1.1      cgd 			*q = *p;
    416  1.1      cgd 			if (p->dblk == 0)
    417  1.1      cgd 				*ntb++ = *otb++; /* copy the datablock also */
    418  1.1      cgd 			prev = q;
    419  1.1      cgd 			q += q->count;
    420  1.1      cgd 		}
    421  1.3  mycroft 		if (prev == NULL)
    422  1.3  mycroft 			quit("rollforward: protocol botch");
    423  1.1      cgd 		if (prev->dblk != 0)
    424  1.1      cgd 			prev->count -= 1;
    425  1.1      cgd 		else
    426  1.1      cgd 			ntb--;
    427  1.1      cgd 		q -= 1;
    428  1.1      cgd 		q->count = 0;
    429  1.1      cgd 		q = &tslp->req[0];
    430  1.1      cgd 		if (i == 0) {
    431  1.1      cgd 			q->dblk = 0;
    432  1.1      cgd 			q->count = 1;
    433  1.1      cgd 			trecno = 0;
    434  1.1      cgd 			nextblock = tslp->tblock;
    435  1.1      cgd 			savedtapea = spcl.c_tapea;
    436  1.1      cgd 			spcl.c_tapea = slp->tapea;
    437  1.1      cgd 			startnewtape(0);
    438  1.1      cgd 			spcl.c_tapea = savedtapea;
    439  1.1      cgd 			lastspclrec = savedtapea - 1;
    440  1.1      cgd 		}
    441  1.1      cgd 		size = (char *)ntb - (char *)q;
    442  1.1      cgd 		if (atomic(write, slp->fd, (char *)q, size) != size) {
    443  1.1      cgd 			perror("  DUMP: error writing command pipe");
    444  1.1      cgd 			dumpabort(0);
    445  1.1      cgd 		}
    446  1.1      cgd 		slp->sent = 1;
    447  1.1      cgd 		if (++slp >= &slaves[SLAVES])
    448  1.1      cgd 			slp = &slaves[0];
    449  1.1      cgd 
    450  1.1      cgd 		q->count = 1;
    451  1.1      cgd 
    452  1.1      cgd 		if (prev->dblk != 0) {
    453  1.1      cgd 			/*
    454  1.1      cgd 			 * If the last one was a disk block, make the
    455  1.1      cgd 			 * first of this one be the last bit of that disk
    456  1.1      cgd 			 * block...
    457  1.1      cgd 			 */
    458  1.1      cgd 			q->dblk = prev->dblk +
    459  1.1      cgd 				prev->count * (TP_BSIZE / DEV_BSIZE);
    460  1.1      cgd 			ntb = (union u_spcl *)tslp->tblock;
    461  1.1      cgd 		} else {
    462  1.1      cgd 			/*
    463  1.1      cgd 			 * It wasn't a disk block.  Copy the data to its
    464  1.1      cgd 			 * new location in the buffer.
    465  1.1      cgd 			 */
    466  1.1      cgd 			q->dblk = 0;
    467  1.1      cgd 			*((union u_spcl *)tslp->tblock) = *ntb;
    468  1.1      cgd 			ntb = (union u_spcl *)tslp->tblock[1];
    469  1.1      cgd 		}
    470  1.1      cgd 	}
    471  1.1      cgd 	slp->req[0] = *q;
    472  1.1      cgd 	nextblock = slp->tblock;
    473  1.1      cgd 	if (q->dblk == 0)
    474  1.1      cgd 		nextblock++;
    475  1.1      cgd 	trecno = 1;
    476  1.1      cgd 
    477  1.1      cgd 	/*
    478  1.1      cgd 	 * Clear the first slaves' response.  One hopes that it
    479  1.1      cgd 	 * worked ok, otherwise the tape is much too short!
    480  1.1      cgd 	 */
    481  1.1      cgd 	if (slp->sent) {
    482  1.1      cgd 		if (atomic(read, slp->fd, (char *)&got, sizeof got)
    483  1.1      cgd 		    != sizeof got) {
    484  1.1      cgd 			perror("  DUMP: error reading command pipe in master");
    485  1.1      cgd 			dumpabort(0);
    486  1.1      cgd 		}
    487  1.1      cgd 		slp->sent = 0;
    488  1.1      cgd 
    489  1.1      cgd 		if (got != writesize) {
    490  1.1      cgd 			quit("EOT detected at start of the tape!\n");
    491  1.1      cgd 		}
    492  1.1      cgd 	}
    493  1.1      cgd }
    494  1.1      cgd 
    495  1.1      cgd /*
    496  1.1      cgd  * We implement taking and restoring checkpoints on the tape level.
    497  1.1      cgd  * When each tape is opened, a new process is created by forking; this
    498  1.1      cgd  * saves all of the necessary context in the parent.  The child
    499  1.1      cgd  * continues the dump; the parent waits around, saving the context.
    500  1.1      cgd  * If the child returns X_REWRITE, then it had problems writing that tape;
    501  1.1      cgd  * this causes the parent to fork again, duplicating the context, and
    502  1.1      cgd  * everything continues as if nothing had happened.
    503  1.1      cgd  */
    504  1.1      cgd void
    505  1.1      cgd startnewtape(top)
    506  1.1      cgd 	int top;
    507  1.1      cgd {
    508  1.1      cgd 	int	parentpid;
    509  1.1      cgd 	int	childpid;
    510  1.1      cgd 	int	status;
    511  1.1      cgd 	int	waitpid;
    512  1.1      cgd 	char	*p;
    513  1.1      cgd #ifdef sunos
    514  1.1      cgd 	void	(*interrupt_save)();
    515  1.1      cgd #else
    516  1.1      cgd 	sig_t	interrupt_save;
    517  1.1      cgd #endif
    518  1.1      cgd 
    519  1.1      cgd 	interrupt_save = signal(SIGINT, SIG_IGN);
    520  1.1      cgd 	parentpid = getpid();
    521  1.1      cgd 
    522  1.3  mycroft restore_check_point:
    523  1.1      cgd 	(void)signal(SIGINT, interrupt_save);
    524  1.1      cgd 	/*
    525  1.1      cgd 	 *	All signals are inherited...
    526  1.1      cgd 	 */
    527  1.1      cgd 	childpid = fork();
    528  1.1      cgd 	if (childpid < 0) {
    529  1.1      cgd 		msg("Context save fork fails in parent %d\n", parentpid);
    530  1.1      cgd 		Exit(X_ABORT);
    531  1.1      cgd 	}
    532  1.1      cgd 	if (childpid != 0) {
    533  1.1      cgd 		/*
    534  1.1      cgd 		 *	PARENT:
    535  1.1      cgd 		 *	save the context by waiting
    536  1.1      cgd 		 *	until the child doing all of the work returns.
    537  1.1      cgd 		 *	don't catch the interrupt
    538  1.1      cgd 		 */
    539  1.1      cgd 		signal(SIGINT, SIG_IGN);
    540  1.1      cgd #ifdef TDEBUG
    541  1.1      cgd 		msg("Tape: %d; parent process: %d child process %d\n",
    542  1.1      cgd 			tapeno+1, parentpid, childpid);
    543  1.3  mycroft #endif /* TDEBUG */
    544  1.1      cgd 		while ((waitpid = wait(&status)) != childpid)
    545  1.1      cgd 			msg("Parent %d waiting for child %d has another child %d return\n",
    546  1.1      cgd 				parentpid, childpid, waitpid);
    547  1.1      cgd 		if (status & 0xFF) {
    548  1.1      cgd 			msg("Child %d returns LOB status %o\n",
    549  1.1      cgd 				childpid, status&0xFF);
    550  1.1      cgd 		}
    551  1.1      cgd 		status = (status >> 8) & 0xFF;
    552  1.1      cgd #ifdef TDEBUG
    553  1.1      cgd 		switch(status) {
    554  1.1      cgd 			case X_FINOK:
    555  1.1      cgd 				msg("Child %d finishes X_FINOK\n", childpid);
    556  1.1      cgd 				break;
    557  1.1      cgd 			case X_ABORT:
    558  1.1      cgd 				msg("Child %d finishes X_ABORT\n", childpid);
    559  1.1      cgd 				break;
    560  1.1      cgd 			case X_REWRITE:
    561  1.1      cgd 				msg("Child %d finishes X_REWRITE\n", childpid);
    562  1.1      cgd 				break;
    563  1.1      cgd 			default:
    564  1.1      cgd 				msg("Child %d finishes unknown %d\n",
    565  1.1      cgd 					childpid, status);
    566  1.1      cgd 				break;
    567  1.1      cgd 		}
    568  1.3  mycroft #endif /* TDEBUG */
    569  1.1      cgd 		switch(status) {
    570  1.1      cgd 			case X_FINOK:
    571  1.1      cgd 				Exit(X_FINOK);
    572  1.1      cgd 			case X_ABORT:
    573  1.1      cgd 				Exit(X_ABORT);
    574  1.1      cgd 			case X_REWRITE:
    575  1.1      cgd 				goto restore_check_point;
    576  1.1      cgd 			default:
    577  1.1      cgd 				msg("Bad return code from dump: %d\n", status);
    578  1.1      cgd 				Exit(X_ABORT);
    579  1.1      cgd 		}
    580  1.1      cgd 		/*NOTREACHED*/
    581  1.1      cgd 	} else {	/* we are the child; just continue */
    582  1.1      cgd #ifdef TDEBUG
    583  1.1      cgd 		sleep(4);	/* allow time for parent's message to get out */
    584  1.1      cgd 		msg("Child on Tape %d has parent %d, my pid = %d\n",
    585  1.1      cgd 			tapeno+1, parentpid, getpid());
    586  1.3  mycroft #endif /* TDEBUG */
    587  1.1      cgd 		/*
    588  1.1      cgd 		 * If we have a name like "/dev/rmt0,/dev/rmt1",
    589  1.1      cgd 		 * use the name before the comma first, and save
    590  1.1      cgd 		 * the remaining names for subsequent volumes.
    591  1.1      cgd 		 */
    592  1.1      cgd 		tapeno++;               /* current tape sequence */
    593  1.4  mycroft 		if (nexttape || strchr(tape, ',')) {
    594  1.1      cgd 			if (nexttape && *nexttape)
    595  1.1      cgd 				tape = nexttape;
    596  1.4  mycroft 			if ((p = strchr(tape, ',')) != NULL) {
    597  1.1      cgd 				*p = '\0';
    598  1.1      cgd 				nexttape = p + 1;
    599  1.1      cgd 			} else
    600  1.1      cgd 				nexttape = NULL;
    601  1.1      cgd 			msg("Dumping volume %d on %s\n", tapeno, tape);
    602  1.1      cgd 		}
    603  1.1      cgd #ifdef RDUMP
    604  1.1      cgd 		while ((tapefd = (host ? rmtopen(tape, 2) :
    605  1.1      cgd 			pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    606  1.1      cgd #else
    607  1.1      cgd 		while ((tapefd = (pipeout ? 1 :
    608  1.1      cgd 				  open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    609  1.1      cgd #endif
    610  1.1      cgd 		    {
    611  1.1      cgd 			msg("Cannot open output \"%s\".\n", tape);
    612  1.1      cgd 			if (!query("Do you want to retry the open?"))
    613  1.1      cgd 				dumpabort(0);
    614  1.1      cgd 		}
    615  1.1      cgd 
    616  1.1      cgd 		enslave();  /* Share open tape file descriptor with slaves */
    617  1.1      cgd 
    618  1.1      cgd 		asize = 0;
    619  1.1      cgd 		blocksthisvol = 0;
    620  1.1      cgd 		if (top)
    621  1.1      cgd 			newtape++;		/* new tape signal */
    622  1.1      cgd 		spcl.c_count = slp->count;
    623  1.1      cgd 		/*
    624  1.1      cgd 		 * measure firstrec in TP_BSIZE units since restore doesn't
    625  1.1      cgd 		 * know the correct ntrec value...
    626  1.1      cgd 		 */
    627  1.1      cgd 		spcl.c_firstrec = slp->firstrec;
    628  1.1      cgd 		spcl.c_volume++;
    629  1.1      cgd 		spcl.c_type = TS_TAPE;
    630  1.1      cgd 		spcl.c_flags |= DR_NEWHEADER;
    631  1.1      cgd 		writeheader((ino_t)slp->inode);
    632  1.1      cgd 		spcl.c_flags &=~ DR_NEWHEADER;
    633  1.1      cgd 		if (tapeno > 1)
    634  1.1      cgd 			msg("Volume %d begins with blocks from inode %d\n",
    635  1.1      cgd 				tapeno, slp->inode);
    636  1.1      cgd 	}
    637  1.1      cgd }
    638  1.1      cgd 
    639  1.1      cgd void
    640  1.1      cgd dumpabort(signo)
    641  1.1      cgd 	int signo;
    642  1.1      cgd {
    643  1.1      cgd 
    644  1.1      cgd 	if (master != 0 && master != getpid())
    645  1.1      cgd 		/* Signals master to call dumpabort */
    646  1.1      cgd 		(void) kill(master, SIGTERM);
    647  1.1      cgd 	else {
    648  1.1      cgd 		killall();
    649  1.1      cgd 		msg("The ENTIRE dump is aborted.\n");
    650  1.1      cgd 	}
    651  1.2      cgd #ifdef RDUMP
    652  1.2      cgd 	rmtclose();
    653  1.2      cgd #endif
    654  1.1      cgd 	Exit(X_ABORT);
    655  1.1      cgd }
    656  1.1      cgd 
    657  1.3  mycroft __dead void
    658  1.1      cgd Exit(status)
    659  1.1      cgd 	int status;
    660  1.1      cgd {
    661  1.1      cgd 
    662  1.1      cgd #ifdef TDEBUG
    663  1.1      cgd 	msg("pid = %d exits with status %d\n", getpid(), status);
    664  1.3  mycroft #endif /* TDEBUG */
    665  1.3  mycroft 	exit(status);
    666  1.1      cgd }
    667  1.1      cgd 
    668  1.1      cgd /*
    669  1.1      cgd  * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
    670  1.1      cgd  */
    671  1.1      cgd void
    672  1.1      cgd proceed(signo)
    673  1.1      cgd 	int signo;
    674  1.1      cgd {
    675  1.1      cgd 
    676  1.1      cgd 	if (ready)
    677  1.1      cgd 		longjmp(jmpbuf, 1);
    678  1.1      cgd 	caught++;
    679  1.1      cgd }
    680  1.1      cgd 
    681  1.1      cgd void
    682  1.1      cgd enslave()
    683  1.1      cgd {
    684  1.1      cgd 	int cmd[2];
    685  1.1      cgd 	register int i, j;
    686  1.1      cgd 
    687  1.1      cgd 	master = getpid();
    688  1.1      cgd 
    689  1.1      cgd 	signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
    690  1.1      cgd 	signal(SIGPIPE, sigpipe);
    691  1.1      cgd 	signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
    692  1.1      cgd 	signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
    693  1.1      cgd 
    694  1.1      cgd 	for (i = 0; i < SLAVES; i++) {
    695  1.1      cgd 		if (i == slp - &slaves[0]) {
    696  1.1      cgd 			caught = 1;
    697  1.1      cgd 		} else {
    698  1.1      cgd 			caught = 0;
    699  1.1      cgd 		}
    700  1.1      cgd 
    701  1.1      cgd 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
    702  1.1      cgd 		    (slaves[i].pid = fork()) < 0)
    703  1.1      cgd 			quit("too many slaves, %d (recompile smaller): %s\n",
    704  1.1      cgd 			    i, strerror(errno));
    705  1.1      cgd 
    706  1.1      cgd 		slaves[i].fd = cmd[1];
    707  1.1      cgd 		slaves[i].sent = 0;
    708  1.1      cgd 		if (slaves[i].pid == 0) { 	    /* Slave starts up here */
    709  1.1      cgd 			for (j = 0; j <= i; j++)
    710  1.1      cgd 			        (void) close(slaves[j].fd);
    711  1.1      cgd 			signal(SIGINT, SIG_IGN);    /* Master handles this */
    712  1.1      cgd 			doslave(cmd[0], i);
    713  1.1      cgd 			Exit(X_FINOK);
    714  1.1      cgd 		}
    715  1.1      cgd 	}
    716  1.1      cgd 
    717  1.1      cgd 	for (i = 0; i < SLAVES; i++)
    718  1.1      cgd 		(void) atomic(write, slaves[i].fd,
    719  1.1      cgd 			      (char *) &slaves[(i + 1) % SLAVES].pid,
    720  1.1      cgd 		              sizeof slaves[0].pid);
    721  1.1      cgd 
    722  1.1      cgd 	master = 0;
    723  1.1      cgd }
    724  1.1      cgd 
    725  1.1      cgd void
    726  1.1      cgd killall()
    727  1.1      cgd {
    728  1.1      cgd 	register int i;
    729  1.1      cgd 
    730  1.1      cgd 	for (i = 0; i < SLAVES; i++)
    731  1.1      cgd 		if (slaves[i].pid > 0)
    732  1.1      cgd 			(void) kill(slaves[i].pid, SIGKILL);
    733  1.1      cgd }
    734  1.1      cgd 
    735  1.1      cgd /*
    736  1.1      cgd  * Synchronization - each process has a lockfile, and shares file
    737  1.1      cgd  * descriptors to the following process's lockfile.  When our write
    738  1.1      cgd  * completes, we release our lock on the following process's lock-
    739  1.1      cgd  * file, allowing the following process to lock it and proceed. We
    740  1.1      cgd  * get the lock back for the next cycle by swapping descriptors.
    741  1.1      cgd  */
    742  1.3  mycroft static void
    743  1.1      cgd doslave(cmd, slave_number)
    744  1.1      cgd 	register int cmd;
    745  1.1      cgd         int slave_number;
    746  1.1      cgd {
    747  1.1      cgd 	register int nread;
    748  1.1      cgd 	int nextslave, size, wrote, eot_count;
    749  1.7  mycroft 	sigset_t sigset;
    750  1.1      cgd 
    751  1.1      cgd 	/*
    752  1.1      cgd 	 * Need our own seek pointer.
    753  1.1      cgd 	 */
    754  1.1      cgd 	(void) close(diskfd);
    755  1.1      cgd 	if ((diskfd = open(disk, O_RDONLY)) < 0)
    756  1.1      cgd 		quit("slave couldn't reopen disk: %s\n", strerror(errno));
    757  1.1      cgd 
    758  1.1      cgd 	/*
    759  1.1      cgd 	 * Need the pid of the next slave in the loop...
    760  1.1      cgd 	 */
    761  1.1      cgd 	if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave))
    762  1.1      cgd 	    != sizeof nextslave) {
    763  1.1      cgd 		quit("master/slave protocol botched - didn't get pid of next slave.\n");
    764  1.1      cgd 	}
    765  1.1      cgd 
    766  1.1      cgd 	/*
    767  1.1      cgd 	 * Get list of blocks to dump, read the blocks into tape buffer
    768  1.1      cgd 	 */
    769  1.1      cgd 	while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) {
    770  1.1      cgd 		register struct req *p = slp->req;
    771  1.1      cgd 
    772  1.1      cgd 		for (trecno = 0; trecno < ntrec;
    773  1.1      cgd 		     trecno += p->count, p += p->count) {
    774  1.1      cgd 			if (p->dblk) {
    775  1.1      cgd 				bread(p->dblk, slp->tblock[trecno],
    776  1.1      cgd 					p->count * TP_BSIZE);
    777  1.1      cgd 			} else {
    778  1.1      cgd 				if (p->count != 1 || atomic(read, cmd,
    779  1.1      cgd 				    (char *)slp->tblock[trecno],
    780  1.1      cgd 				    TP_BSIZE) != TP_BSIZE)
    781  1.1      cgd 				       quit("master/slave protocol botched.\n");
    782  1.1      cgd 			}
    783  1.1      cgd 		}
    784  1.1      cgd 		if (setjmp(jmpbuf) == 0) {
    785  1.1      cgd 			ready = 1;
    786  1.1      cgd 			if (!caught)
    787  1.1      cgd 				(void) pause();
    788  1.1      cgd 		}
    789  1.1      cgd 		ready = 0;
    790  1.1      cgd 		caught = 0;
    791  1.1      cgd 
    792  1.1      cgd 		/* Try to write the data... */
    793  1.1      cgd 		eot_count = 0;
    794  1.1      cgd 		size = 0;
    795  1.1      cgd 
    796  1.1      cgd 		while (eot_count < 10 && size < writesize) {
    797  1.1      cgd #ifdef RDUMP
    798  1.1      cgd 			if (host)
    799  1.1      cgd 				wrote = rmtwrite(slp->tblock[0]+size,
    800  1.1      cgd 				    writesize-size);
    801  1.1      cgd 			else
    802  1.1      cgd #endif
    803  1.1      cgd 				wrote = write(tapefd, slp->tblock[0]+size,
    804  1.1      cgd 				    writesize-size);
    805  1.1      cgd #ifdef WRITEDEBUG
    806  1.1      cgd 			printf("slave %d wrote %d\n", slave_number, wrote);
    807  1.1      cgd #endif
    808  1.1      cgd 			if (wrote < 0)
    809  1.1      cgd 				break;
    810  1.1      cgd 			if (wrote == 0)
    811  1.1      cgd 				eot_count++;
    812  1.1      cgd 			size += wrote;
    813  1.1      cgd 		}
    814  1.1      cgd 
    815  1.1      cgd #ifdef WRITEDEBUG
    816  1.1      cgd 		if (size != writesize)
    817  1.1      cgd 		 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
    818  1.1      cgd 		     slave_number, size, writesize);
    819  1.1      cgd #endif
    820  1.1      cgd 
    821  1.1      cgd 		if (eot_count > 0)
    822  1.1      cgd 			size = 0;
    823  1.1      cgd 
    824  1.1      cgd 		/*
    825  1.1      cgd 		 * fixme: Pyramids running OSx return ENOSPC
    826  1.1      cgd 		 * at EOT on 1/2 inch drives.
    827  1.1      cgd 		 */
    828  1.1      cgd 		if (size < 0) {
    829  1.1      cgd 			(void) kill(master, SIGUSR1);
    830  1.7  mycroft 			sigemptyset(&sigset);
    831  1.1      cgd 			for (;;)
    832  1.7  mycroft 				sigsuspend(&sigset);
    833  1.1      cgd 		} else {
    834  1.1      cgd 			/*
    835  1.1      cgd 			 * pass size of write back to master
    836  1.1      cgd 			 * (for EOT handling)
    837  1.1      cgd 			 */
    838  1.1      cgd 			(void) atomic(write, cmd, (char *)&size, sizeof size);
    839  1.1      cgd 		}
    840  1.1      cgd 
    841  1.1      cgd 		/*
    842  1.1      cgd 		 * If partial write, don't want next slave to go.
    843  1.1      cgd 		 * Also jolts him awake.
    844  1.1      cgd 		 */
    845  1.1      cgd 		(void) kill(nextslave, SIGUSR2);
    846  1.1      cgd 	}
    847  1.1      cgd 	if (nread != 0)
    848  1.1      cgd 		quit("error reading command pipe: %s\n", strerror(errno));
    849  1.1      cgd }
    850  1.1      cgd 
    851  1.1      cgd /*
    852  1.1      cgd  * Since a read from a pipe may not return all we asked for,
    853  1.1      cgd  * or a write may not write all we ask if we get a signal,
    854  1.1      cgd  * loop until the count is satisfied (or error).
    855  1.1      cgd  */
    856  1.5      cgd static ssize_t
    857  1.1      cgd atomic(func, fd, buf, count)
    858  1.5      cgd 	ssize_t (*func)();
    859  1.5      cgd 	int fd;
    860  1.1      cgd 	char *buf;
    861  1.3  mycroft 	int count;
    862  1.1      cgd {
    863  1.5      cgd 	ssize_t got, need = count;
    864  1.1      cgd 
    865  1.1      cgd 	while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
    866  1.1      cgd 		buf += got;
    867  1.1      cgd 	return (got < 0 ? got : count - need);
    868  1.1      cgd }
    869