Home | History | Annotate | Line # | Download | only in dump
tape.c revision 1.14
      1 /*	$NetBSD: tape.c,v 1.14 1997/09/16 06:41:22 lukem 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.14 1997/09/16 06:41:22 lukem 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 = spcl.c_tapea;
    192 	trecno++;
    193 	spcl.c_tapea++;
    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 += 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 %d 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 = 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 		    ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
    268 		msg("Volume %d transfer rate: %ld KB/s\n", tapeno,
    269 		    blocks / ttaken);
    270 		xferrate += blocks / ttaken;
    271 	}
    272 	return(tnow);
    273 }
    274 
    275 /*
    276  * statussig --
    277  *	information message upon receipt of SIGINFO
    278  *	(derived from optr.c::timeest())
    279  */
    280 void
    281 statussig(notused)
    282 	int notused;
    283 {
    284 	time_t	tnow, deltat;
    285 	char	msgbuf[128];
    286 
    287 	if (blockswritten < 500)
    288 		return;
    289 	(void) time((time_t *) &tnow);
    290 	deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
    291 		/ blockswritten * tapesize;
    292 	(void)snprintf(msgbuf, sizeof(msgbuf),
    293 	    "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
    294 	    (blockswritten * 100.0) / tapesize,
    295 	    (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
    296 	    (int)(deltat / 3600), (int)((deltat % 3600) / 60));
    297 	write(STDERR_FILENO, msgbuf, strlen(msgbuf));
    298 }
    299 
    300 static void
    301 flushtape()
    302 {
    303 	int i, blks, got;
    304 	long lastfirstrec;
    305 
    306 	int siz = (char *)nextblock - (char *)slp->req;
    307 
    308 	slp->req[trecno].count = 0;			/* Sentinel */
    309 
    310 	if (atomic_write(slp->fd, (char *)slp->req, siz) != siz)
    311 		quit("error writing command pipe: %s\n", strerror(errno));
    312 	slp->sent = 1; /* we sent a request, read the response later */
    313 
    314 	lastfirstrec = slp->firstrec;
    315 
    316 	if (++slp >= &slaves[SLAVES])
    317 		slp = &slaves[0];
    318 
    319 	/* Read results back from next slave */
    320 	if (slp->sent) {
    321 		if (atomic_read(slp->fd, (char *)&got, sizeof got)
    322 		    != sizeof got) {
    323 			perror("  DUMP: error reading command pipe in master");
    324 			dumpabort(0);
    325 		}
    326 		slp->sent = 0;
    327 
    328 		/* Check for end of tape */
    329 		if (got < writesize) {
    330 			msg("End of tape detected\n");
    331 
    332 			/*
    333 			 * Drain the results, don't care what the values were.
    334 			 * If we read them here then trewind won't...
    335 			 */
    336 			for (i = 0; i < SLAVES; i++) {
    337 				if (slaves[i].sent) {
    338 					if (atomic_read(slaves[i].fd,
    339 					    (char *)&got, sizeof got)
    340 					    != sizeof got) {
    341 						perror("  DUMP: error reading command pipe in master");
    342 						dumpabort(0);
    343 					}
    344 					slaves[i].sent = 0;
    345 				}
    346 			}
    347 
    348 			close_rewind();
    349 			rollforward();
    350 			return;
    351 		}
    352 	}
    353 
    354 	blks = 0;
    355 	if (spcl.c_type != TS_END) {
    356 		for (i = 0; i < spcl.c_count; i++)
    357 			if (spcl.c_addr[i] != 0)
    358 				blks++;
    359 	}
    360 	slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
    361 	slp->tapea = spcl.c_tapea;
    362 	slp->firstrec = lastfirstrec + ntrec;
    363 	slp->inode = curino;
    364 	nextblock = slp->tblock;
    365 	trecno = 0;
    366 	asize += tenths;
    367 	blockswritten += ntrec;
    368 	blocksthisvol += ntrec;
    369 	if (!pipeout && (blocksperfile ?
    370 	    (blocksthisvol >= blocksperfile) : (asize > tsize))) {
    371 		close_rewind();
    372 		startnewtape(0);
    373 	}
    374 	timeest();
    375 }
    376 
    377 void
    378 trewind()
    379 {
    380 	int f;
    381 	int got;
    382 
    383 	for (f = 0; f < SLAVES; f++) {
    384 		/*
    385 		 * Drain the results, but unlike EOT we DO (or should) care
    386 		 * what the return values were, since if we detect EOT after
    387 		 * we think we've written the last blocks to the tape anyway,
    388 		 * we have to replay those blocks with rollforward.
    389 		 *
    390 		 * fixme: punt for now.
    391 		 */
    392 		if (slaves[f].sent) {
    393 			if (atomic_read(slaves[f].fd, (char *)&got, sizeof got)
    394 			    != sizeof got) {
    395 				perror("  DUMP: error reading command pipe in master");
    396 				dumpabort(0);
    397 			}
    398 			slaves[f].sent = 0;
    399 			if (got != writesize) {
    400 				msg("EOT detected in last 2 tape records!\n");
    401 				msg("Use a longer tape, decrease the size estimate\n");
    402 				quit("or use no size estimate at all.\n");
    403 			}
    404 		}
    405 		(void) close(slaves[f].fd);
    406 	}
    407 	while (wait((int *)NULL) >= 0)	/* wait for any signals from slaves */
    408 		/* void */;
    409 
    410 	if (pipeout)
    411 		return;
    412 
    413 	msg("Closing %s\n", tape);
    414 
    415 #ifdef RDUMP
    416 	if (host) {
    417 		rmtclose();
    418 		while (rmtopen(tape, 0) < 0)
    419 			sleep(10);
    420 		rmtclose();
    421 		return;
    422 	}
    423 #endif
    424 	(void) close(tapefd);
    425 	while ((f = open(tape, 0)) < 0)
    426 		sleep (10);
    427 	(void) close(f);
    428 }
    429 
    430 void
    431 close_rewind()
    432 {
    433 	trewind();
    434 	(void)do_stats();
    435 	if (nexttape)
    436 		return;
    437 	if (!nogripe) {
    438 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
    439 		broadcast("CHANGE DUMP VOLUMES!\7\7\n");
    440 	}
    441 	while (!query("Is the new volume mounted and ready to go?"))
    442 		if (query("Do you want to abort?")) {
    443 			dumpabort(0);
    444 			/*NOTREACHED*/
    445 		}
    446 }
    447 
    448 void
    449 rollforward()
    450 {
    451 	struct req *p, *q, *prev;
    452 	struct slave *tslp;
    453 	int i, size, savedtapea, got;
    454 	union u_spcl *ntb, *otb;
    455 	tslp = &slaves[SLAVES];
    456 	ntb = (union u_spcl *)tslp->tblock[1];
    457 
    458 	/*
    459 	 * Each of the N slaves should have requests that need to
    460 	 * be replayed on the next tape.  Use the extra slave buffers
    461 	 * (slaves[SLAVES]) to construct request lists to be sent to
    462 	 * each slave in turn.
    463 	 */
    464 	for (i = 0; i < SLAVES; i++) {
    465 		q = &tslp->req[1];
    466 		otb = (union u_spcl *)slp->tblock;
    467 
    468 		/*
    469 		 * For each request in the current slave, copy it to tslp.
    470 		 */
    471 
    472 		prev = NULL;
    473 		for (p = slp->req; p->count > 0; p += p->count) {
    474 			*q = *p;
    475 			if (p->dblk == 0)
    476 				*ntb++ = *otb++; /* copy the datablock also */
    477 			prev = q;
    478 			q += q->count;
    479 		}
    480 		if (prev == NULL)
    481 			quit("rollforward: protocol botch");
    482 		if (prev->dblk != 0)
    483 			prev->count -= 1;
    484 		else
    485 			ntb--;
    486 		q -= 1;
    487 		q->count = 0;
    488 		q = &tslp->req[0];
    489 		if (i == 0) {
    490 			q->dblk = 0;
    491 			q->count = 1;
    492 			trecno = 0;
    493 			nextblock = tslp->tblock;
    494 			savedtapea = spcl.c_tapea;
    495 			spcl.c_tapea = slp->tapea;
    496 			startnewtape(0);
    497 			spcl.c_tapea = savedtapea;
    498 			lastspclrec = savedtapea - 1;
    499 		}
    500 		size = (char *)ntb - (char *)q;
    501 		if (atomic_write(slp->fd, (char *)q, size) != size) {
    502 			perror("  DUMP: error writing command pipe");
    503 			dumpabort(0);
    504 		}
    505 		slp->sent = 1;
    506 		if (++slp >= &slaves[SLAVES])
    507 			slp = &slaves[0];
    508 
    509 		q->count = 1;
    510 
    511 		if (prev->dblk != 0) {
    512 			/*
    513 			 * If the last one was a disk block, make the
    514 			 * first of this one be the last bit of that disk
    515 			 * block...
    516 			 */
    517 			q->dblk = prev->dblk +
    518 				prev->count * (TP_BSIZE / DEV_BSIZE);
    519 			ntb = (union u_spcl *)tslp->tblock;
    520 		} else {
    521 			/*
    522 			 * It wasn't a disk block.  Copy the data to its
    523 			 * new location in the buffer.
    524 			 */
    525 			q->dblk = 0;
    526 			*((union u_spcl *)tslp->tblock) = *ntb;
    527 			ntb = (union u_spcl *)tslp->tblock[1];
    528 		}
    529 	}
    530 	slp->req[0] = *q;
    531 	nextblock = slp->tblock;
    532 	if (q->dblk == 0)
    533 		nextblock++;
    534 	trecno = 1;
    535 
    536 	/*
    537 	 * Clear the first slaves' response.  One hopes that it
    538 	 * worked ok, otherwise the tape is much too short!
    539 	 */
    540 	if (slp->sent) {
    541 		if (atomic_read(slp->fd, (char *)&got, sizeof got)
    542 		    != sizeof got) {
    543 			perror("  DUMP: error reading command pipe in master");
    544 			dumpabort(0);
    545 		}
    546 		slp->sent = 0;
    547 
    548 		if (got != writesize) {
    549 			quit("EOT detected at start of the tape!\n");
    550 		}
    551 	}
    552 }
    553 
    554 /*
    555  * We implement taking and restoring checkpoints on the tape level.
    556  * When each tape is opened, a new process is created by forking; this
    557  * saves all of the necessary context in the parent.  The child
    558  * continues the dump; the parent waits around, saving the context.
    559  * If the child returns X_REWRITE, then it had problems writing that tape;
    560  * this causes the parent to fork again, duplicating the context, and
    561  * everything continues as if nothing had happened.
    562  */
    563 void
    564 startnewtape(top)
    565 	int top;
    566 {
    567 	int	parentpid;
    568 	int	childpid;
    569 	int	status;
    570 	int	waitpid;
    571 	char	*p;
    572 #ifdef sunos
    573 	void	(*interrupt_save)();
    574 #else
    575 	sig_t	interrupt_save;
    576 #endif
    577 
    578 	interrupt_save = signal(SIGINT, SIG_IGN);
    579 	parentpid = getpid();
    580 	tapea_volume = spcl.c_tapea;
    581 	(void)time(&tstart_volume);
    582 
    583 restore_check_point:
    584 	(void)signal(SIGINT, interrupt_save);
    585 	/*
    586 	 *	All signals are inherited...
    587 	 */
    588 	childpid = fork();
    589 	if (childpid < 0) {
    590 		msg("Context save fork fails in parent %d\n", parentpid);
    591 		Exit(X_ABORT);
    592 	}
    593 	if (childpid != 0) {
    594 		/*
    595 		 *	PARENT:
    596 		 *	save the context by waiting
    597 		 *	until the child doing all of the work returns.
    598 		 *	don't catch the interrupt
    599 		 */
    600 		signal(SIGINT, SIG_IGN);
    601 #ifdef TDEBUG
    602 		msg("Tape: %d; parent process: %d child process %d\n",
    603 			tapeno+1, parentpid, childpid);
    604 #endif /* TDEBUG */
    605 		while ((waitpid = wait(&status)) != childpid)
    606 			msg("Parent %d waiting for child %d has another child %d return\n",
    607 				parentpid, childpid, waitpid);
    608 		if (status & 0xFF) {
    609 			msg("Child %d returns LOB status %o\n",
    610 				childpid, status&0xFF);
    611 		}
    612 		status = (status >> 8) & 0xFF;
    613 #ifdef TDEBUG
    614 		switch(status) {
    615 			case X_FINOK:
    616 				msg("Child %d finishes X_FINOK\n", childpid);
    617 				break;
    618 			case X_ABORT:
    619 				msg("Child %d finishes X_ABORT\n", childpid);
    620 				break;
    621 			case X_REWRITE:
    622 				msg("Child %d finishes X_REWRITE\n", childpid);
    623 				break;
    624 			default:
    625 				msg("Child %d finishes unknown %d\n",
    626 					childpid, status);
    627 				break;
    628 		}
    629 #endif /* TDEBUG */
    630 		switch(status) {
    631 			case X_FINOK:
    632 				Exit(X_FINOK);
    633 			case X_ABORT:
    634 				Exit(X_ABORT);
    635 			case X_REWRITE:
    636 				goto restore_check_point;
    637 			default:
    638 				msg("Bad return code from dump: %d\n", status);
    639 				Exit(X_ABORT);
    640 		}
    641 		/*NOTREACHED*/
    642 	} else {	/* we are the child; just continue */
    643 #ifdef TDEBUG
    644 		sleep(4);	/* allow time for parent's message to get out */
    645 		msg("Child on Tape %d has parent %d, my pid = %d\n",
    646 			tapeno+1, parentpid, getpid());
    647 #endif /* TDEBUG */
    648 		/*
    649 		 * If we have a name like "/dev/rst0,/dev/rst1",
    650 		 * use the name before the comma first, and save
    651 		 * the remaining names for subsequent volumes.
    652 		 */
    653 		tapeno++;               /* current tape sequence */
    654 		if (nexttape || strchr(tape, ',')) {
    655 			if (nexttape && *nexttape)
    656 				tape = nexttape;
    657 			if ((p = strchr(tape, ',')) != NULL) {
    658 				*p = '\0';
    659 				nexttape = p + 1;
    660 			} else
    661 				nexttape = NULL;
    662 			msg("Dumping volume %d on %s\n", tapeno, tape);
    663 		}
    664 #ifdef RDUMP
    665 		while ((tapefd = (host ? rmtopen(tape, 2) :
    666 			pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    667 #else
    668 		while ((tapefd = (pipeout ? 1 :
    669 				  open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    670 #endif
    671 		    {
    672 			msg("Cannot open output \"%s\".\n", tape);
    673 			if (!query("Do you want to retry the open?"))
    674 				dumpabort(0);
    675 		}
    676 
    677 		enslave();  /* Share open tape file descriptor with slaves */
    678 
    679 		asize = 0;
    680 		blocksthisvol = 0;
    681 		if (top)
    682 			newtape++;		/* new tape signal */
    683 		spcl.c_count = slp->count;
    684 		/*
    685 		 * measure firstrec in TP_BSIZE units since restore doesn't
    686 		 * know the correct ntrec value...
    687 		 */
    688 		spcl.c_firstrec = slp->firstrec;
    689 		spcl.c_volume++;
    690 		spcl.c_type = TS_TAPE;
    691 		spcl.c_flags |= DR_NEWHEADER;
    692 		writeheader((ino_t)slp->inode);
    693 		spcl.c_flags &=~ DR_NEWHEADER;
    694 		msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
    695 		if (tapeno > 1)
    696 			msg("Volume %d begins with blocks from inode %d\n",
    697 				tapeno, slp->inode);
    698 	}
    699 }
    700 
    701 void
    702 dumpabort(signo)
    703 	int signo;
    704 {
    705 
    706 	if (master != 0 && master != getpid())
    707 		/* Signals master to call dumpabort */
    708 		(void) kill(master, SIGTERM);
    709 	else {
    710 		killall();
    711 		msg("The ENTIRE dump is aborted.\n");
    712 	}
    713 #ifdef RDUMP
    714 	rmtclose();
    715 #endif
    716 	Exit(X_ABORT);
    717 }
    718 
    719 void
    720 Exit(status)
    721 	int status;
    722 {
    723 
    724 #ifdef TDEBUG
    725 	msg("pid = %d exits with status %d\n", getpid(), status);
    726 #endif /* TDEBUG */
    727 	exit(status);
    728 }
    729 
    730 /*
    731  * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
    732  */
    733 static void
    734 proceed(signo)
    735 	int signo;
    736 {
    737 
    738 	if (ready)
    739 		longjmp(jmpbuf, 1);
    740 	caught++;
    741 }
    742 
    743 void
    744 enslave()
    745 {
    746 	int cmd[2];
    747 	int i, j;
    748 
    749 	master = getpid();
    750 
    751 	signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
    752 	signal(SIGPIPE, sigpipe);
    753 	signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
    754 	signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
    755 
    756 	for (i = 0; i < SLAVES; i++) {
    757 		if (i == slp - &slaves[0]) {
    758 			caught = 1;
    759 		} else {
    760 			caught = 0;
    761 		}
    762 
    763 		if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
    764 		    (slaves[i].pid = fork()) < 0)
    765 			quit("too many slaves, %d (recompile smaller): %s\n",
    766 			    i, strerror(errno));
    767 
    768 		slaves[i].fd = cmd[1];
    769 		slaves[i].sent = 0;
    770 		if (slaves[i].pid == 0) { 	    /* Slave starts up here */
    771 			for (j = 0; j <= i; j++)
    772 			        (void) close(slaves[j].fd);
    773 			signal(SIGINT, SIG_IGN);    /* Master handles this */
    774 			signal(SIGINFO, SIG_IGN);
    775 			doslave(cmd[0], i);
    776 			Exit(X_FINOK);
    777 		}
    778 	}
    779 
    780 	for (i = 0; i < SLAVES; i++)
    781 		(void) atomic_write(slaves[i].fd,
    782 			      (char *) &slaves[(i + 1) % SLAVES].pid,
    783 		              sizeof slaves[0].pid);
    784 
    785 	master = 0;
    786 }
    787 
    788 void
    789 killall()
    790 {
    791 	int i;
    792 
    793 	for (i = 0; i < SLAVES; i++)
    794 		if (slaves[i].pid > 0)
    795 			(void) kill(slaves[i].pid, SIGKILL);
    796 }
    797 
    798 /*
    799  * Synchronization - each process has a lockfile, and shares file
    800  * descriptors to the following process's lockfile.  When our write
    801  * completes, we release our lock on the following process's lock-
    802  * file, allowing the following process to lock it and proceed. We
    803  * get the lock back for the next cycle by swapping descriptors.
    804  */
    805 static void
    806 doslave(cmd, slave_number)
    807 	int cmd;
    808         int slave_number;
    809 {
    810 	int nread;
    811 	int nextslave, size, wrote, eot_count;
    812 	sigset_t sigset;
    813 
    814 	/*
    815 	 * Need our own seek pointer.
    816 	 */
    817 	(void) close(diskfd);
    818 	if ((diskfd = open(disk, O_RDONLY)) < 0)
    819 		quit("slave couldn't reopen disk: %s\n", strerror(errno));
    820 
    821 	/*
    822 	 * Need the pid of the next slave in the loop...
    823 	 */
    824 	if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
    825 	    != sizeof nextslave) {
    826 		quit("master/slave protocol botched - didn't get pid of next slave.\n");
    827 	}
    828 
    829 	/*
    830 	 * Get list of blocks to dump, read the blocks into tape buffer
    831 	 */
    832 	while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
    833 		struct req *p = slp->req;
    834 
    835 		for (trecno = 0; trecno < ntrec;
    836 		     trecno += p->count, p += p->count) {
    837 			if (p->dblk) {
    838 				bread(p->dblk, slp->tblock[trecno],
    839 					p->count * TP_BSIZE);
    840 			} else {
    841 				if (p->count != 1 || atomic_read(cmd,
    842 				    (char *)slp->tblock[trecno],
    843 				    TP_BSIZE) != TP_BSIZE)
    844 				       quit("master/slave protocol botched.\n");
    845 			}
    846 		}
    847 		if (setjmp(jmpbuf) == 0) {
    848 			ready = 1;
    849 			if (!caught)
    850 				(void) pause();
    851 		}
    852 		ready = 0;
    853 		caught = 0;
    854 
    855 		/* Try to write the data... */
    856 		eot_count = 0;
    857 		size = 0;
    858 
    859 		while (eot_count < 10 && size < writesize) {
    860 #ifdef RDUMP
    861 			if (host)
    862 				wrote = rmtwrite(slp->tblock[0]+size,
    863 				    writesize-size);
    864 			else
    865 #endif
    866 				wrote = write(tapefd, slp->tblock[0]+size,
    867 				    writesize-size);
    868 #ifdef WRITEDEBUG
    869 			printf("slave %d wrote %d\n", slave_number, wrote);
    870 #endif
    871 			if (wrote < 0)
    872 				break;
    873 			if (wrote == 0)
    874 				eot_count++;
    875 			size += wrote;
    876 		}
    877 
    878 #ifdef WRITEDEBUG
    879 		if (size != writesize)
    880 		 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
    881 		     slave_number, size, writesize);
    882 #endif
    883 
    884 		if (eot_count > 0)
    885 			size = 0;
    886 
    887 		/*
    888 		 * fixme: Pyramids running OSx return ENOSPC
    889 		 * at EOT on 1/2 inch drives.
    890 		 */
    891 		if (size < 0) {
    892 			(void) kill(master, SIGUSR1);
    893 			sigemptyset(&sigset);
    894 			for (;;)
    895 				sigsuspend(&sigset);
    896 		} else {
    897 			/*
    898 			 * pass size of write back to master
    899 			 * (for EOT handling)
    900 			 */
    901 			(void) atomic_write(cmd, (char *)&size, sizeof size);
    902 		}
    903 
    904 		/*
    905 		 * If partial write, don't want next slave to go.
    906 		 * Also jolts him awake.
    907 		 */
    908 		(void) kill(nextslave, SIGUSR2);
    909 	}
    910 	if (nread != 0)
    911 		quit("error reading command pipe: %s\n", strerror(errno));
    912 }
    913 
    914 /*
    915  * Since a read from a pipe may not return all we asked for,
    916  * loop until the count is satisfied (or error).
    917  */
    918 static ssize_t
    919 atomic_read(fd, buf, count)
    920 	int fd;
    921 	char *buf;
    922 	int count;
    923 {
    924 	ssize_t got, need = count;
    925 
    926 	while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
    927 		buf += got;
    928 	return (got < 0 ? got : count - need);
    929 }
    930 
    931 /*
    932  * Since a write may not write all we ask if we get a signal,
    933  * loop until the count is satisfied (or error).
    934  */
    935 static ssize_t
    936 atomic_write(fd, buf, count)
    937 	int fd;
    938 	char *buf;
    939 	int count;
    940 {
    941 	ssize_t got, need = count;
    942 
    943 	while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
    944 		buf += got;
    945 	return (got < 0 ? got : count - need);
    946 }
    947