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