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