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