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