Home | History | Annotate | Line # | Download | only in dump
tape.c revision 1.21.6.5
      1  1.21.6.5        he /*	$NetBSD: tape.c,v 1.21.6.5 2002/01/16 09:57:46 he 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.12     lukem #include <sys/cdefs.h>
     37       1.1       cgd #ifndef lint
     38       1.6       cgd #if 0
     39      1.14     lukem static char sccsid[] = "@(#)tape.c	8.4 (Berkeley) 5/1/95";
     40       1.6       cgd #else
     41  1.21.6.5        he __RCSID("$NetBSD: tape.c,v 1.21.6.5 2002/01/16 09:57:46 he Exp $");
     42       1.6       cgd #endif
     43       1.1       cgd #endif /* not lint */
     44       1.1       cgd 
     45       1.3   mycroft #include <sys/param.h>
     46       1.3   mycroft #include <sys/socket.h>
     47       1.3   mycroft #include <sys/time.h>
     48       1.3   mycroft #include <sys/wait.h>
     49      1.14     lukem #include <ufs/ufs/dinode.h>
     50  1.21.6.2        he #include <sys/ioctl.h>
     51  1.21.6.2        he #include <sys/mtio.h>
     52       1.3   mycroft 
     53       1.1       cgd #include <protocols/dumprestore.h>
     54       1.3   mycroft 
     55       1.1       cgd #include <errno.h>
     56       1.3   mycroft #include <fcntl.h>
     57       1.1       cgd #include <setjmp.h>
     58       1.3   mycroft #include <signal.h>
     59       1.3   mycroft #include <stdio.h>
     60       1.1       cgd #ifdef __STDC__
     61       1.1       cgd #include <stdlib.h>
     62       1.1       cgd #include <string.h>
     63       1.8     lukem #include <time.h>
     64       1.3   mycroft #include <unistd.h>
     65       1.3   mycroft #else
     66       1.3   mycroft int	write(), read();
     67       1.1       cgd #endif
     68       1.3   mycroft 
     69       1.1       cgd #include "dump.h"
     70       1.1       cgd #include "pathnames.h"
     71       1.1       cgd 
     72       1.1       cgd int	writesize;		/* size of malloc()ed buffer for tape */
     73       1.1       cgd long	lastspclrec = -1;	/* tape block number of last written header */
     74       1.1       cgd int	trecno = 0;		/* next record to write in current block */
     75       1.1       cgd extern	long blocksperfile;	/* number of blocks per output file */
     76       1.1       cgd long	blocksthisvol;		/* number of blocks on current output file */
     77       1.1       cgd extern	int ntrec;		/* blocking factor on tape */
     78       1.1       cgd extern	int cartridge;
     79       1.1       cgd extern	char *host;
     80       1.1       cgd char	*nexttape;
     81       1.3   mycroft 
     82      1.12     lukem static	ssize_t atomic_read __P((int, char *, int));
     83      1.12     lukem static	ssize_t atomic_write __P((int, char *, int));
     84       1.3   mycroft static	void doslave __P((int, int));
     85       1.3   mycroft static	void enslave __P((void));
     86       1.3   mycroft static	void flushtape __P((void));
     87       1.3   mycroft static	void killall __P((void));
     88      1.12     lukem static	void proceed __P((int));
     89       1.3   mycroft static	void rollforward __P((void));
     90      1.12     lukem static	void sigpipe __P((int));
     91      1.12     lukem static	void tperror __P((int));
     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.8     lukem static time_t tstart_volume;	/* time of volume start */
    125       1.8     lukem static int tapea_volume;	/* value of spcl.c_tapea at volume start */
    126       1.8     lukem 
    127       1.1       cgd int master;		/* pid of master, for sending error signals */
    128       1.1       cgd int tenths;		/* length of tape used per block written */
    129       1.1       cgd static int caught;	/* have we caught the signal to proceed? */
    130       1.1       cgd static int ready;	/* have we reached the lock point without having */
    131       1.1       cgd 			/* received the SIGUSR2 signal from the prev slave? */
    132       1.1       cgd static jmp_buf jmpbuf;	/* where to jump to if we are ready when the */
    133       1.1       cgd 			/* SIGUSR2 arrives from the previous slave */
    134       1.1       cgd 
    135       1.1       cgd int
    136       1.1       cgd alloctape()
    137       1.1       cgd {
    138       1.1       cgd 	int pgoff = getpagesize() - 1;
    139       1.1       cgd 	char *buf;
    140       1.1       cgd 	int i;
    141       1.1       cgd 
    142       1.1       cgd 	writesize = ntrec * TP_BSIZE;
    143       1.1       cgd 	reqsiz = (ntrec + 1) * sizeof(struct req);
    144       1.1       cgd 	/*
    145       1.1       cgd 	 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
    146       1.1       cgd 	 * (see DEC TU80 User's Guide).  The shorter gaps of 6250-bpi require
    147       1.1       cgd 	 * repositioning after stopping, i.e, streaming mode, where the gap is
    148       1.1       cgd 	 * variable, 0.30" to 0.45".  The gap is maximal when the tape stops.
    149       1.1       cgd 	 */
    150       1.1       cgd 	if (blocksperfile == 0)
    151       1.1       cgd 		tenths = writesize / density +
    152       1.1       cgd 		    (cartridge ? 16 : density == 625 ? 5 : 8);
    153       1.1       cgd 	/*
    154       1.1       cgd 	 * Allocate tape buffer contiguous with the array of instruction
    155       1.1       cgd 	 * packets, so flushtape() can write them together with one write().
    156       1.1       cgd 	 * Align tape buffer on page boundary to speed up tape write().
    157       1.1       cgd 	 */
    158       1.1       cgd 	for (i = 0; i <= SLAVES; i++) {
    159       1.1       cgd 		buf = (char *)
    160       1.1       cgd 		    malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
    161       1.1       cgd 		if (buf == NULL)
    162       1.1       cgd 			return(0);
    163       1.1       cgd 		slaves[i].tblock = (char (*)[TP_BSIZE])
    164       1.1       cgd 		    (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
    165       1.1       cgd 		slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
    166       1.1       cgd 	}
    167       1.1       cgd 	slp = &slaves[0];
    168       1.1       cgd 	slp->count = 1;
    169       1.1       cgd 	slp->tapea = 0;
    170       1.1       cgd 	slp->firstrec = 0;
    171       1.1       cgd 	nextblock = slp->tblock;
    172       1.1       cgd 	return(1);
    173       1.1       cgd }
    174       1.1       cgd 
    175       1.1       cgd void
    176       1.1       cgd writerec(dp, isspcl)
    177       1.1       cgd 	char *dp;
    178       1.1       cgd 	int isspcl;
    179       1.1       cgd {
    180       1.1       cgd 
    181       1.1       cgd 	slp->req[trecno].dblk = (daddr_t)0;
    182       1.1       cgd 	slp->req[trecno].count = 1;
    183       1.1       cgd 	*(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
    184       1.1       cgd 	if (isspcl)
    185      1.16    bouyer 		lastspclrec = iswap32(spcl.c_tapea);
    186       1.1       cgd 	trecno++;
    187      1.16    bouyer 	spcl.c_tapea = iswap32(iswap32(spcl.c_tapea) +1);
    188       1.1       cgd 	if (trecno >= ntrec)
    189       1.1       cgd 		flushtape();
    190       1.1       cgd }
    191       1.1       cgd 
    192       1.1       cgd void
    193       1.1       cgd dumpblock(blkno, size)
    194       1.1       cgd 	daddr_t blkno;
    195       1.1       cgd 	int size;
    196       1.1       cgd {
    197       1.1       cgd 	int avail, tpblks, dblkno;
    198       1.1       cgd 
    199      1.21  perseant 	dblkno = fsatoda(ufsib, blkno);
    200       1.1       cgd 	tpblks = size >> tp_bshift;
    201       1.1       cgd 	while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
    202       1.1       cgd 		slp->req[trecno].dblk = dblkno;
    203       1.1       cgd 		slp->req[trecno].count = avail;
    204       1.1       cgd 		trecno += avail;
    205      1.16    bouyer 		spcl.c_tapea = iswap32(iswap32(spcl.c_tapea) + avail);
    206       1.1       cgd 		if (trecno >= ntrec)
    207       1.1       cgd 			flushtape();
    208       1.1       cgd 		dblkno += avail << (tp_bshift - dev_bshift);
    209       1.1       cgd 		tpblks -= avail;
    210       1.1       cgd 	}
    211       1.1       cgd }
    212       1.1       cgd 
    213       1.1       cgd int	nogripe = 0;
    214       1.1       cgd 
    215      1.12     lukem static void
    216       1.1       cgd tperror(signo)
    217       1.1       cgd 	int signo;
    218       1.1       cgd {
    219       1.1       cgd 
    220       1.1       cgd 	if (pipeout) {
    221       1.1       cgd 		msg("write error on %s\n", tape);
    222       1.1       cgd 		quit("Cannot recover\n");
    223       1.1       cgd 		/* NOTREACHED */
    224       1.1       cgd 	}
    225  1.21.6.1        tv 	msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
    226       1.1       cgd 	broadcast("DUMP WRITE ERROR!\n");
    227       1.1       cgd 	if (!query("Do you want to restart?"))
    228       1.1       cgd 		dumpabort(0);
    229       1.1       cgd 	msg("Closing this volume.  Prepare to restart with new media;\n");
    230       1.1       cgd 	msg("this dump volume will be rewritten.\n");
    231       1.1       cgd 	killall();
    232       1.1       cgd 	nogripe = 1;
    233       1.1       cgd 	close_rewind();
    234       1.1       cgd 	Exit(X_REWRITE);
    235       1.1       cgd }
    236       1.1       cgd 
    237      1.12     lukem static void
    238       1.1       cgd sigpipe(signo)
    239       1.1       cgd 	int signo;
    240       1.1       cgd {
    241       1.1       cgd 
    242       1.1       cgd 	quit("Broken pipe\n");
    243       1.1       cgd }
    244       1.1       cgd 
    245       1.8     lukem /*
    246       1.8     lukem  * do_stats --
    247       1.8     lukem  *	Update xferrate stats
    248       1.8     lukem  */
    249       1.8     lukem time_t
    250       1.8     lukem do_stats()
    251       1.8     lukem {
    252       1.8     lukem 	time_t tnow, ttaken;
    253       1.8     lukem 	int blocks;
    254       1.8     lukem 
    255       1.8     lukem 	(void)time(&tnow);
    256       1.8     lukem 	ttaken = tnow - tstart_volume;
    257      1.16    bouyer 	blocks = iswap32(spcl.c_tapea) - tapea_volume;
    258       1.8     lukem 	msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
    259       1.8     lukem 	if (ttaken > 0) {
    260       1.8     lukem 		msg("Volume %d took %d:%02d:%02d\n", tapeno,
    261  1.21.6.1        tv 		    (int) (ttaken / 3600), (int) ((ttaken % 3600) / 60),
    262  1.21.6.5        he 		    (int) (ttaken % 60));
    263  1.21.6.1        tv 		msg("Volume %d transfer rate: %d KB/s\n", tapeno,
    264  1.21.6.1        tv 		    (int) (blocks / ttaken));
    265       1.8     lukem 		xferrate += blocks / ttaken;
    266       1.8     lukem 	}
    267       1.8     lukem 	return(tnow);
    268       1.8     lukem }
    269       1.8     lukem 
    270      1.11     lukem /*
    271      1.11     lukem  * statussig --
    272      1.11     lukem  *	information message upon receipt of SIGINFO
    273      1.11     lukem  *	(derived from optr.c::timeest())
    274      1.11     lukem  */
    275      1.11     lukem void
    276      1.11     lukem statussig(notused)
    277      1.11     lukem 	int notused;
    278      1.11     lukem {
    279      1.11     lukem 	time_t	tnow, deltat;
    280      1.11     lukem 	char	msgbuf[128];
    281      1.11     lukem 
    282      1.11     lukem 	if (blockswritten < 500)
    283  1.21.6.5        he 		return;
    284      1.11     lukem 	(void) time((time_t *) &tnow);
    285  1.21.6.5        he 	if (tnow <= tstart_volume)
    286  1.21.6.5        he 		return;
    287  1.21.6.5        he 	deltat = tstart_writing - tnow +
    288  1.21.6.5        he 	    (1.0 * (tnow - tstart_writing)) / blockswritten * tapesize;
    289      1.11     lukem 	(void)snprintf(msgbuf, sizeof(msgbuf),
    290      1.11     lukem 	    "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
    291      1.11     lukem 	    (blockswritten * 100.0) / tapesize,
    292  1.21.6.5        he 	    (long)((iswap32(spcl.c_tapea) - tapea_volume) /
    293  1.21.6.5        he 	      (tnow - tstart_volume)),
    294      1.11     lukem 	    (int)(deltat / 3600), (int)((deltat % 3600) / 60));
    295      1.11     lukem 	write(STDERR_FILENO, msgbuf, strlen(msgbuf));
    296      1.11     lukem }
    297      1.11     lukem 
    298       1.3   mycroft static void
    299       1.1       cgd flushtape()
    300       1.1       cgd {
    301       1.1       cgd 	int i, blks, got;
    302       1.1       cgd 	long lastfirstrec;
    303       1.1       cgd 
    304       1.1       cgd 	int siz = (char *)nextblock - (char *)slp->req;
    305       1.1       cgd 
    306       1.1       cgd 	slp->req[trecno].count = 0;			/* Sentinel */
    307       1.1       cgd 
    308      1.12     lukem 	if (atomic_write(slp->fd, (char *)slp->req, siz) != siz)
    309       1.1       cgd 		quit("error writing command pipe: %s\n", strerror(errno));
    310       1.1       cgd 	slp->sent = 1; /* we sent a request, read the response later */
    311       1.1       cgd 
    312       1.1       cgd 	lastfirstrec = slp->firstrec;
    313       1.1       cgd 
    314       1.1       cgd 	if (++slp >= &slaves[SLAVES])
    315       1.1       cgd 		slp = &slaves[0];
    316       1.1       cgd 
    317       1.1       cgd 	/* Read results back from next slave */
    318       1.1       cgd 	if (slp->sent) {
    319      1.12     lukem 		if (atomic_read(slp->fd, (char *)&got, sizeof got)
    320       1.1       cgd 		    != sizeof got) {
    321       1.1       cgd 			perror("  DUMP: error reading command pipe in master");
    322       1.1       cgd 			dumpabort(0);
    323       1.1       cgd 		}
    324       1.1       cgd 		slp->sent = 0;
    325       1.1       cgd 
    326       1.1       cgd 		/* Check for end of tape */
    327       1.1       cgd 		if (got < writesize) {
    328       1.1       cgd 			msg("End of tape detected\n");
    329       1.1       cgd 
    330       1.1       cgd 			/*
    331       1.1       cgd 			 * Drain the results, don't care what the values were.
    332       1.1       cgd 			 * If we read them here then trewind won't...
    333       1.1       cgd 			 */
    334       1.1       cgd 			for (i = 0; i < SLAVES; i++) {
    335       1.1       cgd 				if (slaves[i].sent) {
    336      1.12     lukem 					if (atomic_read(slaves[i].fd,
    337       1.1       cgd 					    (char *)&got, sizeof got)
    338       1.1       cgd 					    != sizeof got) {
    339       1.1       cgd 						perror("  DUMP: error reading command pipe in master");
    340       1.1       cgd 						dumpabort(0);
    341       1.1       cgd 					}
    342       1.1       cgd 					slaves[i].sent = 0;
    343       1.1       cgd 				}
    344       1.1       cgd 			}
    345       1.1       cgd 
    346       1.1       cgd 			close_rewind();
    347       1.1       cgd 			rollforward();
    348       1.1       cgd 			return;
    349       1.1       cgd 		}
    350       1.1       cgd 	}
    351       1.1       cgd 
    352       1.1       cgd 	blks = 0;
    353      1.16    bouyer 	if (iswap32(spcl.c_type) != TS_END) {
    354      1.16    bouyer 		for (i = 0; i < iswap32(spcl.c_count); i++)
    355       1.1       cgd 			if (spcl.c_addr[i] != 0)
    356       1.1       cgd 				blks++;
    357       1.1       cgd 	}
    358      1.16    bouyer 	slp->count = lastspclrec + blks + 1 - iswap32(spcl.c_tapea);
    359      1.16    bouyer 	slp->tapea = iswap32(spcl.c_tapea);
    360       1.1       cgd 	slp->firstrec = lastfirstrec + ntrec;
    361       1.1       cgd 	slp->inode = curino;
    362       1.1       cgd 	nextblock = slp->tblock;
    363       1.1       cgd 	trecno = 0;
    364       1.1       cgd 	asize += tenths;
    365       1.1       cgd 	blockswritten += ntrec;
    366       1.1       cgd 	blocksthisvol += ntrec;
    367       1.1       cgd 	if (!pipeout && (blocksperfile ?
    368       1.1       cgd 	    (blocksthisvol >= blocksperfile) : (asize > tsize))) {
    369       1.1       cgd 		close_rewind();
    370       1.1       cgd 		startnewtape(0);
    371       1.1       cgd 	}
    372       1.1       cgd 	timeest();
    373       1.1       cgd }
    374       1.1       cgd 
    375       1.1       cgd void
    376  1.21.6.3        he trewind(int eject)
    377       1.1       cgd {
    378       1.1       cgd 	int f;
    379       1.1       cgd 	int got;
    380       1.1       cgd 
    381       1.1       cgd 	for (f = 0; f < SLAVES; f++) {
    382       1.1       cgd 		/*
    383  1.21.6.5        he 		 * Drain the results, but unlike EOT we DO (or should) care
    384  1.21.6.5        he 		 * what the return values were, since if we detect EOT after
    385  1.21.6.5        he 		 * we think we've written the last blocks to the tape anyway,
    386       1.1       cgd 		 * we have to replay those blocks with rollforward.
    387       1.1       cgd 		 *
    388  1.21.6.5        he 		 * fixme: punt for now.
    389       1.1       cgd 		 */
    390       1.1       cgd 		if (slaves[f].sent) {
    391      1.12     lukem 			if (atomic_read(slaves[f].fd, (char *)&got, sizeof got)
    392       1.1       cgd 			    != sizeof got) {
    393       1.1       cgd 				perror("  DUMP: error reading command pipe in master");
    394       1.1       cgd 				dumpabort(0);
    395       1.1       cgd 			}
    396       1.1       cgd 			slaves[f].sent = 0;
    397       1.1       cgd 			if (got != writesize) {
    398       1.1       cgd 				msg("EOT detected in last 2 tape records!\n");
    399       1.1       cgd 				msg("Use a longer tape, decrease the size estimate\n");
    400       1.1       cgd 				quit("or use no size estimate at all.\n");
    401       1.1       cgd 			}
    402       1.1       cgd 		}
    403       1.1       cgd 		(void) close(slaves[f].fd);
    404       1.1       cgd 	}
    405       1.1       cgd 	while (wait((int *)NULL) >= 0)	/* wait for any signals from slaves */
    406       1.1       cgd 		/* void */;
    407       1.1       cgd 
    408       1.1       cgd 	if (pipeout)
    409       1.1       cgd 		return;
    410       1.1       cgd 
    411       1.1       cgd 	msg("Closing %s\n", tape);
    412       1.1       cgd 
    413       1.1       cgd #ifdef RDUMP
    414       1.1       cgd 	if (host) {
    415       1.1       cgd 		rmtclose();
    416  1.21.6.4        he 		while (rmtopen(tape, 0, 0) < 0)
    417       1.1       cgd 			sleep(10);
    418  1.21.6.4        he 		if (eflag && eject) {
    419  1.21.6.4        he 			msg("Ejecting %s\n", tape);
    420  1.21.6.4        he 			(void) rmtioctl(MTOFFL, 0);
    421  1.21.6.4        he 		}
    422       1.1       cgd 		rmtclose();
    423       1.1       cgd 		return;
    424       1.1       cgd 	}
    425       1.1       cgd #endif
    426       1.1       cgd 	(void) close(tapefd);
    427       1.1       cgd 	while ((f = open(tape, 0)) < 0)
    428       1.1       cgd 		sleep (10);
    429  1.21.6.3        he 	if (eflag && eject) {
    430  1.21.6.2        he 		struct mtop offl;
    431  1.21.6.2        he 
    432  1.21.6.2        he 		msg("Ejecting %s\n", tape);
    433  1.21.6.2        he 		offl.mt_op = MTOFFL;
    434  1.21.6.2        he 		offl.mt_count = 0;
    435  1.21.6.2        he 		(void) ioctl(f, MTIOCTOP, &offl);
    436  1.21.6.2        he 	}
    437       1.1       cgd 	(void) close(f);
    438       1.1       cgd }
    439       1.1       cgd 
    440       1.1       cgd void
    441       1.1       cgd close_rewind()
    442       1.1       cgd {
    443  1.21.6.4        he 	int i, f;
    444  1.21.6.4        he 
    445  1.21.6.3        he 	trewind(1);
    446       1.8     lukem 	(void)do_stats();
    447       1.1       cgd 	if (nexttape)
    448       1.1       cgd 		return;
    449       1.1       cgd 	if (!nogripe) {
    450       1.1       cgd 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
    451  1.21.6.5        he 		broadcast("CHANGE DUMP VOLUMES!\a\a\n");
    452       1.1       cgd 	}
    453  1.21.6.4        he 	if (lflag) {
    454  1.21.6.4        he 		for (i = 0; i < lflag / 10; i++) { /* wait lflag seconds */
    455  1.21.6.4        he 			if (host) {
    456  1.21.6.4        he 				if (rmtopen(tape, 0, 0) >= 0) {
    457  1.21.6.4        he 					rmtclose();
    458  1.21.6.4        he 					return;
    459  1.21.6.4        he 				}
    460  1.21.6.4        he 			} else {
    461  1.21.6.4        he 				if ((f = open(tape, 0)) >= 0) {
    462  1.21.6.4        he 					close(f);
    463  1.21.6.4        he 					return;
    464  1.21.6.4        he 				}
    465  1.21.6.4        he 			}
    466  1.21.6.4        he 			sleep (10);
    467  1.21.6.4        he 		}
    468  1.21.6.4        he 	}
    469  1.21.6.5        he 
    470       1.1       cgd 	while (!query("Is the new volume mounted and ready to go?"))
    471       1.1       cgd 		if (query("Do you want to abort?")) {
    472       1.1       cgd 			dumpabort(0);
    473       1.1       cgd 			/*NOTREACHED*/
    474       1.1       cgd 		}
    475       1.1       cgd }
    476       1.1       cgd 
    477       1.1       cgd void
    478       1.1       cgd rollforward()
    479       1.1       cgd {
    480       1.9     lukem 	struct req *p, *q, *prev;
    481       1.9     lukem 	struct slave *tslp;
    482       1.1       cgd 	int i, size, savedtapea, got;
    483       1.1       cgd 	union u_spcl *ntb, *otb;
    484       1.1       cgd 	tslp = &slaves[SLAVES];
    485       1.1       cgd 	ntb = (union u_spcl *)tslp->tblock[1];
    486       1.1       cgd 
    487       1.1       cgd 	/*
    488  1.21.6.5        he 	 * Each of the N slaves should have requests that need to
    489  1.21.6.5        he 	 * be replayed on the next tape.  Use the extra slave buffers
    490  1.21.6.5        he 	 * (slaves[SLAVES]) to construct request lists to be sent to
    491       1.1       cgd 	 * each slave in turn.
    492       1.1       cgd 	 */
    493       1.1       cgd 	for (i = 0; i < SLAVES; i++) {
    494       1.1       cgd 		q = &tslp->req[1];
    495       1.1       cgd 		otb = (union u_spcl *)slp->tblock;
    496       1.1       cgd 
    497       1.1       cgd 		/*
    498  1.21.6.5        he 		 * For each request in the current slave, copy it to tslp.
    499       1.1       cgd 		 */
    500       1.1       cgd 
    501       1.3   mycroft 		prev = NULL;
    502       1.1       cgd 		for (p = slp->req; p->count > 0; p += p->count) {
    503       1.1       cgd 			*q = *p;
    504       1.1       cgd 			if (p->dblk == 0)
    505       1.1       cgd 				*ntb++ = *otb++; /* copy the datablock also */
    506       1.1       cgd 			prev = q;
    507       1.1       cgd 			q += q->count;
    508       1.1       cgd 		}
    509       1.3   mycroft 		if (prev == NULL)
    510       1.3   mycroft 			quit("rollforward: protocol botch");
    511       1.1       cgd 		if (prev->dblk != 0)
    512       1.1       cgd 			prev->count -= 1;
    513       1.1       cgd 		else
    514       1.1       cgd 			ntb--;
    515       1.1       cgd 		q -= 1;
    516       1.1       cgd 		q->count = 0;
    517       1.1       cgd 		q = &tslp->req[0];
    518       1.1       cgd 		if (i == 0) {
    519       1.1       cgd 			q->dblk = 0;
    520       1.1       cgd 			q->count = 1;
    521       1.1       cgd 			trecno = 0;
    522       1.1       cgd 			nextblock = tslp->tblock;
    523      1.16    bouyer 			savedtapea = iswap32(spcl.c_tapea);
    524      1.16    bouyer 			spcl.c_tapea = iswap32(slp->tapea);
    525       1.1       cgd 			startnewtape(0);
    526      1.16    bouyer 			spcl.c_tapea = iswap32(savedtapea);
    527       1.1       cgd 			lastspclrec = savedtapea - 1;
    528       1.1       cgd 		}
    529       1.1       cgd 		size = (char *)ntb - (char *)q;
    530      1.12     lukem 		if (atomic_write(slp->fd, (char *)q, size) != size) {
    531       1.1       cgd 			perror("  DUMP: error writing command pipe");
    532       1.1       cgd 			dumpabort(0);
    533       1.1       cgd 		}
    534       1.1       cgd 		slp->sent = 1;
    535       1.1       cgd 		if (++slp >= &slaves[SLAVES])
    536       1.1       cgd 			slp = &slaves[0];
    537       1.1       cgd 
    538       1.1       cgd 		q->count = 1;
    539       1.1       cgd 
    540       1.1       cgd 		if (prev->dblk != 0) {
    541       1.1       cgd 			/*
    542  1.21.6.5        he 			 * If the last one was a disk block, make the
    543  1.21.6.5        he 			 * first of this one be the last bit of that disk
    544       1.1       cgd 			 * block...
    545       1.1       cgd 			 */
    546       1.1       cgd 			q->dblk = prev->dblk +
    547       1.1       cgd 				prev->count * (TP_BSIZE / DEV_BSIZE);
    548       1.1       cgd 			ntb = (union u_spcl *)tslp->tblock;
    549       1.1       cgd 		} else {
    550       1.1       cgd 			/*
    551  1.21.6.5        he 			 * It wasn't a disk block.  Copy the data to its
    552       1.1       cgd 			 * new location in the buffer.
    553       1.1       cgd 			 */
    554       1.1       cgd 			q->dblk = 0;
    555       1.1       cgd 			*((union u_spcl *)tslp->tblock) = *ntb;
    556       1.1       cgd 			ntb = (union u_spcl *)tslp->tblock[1];
    557       1.1       cgd 		}
    558       1.1       cgd 	}
    559       1.1       cgd 	slp->req[0] = *q;
    560       1.1       cgd 	nextblock = slp->tblock;
    561       1.1       cgd 	if (q->dblk == 0)
    562       1.1       cgd 		nextblock++;
    563       1.1       cgd 	trecno = 1;
    564       1.1       cgd 
    565       1.1       cgd 	/*
    566       1.1       cgd 	 * Clear the first slaves' response.  One hopes that it
    567       1.1       cgd 	 * worked ok, otherwise the tape is much too short!
    568       1.1       cgd 	 */
    569       1.1       cgd 	if (slp->sent) {
    570      1.12     lukem 		if (atomic_read(slp->fd, (char *)&got, sizeof got)
    571       1.1       cgd 		    != sizeof got) {
    572       1.1       cgd 			perror("  DUMP: error reading command pipe in master");
    573       1.1       cgd 			dumpabort(0);
    574       1.1       cgd 		}
    575       1.1       cgd 		slp->sent = 0;
    576       1.1       cgd 
    577       1.1       cgd 		if (got != writesize) {
    578       1.1       cgd 			quit("EOT detected at start of the tape!\n");
    579       1.1       cgd 		}
    580       1.1       cgd 	}
    581       1.1       cgd }
    582       1.1       cgd 
    583       1.1       cgd /*
    584       1.1       cgd  * We implement taking and restoring checkpoints on the tape level.
    585       1.1       cgd  * When each tape is opened, a new process is created by forking; this
    586       1.1       cgd  * saves all of the necessary context in the parent.  The child
    587       1.1       cgd  * continues the dump; the parent waits around, saving the context.
    588       1.1       cgd  * If the child returns X_REWRITE, then it had problems writing that tape;
    589       1.1       cgd  * this causes the parent to fork again, duplicating the context, and
    590       1.1       cgd  * everything continues as if nothing had happened.
    591       1.1       cgd  */
    592       1.1       cgd void
    593       1.1       cgd startnewtape(top)
    594       1.1       cgd 	int top;
    595       1.1       cgd {
    596       1.1       cgd 	int	parentpid;
    597       1.1       cgd 	int	childpid;
    598       1.1       cgd 	int	status;
    599       1.1       cgd 	int	waitpid;
    600       1.1       cgd 	char	*p;
    601       1.1       cgd 	sig_t	interrupt_save;
    602       1.1       cgd 
    603       1.1       cgd 	interrupt_save = signal(SIGINT, SIG_IGN);
    604       1.1       cgd 	parentpid = getpid();
    605      1.16    bouyer 	tapea_volume = iswap32(spcl.c_tapea);
    606       1.8     lukem 	(void)time(&tstart_volume);
    607       1.1       cgd 
    608       1.3   mycroft restore_check_point:
    609       1.1       cgd 	(void)signal(SIGINT, interrupt_save);
    610       1.1       cgd 	/*
    611       1.1       cgd 	 *	All signals are inherited...
    612       1.1       cgd 	 */
    613       1.1       cgd 	childpid = fork();
    614       1.1       cgd 	if (childpid < 0) {
    615       1.1       cgd 		msg("Context save fork fails in parent %d\n", parentpid);
    616       1.1       cgd 		Exit(X_ABORT);
    617       1.1       cgd 	}
    618       1.1       cgd 	if (childpid != 0) {
    619       1.1       cgd 		/*
    620       1.1       cgd 		 *	PARENT:
    621       1.1       cgd 		 *	save the context by waiting
    622       1.1       cgd 		 *	until the child doing all of the work returns.
    623       1.1       cgd 		 *	don't catch the interrupt
    624       1.1       cgd 		 */
    625       1.1       cgd 		signal(SIGINT, SIG_IGN);
    626  1.21.6.5        he 		signal(SIGINFO, SIG_IGN);	/* only want child's stats */
    627       1.1       cgd #ifdef TDEBUG
    628       1.1       cgd 		msg("Tape: %d; parent process: %d child process %d\n",
    629       1.1       cgd 			tapeno+1, parentpid, childpid);
    630       1.3   mycroft #endif /* TDEBUG */
    631       1.1       cgd 		while ((waitpid = wait(&status)) != childpid)
    632       1.1       cgd 			msg("Parent %d waiting for child %d has another child %d return\n",
    633       1.1       cgd 				parentpid, childpid, waitpid);
    634       1.1       cgd 		if (status & 0xFF) {
    635       1.1       cgd 			msg("Child %d returns LOB status %o\n",
    636       1.1       cgd 				childpid, status&0xFF);
    637       1.1       cgd 		}
    638       1.1       cgd 		status = (status >> 8) & 0xFF;
    639       1.1       cgd #ifdef TDEBUG
    640       1.1       cgd 		switch(status) {
    641       1.1       cgd 			case X_FINOK:
    642       1.1       cgd 				msg("Child %d finishes X_FINOK\n", childpid);
    643       1.1       cgd 				break;
    644  1.21.6.5        he 			case X_ABORT:
    645       1.1       cgd 				msg("Child %d finishes X_ABORT\n", childpid);
    646       1.1       cgd 				break;
    647       1.1       cgd 			case X_REWRITE:
    648       1.1       cgd 				msg("Child %d finishes X_REWRITE\n", childpid);
    649       1.1       cgd 				break;
    650       1.1       cgd 			default:
    651       1.1       cgd 				msg("Child %d finishes unknown %d\n",
    652       1.1       cgd 					childpid, status);
    653       1.1       cgd 				break;
    654       1.1       cgd 		}
    655       1.3   mycroft #endif /* TDEBUG */
    656       1.1       cgd 		switch(status) {
    657       1.1       cgd 			case X_FINOK:
    658       1.1       cgd 				Exit(X_FINOK);
    659       1.1       cgd 			case X_ABORT:
    660       1.1       cgd 				Exit(X_ABORT);
    661       1.1       cgd 			case X_REWRITE:
    662       1.1       cgd 				goto restore_check_point;
    663       1.1       cgd 			default:
    664       1.1       cgd 				msg("Bad return code from dump: %d\n", status);
    665       1.1       cgd 				Exit(X_ABORT);
    666       1.1       cgd 		}
    667       1.1       cgd 		/*NOTREACHED*/
    668       1.1       cgd 	} else {	/* we are the child; just continue */
    669  1.21.6.5        he 		signal(SIGINFO, statussig);	/* now want child's stats */
    670       1.1       cgd #ifdef TDEBUG
    671       1.1       cgd 		sleep(4);	/* allow time for parent's message to get out */
    672       1.1       cgd 		msg("Child on Tape %d has parent %d, my pid = %d\n",
    673       1.1       cgd 			tapeno+1, parentpid, getpid());
    674       1.3   mycroft #endif /* TDEBUG */
    675       1.1       cgd 		/*
    676      1.10     lukem 		 * If we have a name like "/dev/rst0,/dev/rst1",
    677       1.1       cgd 		 * use the name before the comma first, and save
    678       1.1       cgd 		 * the remaining names for subsequent volumes.
    679       1.1       cgd 		 */
    680  1.21.6.5        he 		tapeno++;			/* current tape sequence */
    681       1.4   mycroft 		if (nexttape || strchr(tape, ',')) {
    682       1.1       cgd 			if (nexttape && *nexttape)
    683       1.1       cgd 				tape = nexttape;
    684       1.4   mycroft 			if ((p = strchr(tape, ',')) != NULL) {
    685       1.1       cgd 				*p = '\0';
    686       1.1       cgd 				nexttape = p + 1;
    687       1.1       cgd 			} else
    688       1.1       cgd 				nexttape = NULL;
    689       1.1       cgd 			msg("Dumping volume %d on %s\n", tapeno, tape);
    690       1.1       cgd 		}
    691       1.1       cgd #ifdef RDUMP
    692  1.21.6.4        he 		while ((tapefd = (host ? rmtopen(tape, 2, 1) :
    693       1.1       cgd 			pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    694       1.1       cgd #else
    695  1.21.6.5        he 		while ((tapefd = (pipeout ? 1 :
    696       1.1       cgd 				  open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    697       1.1       cgd #endif
    698       1.1       cgd 		    {
    699       1.1       cgd 			msg("Cannot open output \"%s\".\n", tape);
    700       1.1       cgd 			if (!query("Do you want to retry the open?"))
    701       1.1       cgd 				dumpabort(0);
    702       1.1       cgd 		}
    703       1.1       cgd 
    704       1.1       cgd 		enslave();  /* Share open tape file descriptor with slaves */
    705       1.1       cgd 
    706       1.1       cgd 		asize = 0;
    707       1.1       cgd 		blocksthisvol = 0;
    708       1.1       cgd 		if (top)
    709       1.1       cgd 			newtape++;		/* new tape signal */
    710      1.16    bouyer 		spcl.c_count = iswap32(slp->count);
    711       1.1       cgd 		/*
    712       1.1       cgd 		 * measure firstrec in TP_BSIZE units since restore doesn't
    713       1.1       cgd 		 * know the correct ntrec value...
    714       1.1       cgd 		 */
    715      1.16    bouyer 		spcl.c_firstrec = iswap32(slp->firstrec);
    716      1.16    bouyer 		spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
    717      1.16    bouyer 		spcl.c_type = iswap32(TS_TAPE);
    718      1.16    bouyer 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWHEADER);
    719       1.1       cgd 		writeheader((ino_t)slp->inode);
    720      1.16    bouyer 		spcl.c_flags  = iswap32(iswap32(spcl.c_flags) & ~ DR_NEWHEADER);
    721       1.8     lukem 		msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
    722       1.1       cgd 		if (tapeno > 1)
    723       1.1       cgd 			msg("Volume %d begins with blocks from inode %d\n",
    724       1.1       cgd 				tapeno, slp->inode);
    725       1.1       cgd 	}
    726       1.1       cgd }
    727       1.1       cgd 
    728       1.1       cgd void
    729       1.1       cgd dumpabort(signo)
    730       1.1       cgd 	int signo;
    731       1.1       cgd {
    732       1.1       cgd 
    733       1.1       cgd 	if (master != 0 && master != getpid())
    734       1.1       cgd 		/* Signals master to call dumpabort */
    735       1.1       cgd 		(void) kill(master, SIGTERM);
    736       1.1       cgd 	else {
    737       1.1       cgd 		killall();
    738       1.1       cgd 		msg("The ENTIRE dump is aborted.\n");
    739       1.1       cgd 	}
    740       1.2       cgd #ifdef RDUMP
    741       1.2       cgd 	rmtclose();
    742       1.2       cgd #endif
    743       1.1       cgd 	Exit(X_ABORT);
    744       1.1       cgd }
    745       1.1       cgd 
    746      1.13     lukem void
    747       1.1       cgd Exit(status)
    748       1.1       cgd 	int status;
    749       1.1       cgd {
    750       1.1       cgd 
    751       1.1       cgd #ifdef TDEBUG
    752       1.1       cgd 	msg("pid = %d exits with status %d\n", getpid(), status);
    753       1.3   mycroft #endif /* TDEBUG */
    754       1.3   mycroft 	exit(status);
    755       1.1       cgd }
    756       1.1       cgd 
    757       1.1       cgd /*
    758       1.1       cgd  * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
    759       1.1       cgd  */
    760      1.12     lukem static void
    761       1.1       cgd proceed(signo)
    762       1.1       cgd 	int signo;
    763       1.1       cgd {
    764       1.1       cgd 
    765       1.1       cgd 	if (ready)
    766       1.1       cgd 		longjmp(jmpbuf, 1);
    767       1.1       cgd 	caught++;
    768       1.1       cgd }
    769       1.1       cgd 
    770       1.1       cgd void
    771       1.1       cgd enslave()
    772       1.1       cgd {
    773       1.1       cgd 	int cmd[2];
    774       1.9     lukem 	int i, j;
    775       1.1       cgd 
    776       1.1       cgd 	master = getpid();
    777       1.1       cgd 
    778       1.1       cgd 	signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
    779       1.1       cgd 	signal(SIGPIPE, sigpipe);
    780       1.1       cgd 	signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
    781       1.1       cgd 	signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
    782       1.1       cgd 
    783       1.1       cgd 	for (i = 0; i < SLAVES; i++) {
    784       1.1       cgd 		if (i == slp - &slaves[0]) {
    785       1.1       cgd 			caught = 1;
    786       1.1       cgd 		} else {
    787       1.1       cgd 			caught = 0;
    788       1.1       cgd 		}
    789       1.1       cgd 
    790      1.17     lukem 		if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
    791       1.1       cgd 		    (slaves[i].pid = fork()) < 0)
    792       1.1       cgd 			quit("too many slaves, %d (recompile smaller): %s\n",
    793       1.1       cgd 			    i, strerror(errno));
    794       1.1       cgd 
    795       1.1       cgd 		slaves[i].fd = cmd[1];
    796       1.1       cgd 		slaves[i].sent = 0;
    797       1.1       cgd 		if (slaves[i].pid == 0) { 	    /* Slave starts up here */
    798       1.1       cgd 			for (j = 0; j <= i; j++)
    799  1.21.6.5        he 				(void) close(slaves[j].fd);
    800       1.1       cgd 			signal(SIGINT, SIG_IGN);    /* Master handles this */
    801      1.11     lukem 			signal(SIGINFO, SIG_IGN);
    802       1.1       cgd 			doslave(cmd[0], i);
    803       1.1       cgd 			Exit(X_FINOK);
    804       1.1       cgd 		}
    805       1.1       cgd 	}
    806  1.21.6.5        he 
    807       1.1       cgd 	for (i = 0; i < SLAVES; i++)
    808  1.21.6.5        he 		(void) atomic_write(slaves[i].fd,
    809  1.21.6.5        he 				(char *) &slaves[(i + 1) % SLAVES].pid,
    810  1.21.6.5        he 				sizeof slaves[0].pid);
    811  1.21.6.5        he 
    812  1.21.6.5        he 	master = 0;
    813       1.1       cgd }
    814       1.1       cgd 
    815       1.1       cgd void
    816       1.1       cgd killall()
    817       1.1       cgd {
    818       1.9     lukem 	int i;
    819       1.1       cgd 
    820       1.1       cgd 	for (i = 0; i < SLAVES; i++)
    821  1.21.6.5        he 		if (slaves[i].pid > 0) {
    822       1.1       cgd 			(void) kill(slaves[i].pid, SIGKILL);
    823  1.21.6.5        he 			slaves[i].sent = 0;
    824  1.21.6.5        he 		}
    825       1.1       cgd }
    826       1.1       cgd 
    827       1.1       cgd /*
    828       1.1       cgd  * Synchronization - each process has a lockfile, and shares file
    829       1.1       cgd  * descriptors to the following process's lockfile.  When our write
    830       1.1       cgd  * completes, we release our lock on the following process's lock-
    831       1.1       cgd  * file, allowing the following process to lock it and proceed. We
    832       1.1       cgd  * get the lock back for the next cycle by swapping descriptors.
    833       1.1       cgd  */
    834       1.3   mycroft static void
    835       1.1       cgd doslave(cmd, slave_number)
    836       1.9     lukem 	int cmd;
    837       1.1       cgd         int slave_number;
    838       1.1       cgd {
    839  1.21.6.5        he 	int nread, nextslave, size, wrote, eot_count, werror;
    840       1.7   mycroft 	sigset_t sigset;
    841       1.1       cgd 
    842       1.1       cgd 	/*
    843       1.1       cgd 	 * Need our own seek pointer.
    844       1.1       cgd 	 */
    845       1.1       cgd 	(void) close(diskfd);
    846       1.1       cgd 	if ((diskfd = open(disk, O_RDONLY)) < 0)
    847       1.1       cgd 		quit("slave couldn't reopen disk: %s\n", strerror(errno));
    848       1.1       cgd 
    849       1.1       cgd 	/*
    850       1.1       cgd 	 * Need the pid of the next slave in the loop...
    851       1.1       cgd 	 */
    852      1.12     lukem 	if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
    853       1.1       cgd 	    != sizeof nextslave) {
    854       1.1       cgd 		quit("master/slave protocol botched - didn't get pid of next slave.\n");
    855       1.1       cgd 	}
    856       1.1       cgd 
    857       1.1       cgd 	/*
    858       1.1       cgd 	 * Get list of blocks to dump, read the blocks into tape buffer
    859       1.1       cgd 	 */
    860      1.12     lukem 	while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
    861       1.9     lukem 		struct req *p = slp->req;
    862       1.1       cgd 
    863       1.1       cgd 		for (trecno = 0; trecno < ntrec;
    864       1.1       cgd 		     trecno += p->count, p += p->count) {
    865       1.1       cgd 			if (p->dblk) {
    866       1.1       cgd 				bread(p->dblk, slp->tblock[trecno],
    867       1.1       cgd 					p->count * TP_BSIZE);
    868       1.1       cgd 			} else {
    869      1.12     lukem 				if (p->count != 1 || atomic_read(cmd,
    870  1.21.6.5        he 				    (char *)slp->tblock[trecno],
    871       1.1       cgd 				    TP_BSIZE) != TP_BSIZE)
    872       1.1       cgd 				       quit("master/slave protocol botched.\n");
    873       1.1       cgd 			}
    874       1.1       cgd 		}
    875       1.1       cgd 		if (setjmp(jmpbuf) == 0) {
    876       1.1       cgd 			ready = 1;
    877       1.1       cgd 			if (!caught)
    878       1.1       cgd 				(void) pause();
    879       1.1       cgd 		}
    880       1.1       cgd 		ready = 0;
    881       1.1       cgd 		caught = 0;
    882       1.1       cgd 
    883       1.1       cgd 		/* Try to write the data... */
    884       1.1       cgd 		eot_count = 0;
    885       1.1       cgd 		size = 0;
    886  1.21.6.5        he 		werror = 0;
    887       1.1       cgd 
    888       1.1       cgd 		while (eot_count < 10 && size < writesize) {
    889       1.1       cgd #ifdef RDUMP
    890       1.1       cgd 			if (host)
    891       1.1       cgd 				wrote = rmtwrite(slp->tblock[0]+size,
    892       1.1       cgd 				    writesize-size);
    893       1.1       cgd 			else
    894       1.1       cgd #endif
    895       1.1       cgd 				wrote = write(tapefd, slp->tblock[0]+size,
    896       1.1       cgd 				    writesize-size);
    897  1.21.6.5        he 			werror = errno;
    898       1.1       cgd #ifdef WRITEDEBUG
    899  1.21.6.5        he 			fprintf(stderr, "slave %d wrote %d werror %d\n",
    900  1.21.6.5        he 			    slave_number, wrote, werror);
    901       1.1       cgd #endif
    902  1.21.6.5        he 			if (wrote < 0)
    903       1.1       cgd 				break;
    904       1.1       cgd 			if (wrote == 0)
    905       1.1       cgd 				eot_count++;
    906       1.1       cgd 			size += wrote;
    907       1.1       cgd 		}
    908       1.1       cgd 
    909       1.1       cgd #ifdef WRITEDEBUG
    910  1.21.6.5        he 		if (size != writesize)
    911  1.21.6.5        he 			fprintf(stderr,
    912  1.21.6.5        he 		    "slave %d only wrote %d out of %d bytes and gave up.\n",
    913  1.21.6.5        he 			    slave_number, size, writesize);
    914       1.1       cgd #endif
    915       1.1       cgd 
    916  1.21.6.5        he 		/*
    917  1.21.6.5        he 		 * Handle ENOSPC as an EOT condition.
    918  1.21.6.5        he 		 */
    919  1.21.6.5        he 		if (wrote < 0 && werror == ENOSPC) {
    920  1.21.6.5        he 			wrote = 0;
    921  1.21.6.5        he 			eot_count++;
    922  1.21.6.5        he 		}
    923  1.21.6.5        he 
    924       1.1       cgd 		if (eot_count > 0)
    925       1.1       cgd 			size = 0;
    926       1.1       cgd 
    927  1.21.6.5        he 		if (wrote < 0) {
    928       1.1       cgd 			(void) kill(master, SIGUSR1);
    929       1.7   mycroft 			sigemptyset(&sigset);
    930       1.1       cgd 			for (;;)
    931       1.7   mycroft 				sigsuspend(&sigset);
    932       1.1       cgd 		} else {
    933       1.1       cgd 			/*
    934       1.1       cgd 			 * pass size of write back to master
    935       1.1       cgd 			 * (for EOT handling)
    936       1.1       cgd 			 */
    937      1.12     lukem 			(void) atomic_write(cmd, (char *)&size, sizeof size);
    938  1.21.6.5        he 		}
    939       1.1       cgd 
    940       1.1       cgd 		/*
    941       1.1       cgd 		 * If partial write, don't want next slave to go.
    942       1.1       cgd 		 * Also jolts him awake.
    943       1.1       cgd 		 */
    944       1.1       cgd 		(void) kill(nextslave, SIGUSR2);
    945       1.1       cgd 	}
    946      1.18    bouyer 	printcachestats();
    947       1.1       cgd 	if (nread != 0)
    948       1.1       cgd 		quit("error reading command pipe: %s\n", strerror(errno));
    949       1.1       cgd }
    950       1.1       cgd 
    951       1.1       cgd /*
    952       1.1       cgd  * Since a read from a pipe may not return all we asked for,
    953       1.1       cgd  * loop until the count is satisfied (or error).
    954       1.1       cgd  */
    955       1.5       cgd static ssize_t
    956      1.12     lukem atomic_read(fd, buf, count)
    957      1.12     lukem 	int fd;
    958      1.12     lukem 	char *buf;
    959      1.12     lukem 	int count;
    960      1.12     lukem {
    961      1.12     lukem 	ssize_t got, need = count;
    962      1.12     lukem 
    963      1.12     lukem 	while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
    964      1.12     lukem 		buf += got;
    965      1.12     lukem 	return (got < 0 ? got : count - need);
    966      1.12     lukem }
    967      1.12     lukem 
    968      1.12     lukem /*
    969      1.12     lukem  * Since a write may not write all we ask if we get a signal,
    970      1.12     lukem  * loop until the count is satisfied (or error).
    971      1.12     lukem  */
    972      1.12     lukem static ssize_t
    973      1.12     lukem atomic_write(fd, buf, count)
    974       1.5       cgd 	int fd;
    975       1.1       cgd 	char *buf;
    976       1.3   mycroft 	int count;
    977       1.1       cgd {
    978       1.5       cgd 	ssize_t got, need = count;
    979       1.1       cgd 
    980      1.12     lukem 	while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
    981       1.1       cgd 		buf += got;
    982       1.1       cgd 	return (got < 0 ? got : count - need);
    983       1.1       cgd }
    984