Home | History | Annotate | Line # | Download | only in dump
tape.c revision 1.24
      1 /*	$NetBSD: tape.c,v 1.24 2001/05/07 21:17:48 tron 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.24 2001/05/07 21:17:48 tron 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()
    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)
    421 			sleep(10);
    422 		rmtclose();
    423 		return;
    424 	}
    425 #endif
    426 	(void) close(tapefd);
    427 	while ((f = open(tape, 0)) < 0)
    428 		sleep (10);
    429 	if (eflag) {
    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 	trewind();
    444 	(void)do_stats();
    445 	if (nexttape)
    446 		return;
    447 	if (!nogripe) {
    448 		msg("Change Volumes: Mount volume #%d\n", tapeno+1);
    449 		broadcast("CHANGE DUMP VOLUMES!\7\7\n");
    450 	}
    451 	while (!query("Is the new volume mounted and ready to go?"))
    452 		if (query("Do you want to abort?")) {
    453 			dumpabort(0);
    454 			/*NOTREACHED*/
    455 		}
    456 }
    457 
    458 void
    459 rollforward()
    460 {
    461 	struct req *p, *q, *prev;
    462 	struct slave *tslp;
    463 	int i, size, savedtapea, got;
    464 	union u_spcl *ntb, *otb;
    465 	tslp = &slaves[SLAVES];
    466 	ntb = (union u_spcl *)tslp->tblock[1];
    467 
    468 	/*
    469 	 * Each of the N slaves should have requests that need to
    470 	 * be replayed on the next tape.  Use the extra slave buffers
    471 	 * (slaves[SLAVES]) to construct request lists to be sent to
    472 	 * each slave in turn.
    473 	 */
    474 	for (i = 0; i < SLAVES; i++) {
    475 		q = &tslp->req[1];
    476 		otb = (union u_spcl *)slp->tblock;
    477 
    478 		/*
    479 		 * For each request in the current slave, copy it to tslp.
    480 		 */
    481 
    482 		prev = NULL;
    483 		for (p = slp->req; p->count > 0; p += p->count) {
    484 			*q = *p;
    485 			if (p->dblk == 0)
    486 				*ntb++ = *otb++; /* copy the datablock also */
    487 			prev = q;
    488 			q += q->count;
    489 		}
    490 		if (prev == NULL)
    491 			quit("rollforward: protocol botch");
    492 		if (prev->dblk != 0)
    493 			prev->count -= 1;
    494 		else
    495 			ntb--;
    496 		q -= 1;
    497 		q->count = 0;
    498 		q = &tslp->req[0];
    499 		if (i == 0) {
    500 			q->dblk = 0;
    501 			q->count = 1;
    502 			trecno = 0;
    503 			nextblock = tslp->tblock;
    504 			savedtapea = iswap32(spcl.c_tapea);
    505 			spcl.c_tapea = iswap32(slp->tapea);
    506 			startnewtape(0);
    507 			spcl.c_tapea = iswap32(savedtapea);
    508 			lastspclrec = savedtapea - 1;
    509 		}
    510 		size = (char *)ntb - (char *)q;
    511 		if (atomic_write(slp->fd, (char *)q, size) != size) {
    512 			perror("  DUMP: error writing command pipe");
    513 			dumpabort(0);
    514 		}
    515 		slp->sent = 1;
    516 		if (++slp >= &slaves[SLAVES])
    517 			slp = &slaves[0];
    518 
    519 		q->count = 1;
    520 
    521 		if (prev->dblk != 0) {
    522 			/*
    523 			 * If the last one was a disk block, make the
    524 			 * first of this one be the last bit of that disk
    525 			 * block...
    526 			 */
    527 			q->dblk = prev->dblk +
    528 				prev->count * (TP_BSIZE / DEV_BSIZE);
    529 			ntb = (union u_spcl *)tslp->tblock;
    530 		} else {
    531 			/*
    532 			 * It wasn't a disk block.  Copy the data to its
    533 			 * new location in the buffer.
    534 			 */
    535 			q->dblk = 0;
    536 			*((union u_spcl *)tslp->tblock) = *ntb;
    537 			ntb = (union u_spcl *)tslp->tblock[1];
    538 		}
    539 	}
    540 	slp->req[0] = *q;
    541 	nextblock = slp->tblock;
    542 	if (q->dblk == 0)
    543 		nextblock++;
    544 	trecno = 1;
    545 
    546 	/*
    547 	 * Clear the first slaves' response.  One hopes that it
    548 	 * worked ok, otherwise the tape is much too short!
    549 	 */
    550 	if (slp->sent) {
    551 		if (atomic_read(slp->fd, (char *)&got, sizeof got)
    552 		    != sizeof got) {
    553 			perror("  DUMP: error reading command pipe in master");
    554 			dumpabort(0);
    555 		}
    556 		slp->sent = 0;
    557 
    558 		if (got != writesize) {
    559 			quit("EOT detected at start of the tape!\n");
    560 		}
    561 	}
    562 }
    563 
    564 /*
    565  * We implement taking and restoring checkpoints on the tape level.
    566  * When each tape is opened, a new process is created by forking; this
    567  * saves all of the necessary context in the parent.  The child
    568  * continues the dump; the parent waits around, saving the context.
    569  * If the child returns X_REWRITE, then it had problems writing that tape;
    570  * this causes the parent to fork again, duplicating the context, and
    571  * everything continues as if nothing had happened.
    572  */
    573 void
    574 startnewtape(top)
    575 	int top;
    576 {
    577 	int	parentpid;
    578 	int	childpid;
    579 	int	status;
    580 	int	waitpid;
    581 	char	*p;
    582 #ifdef sunos
    583 	void	(*interrupt_save)();
    584 #else
    585 	sig_t	interrupt_save;
    586 #endif
    587 
    588 	interrupt_save = signal(SIGINT, SIG_IGN);
    589 	parentpid = getpid();
    590 	tapea_volume = iswap32(spcl.c_tapea);
    591 	(void)time(&tstart_volume);
    592 
    593 restore_check_point:
    594 	(void)signal(SIGINT, interrupt_save);
    595 	/*
    596 	 *	All signals are inherited...
    597 	 */
    598 	childpid = fork();
    599 	if (childpid < 0) {
    600 		msg("Context save fork fails in parent %d\n", parentpid);
    601 		Exit(X_ABORT);
    602 	}
    603 	if (childpid != 0) {
    604 		/*
    605 		 *	PARENT:
    606 		 *	save the context by waiting
    607 		 *	until the child doing all of the work returns.
    608 		 *	don't catch the interrupt
    609 		 */
    610 		signal(SIGINT, SIG_IGN);
    611 #ifdef TDEBUG
    612 		msg("Tape: %d; parent process: %d child process %d\n",
    613 			tapeno+1, parentpid, childpid);
    614 #endif /* TDEBUG */
    615 		while ((waitpid = wait(&status)) != childpid)
    616 			msg("Parent %d waiting for child %d has another child %d return\n",
    617 				parentpid, childpid, waitpid);
    618 		if (status & 0xFF) {
    619 			msg("Child %d returns LOB status %o\n",
    620 				childpid, status&0xFF);
    621 		}
    622 		status = (status >> 8) & 0xFF;
    623 #ifdef TDEBUG
    624 		switch(status) {
    625 			case X_FINOK:
    626 				msg("Child %d finishes X_FINOK\n", childpid);
    627 				break;
    628 			case X_ABORT:
    629 				msg("Child %d finishes X_ABORT\n", childpid);
    630 				break;
    631 			case X_REWRITE:
    632 				msg("Child %d finishes X_REWRITE\n", childpid);
    633 				break;
    634 			default:
    635 				msg("Child %d finishes unknown %d\n",
    636 					childpid, status);
    637 				break;
    638 		}
    639 #endif /* TDEBUG */
    640 		switch(status) {
    641 			case X_FINOK:
    642 				Exit(X_FINOK);
    643 			case X_ABORT:
    644 				Exit(X_ABORT);
    645 			case X_REWRITE:
    646 				goto restore_check_point;
    647 			default:
    648 				msg("Bad return code from dump: %d\n", status);
    649 				Exit(X_ABORT);
    650 		}
    651 		/*NOTREACHED*/
    652 	} else {	/* we are the child; just continue */
    653 #ifdef TDEBUG
    654 		sleep(4);	/* allow time for parent's message to get out */
    655 		msg("Child on Tape %d has parent %d, my pid = %d\n",
    656 			tapeno+1, parentpid, getpid());
    657 #endif /* TDEBUG */
    658 		/*
    659 		 * If we have a name like "/dev/rst0,/dev/rst1",
    660 		 * use the name before the comma first, and save
    661 		 * the remaining names for subsequent volumes.
    662 		 */
    663 		tapeno++;               /* current tape sequence */
    664 		if (nexttape || strchr(tape, ',')) {
    665 			if (nexttape && *nexttape)
    666 				tape = nexttape;
    667 			if ((p = strchr(tape, ',')) != NULL) {
    668 				*p = '\0';
    669 				nexttape = p + 1;
    670 			} else
    671 				nexttape = NULL;
    672 			msg("Dumping volume %d on %s\n", tapeno, tape);
    673 		}
    674 #ifdef RDUMP
    675 		while ((tapefd = (host ? rmtopen(tape, 2) :
    676 			pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    677 #else
    678 		while ((tapefd = (pipeout ? 1 :
    679 				  open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
    680 #endif
    681 		    {
    682 			msg("Cannot open output \"%s\".\n", tape);
    683 			if (!query("Do you want to retry the open?"))
    684 				dumpabort(0);
    685 		}
    686 
    687 		enslave();  /* Share open tape file descriptor with slaves */
    688 
    689 		asize = 0;
    690 		blocksthisvol = 0;
    691 		if (top)
    692 			newtape++;		/* new tape signal */
    693 		spcl.c_count = iswap32(slp->count);
    694 		/*
    695 		 * measure firstrec in TP_BSIZE units since restore doesn't
    696 		 * know the correct ntrec value...
    697 		 */
    698 		spcl.c_firstrec = iswap32(slp->firstrec);
    699 		spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
    700 		spcl.c_type = iswap32(TS_TAPE);
    701 		spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWHEADER);
    702 		writeheader((ino_t)slp->inode);
    703 		spcl.c_flags  = iswap32(iswap32(spcl.c_flags) & ~ DR_NEWHEADER);
    704 		msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
    705 		if (tapeno > 1)
    706 			msg("Volume %d begins with blocks from inode %d\n",
    707 				tapeno, slp->inode);
    708 	}
    709 }
    710 
    711 void
    712 dumpabort(signo)
    713 	int signo;
    714 {
    715 
    716 	if (master != 0 && master != getpid())
    717 		/* Signals master to call dumpabort */
    718 		(void) kill(master, SIGTERM);
    719 	else {
    720 		killall();
    721 		msg("The ENTIRE dump is aborted.\n");
    722 	}
    723 #ifdef RDUMP
    724 	rmtclose();
    725 #endif
    726 	Exit(X_ABORT);
    727 }
    728 
    729 void
    730 Exit(status)
    731 	int status;
    732 {
    733 
    734 #ifdef TDEBUG
    735 	msg("pid = %d exits with status %d\n", getpid(), status);
    736 #endif /* TDEBUG */
    737 	exit(status);
    738 }
    739 
    740 /*
    741  * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
    742  */
    743 static void
    744 proceed(signo)
    745 	int signo;
    746 {
    747 
    748 	if (ready)
    749 		longjmp(jmpbuf, 1);
    750 	caught++;
    751 }
    752 
    753 void
    754 enslave()
    755 {
    756 	int cmd[2];
    757 	int i, j;
    758 
    759 	master = getpid();
    760 
    761 	signal(SIGTERM, dumpabort);  /* Slave sends SIGTERM on dumpabort() */
    762 	signal(SIGPIPE, sigpipe);
    763 	signal(SIGUSR1, tperror);    /* Slave sends SIGUSR1 on tape errors */
    764 	signal(SIGUSR2, proceed);    /* Slave sends SIGUSR2 to next slave */
    765 
    766 	for (i = 0; i < SLAVES; i++) {
    767 		if (i == slp - &slaves[0]) {
    768 			caught = 1;
    769 		} else {
    770 			caught = 0;
    771 		}
    772 
    773 		if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
    774 		    (slaves[i].pid = fork()) < 0)
    775 			quit("too many slaves, %d (recompile smaller): %s\n",
    776 			    i, strerror(errno));
    777 
    778 		slaves[i].fd = cmd[1];
    779 		slaves[i].sent = 0;
    780 		if (slaves[i].pid == 0) { 	    /* Slave starts up here */
    781 			for (j = 0; j <= i; j++)
    782 			        (void) close(slaves[j].fd);
    783 			signal(SIGINT, SIG_IGN);    /* Master handles this */
    784 			signal(SIGINFO, SIG_IGN);
    785 			doslave(cmd[0], i);
    786 			Exit(X_FINOK);
    787 		}
    788 	}
    789 
    790 	for (i = 0; i < SLAVES; i++)
    791 		(void) atomic_write(slaves[i].fd,
    792 			      (char *) &slaves[(i + 1) % SLAVES].pid,
    793 		              sizeof slaves[0].pid);
    794 
    795 	master = 0;
    796 }
    797 
    798 void
    799 killall()
    800 {
    801 	int i;
    802 
    803 	for (i = 0; i < SLAVES; i++)
    804 		if (slaves[i].pid > 0)
    805 			(void) kill(slaves[i].pid, SIGKILL);
    806 }
    807 
    808 /*
    809  * Synchronization - each process has a lockfile, and shares file
    810  * descriptors to the following process's lockfile.  When our write
    811  * completes, we release our lock on the following process's lock-
    812  * file, allowing the following process to lock it and proceed. We
    813  * get the lock back for the next cycle by swapping descriptors.
    814  */
    815 static void
    816 doslave(cmd, slave_number)
    817 	int cmd;
    818         int slave_number;
    819 {
    820 	int nread;
    821 	int nextslave, size, wrote, eot_count;
    822 	sigset_t sigset;
    823 
    824 	/*
    825 	 * Need our own seek pointer.
    826 	 */
    827 	(void) close(diskfd);
    828 	if ((diskfd = open(disk, O_RDONLY)) < 0)
    829 		quit("slave couldn't reopen disk: %s\n", strerror(errno));
    830 
    831 	/*
    832 	 * Need the pid of the next slave in the loop...
    833 	 */
    834 	if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
    835 	    != sizeof nextslave) {
    836 		quit("master/slave protocol botched - didn't get pid of next slave.\n");
    837 	}
    838 
    839 	/*
    840 	 * Get list of blocks to dump, read the blocks into tape buffer
    841 	 */
    842 	while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
    843 		struct req *p = slp->req;
    844 
    845 		for (trecno = 0; trecno < ntrec;
    846 		     trecno += p->count, p += p->count) {
    847 			if (p->dblk) {
    848 				bread(p->dblk, slp->tblock[trecno],
    849 					p->count * TP_BSIZE);
    850 			} else {
    851 				if (p->count != 1 || atomic_read(cmd,
    852 				    (char *)slp->tblock[trecno],
    853 				    TP_BSIZE) != TP_BSIZE)
    854 				       quit("master/slave protocol botched.\n");
    855 			}
    856 		}
    857 		if (setjmp(jmpbuf) == 0) {
    858 			ready = 1;
    859 			if (!caught)
    860 				(void) pause();
    861 		}
    862 		ready = 0;
    863 		caught = 0;
    864 
    865 		/* Try to write the data... */
    866 		eot_count = 0;
    867 		size = 0;
    868 
    869 		while (eot_count < 10 && size < writesize) {
    870 #ifdef RDUMP
    871 			if (host)
    872 				wrote = rmtwrite(slp->tblock[0]+size,
    873 				    writesize-size);
    874 			else
    875 #endif
    876 				wrote = write(tapefd, slp->tblock[0]+size,
    877 				    writesize-size);
    878 #ifdef WRITEDEBUG
    879 			fprintf(stderr, "slave %d wrote %d\n", slave_number, wrote);
    880 #endif
    881 			if (wrote < 0)
    882 				break;
    883 			if (wrote == 0)
    884 				eot_count++;
    885 			size += wrote;
    886 		}
    887 
    888 #ifdef WRITEDEBUG
    889 		if (size != writesize)
    890 		 fprintf(stderr, "slave %d only wrote %d out of %d bytes and gave up.\n",
    891 		     slave_number, size, writesize);
    892 #endif
    893 
    894 		if (eot_count > 0)
    895 			size = 0;
    896 
    897 		/*
    898 		 * fixme: Pyramids running OSx return ENOSPC
    899 		 * at EOT on 1/2 inch drives.
    900 		 */
    901 		if (size < 0) {
    902 			(void) kill(master, SIGUSR1);
    903 			sigemptyset(&sigset);
    904 			for (;;)
    905 				sigsuspend(&sigset);
    906 		} else {
    907 			/*
    908 			 * pass size of write back to master
    909 			 * (for EOT handling)
    910 			 */
    911 			(void) atomic_write(cmd, (char *)&size, sizeof size);
    912 		}
    913 
    914 		/*
    915 		 * If partial write, don't want next slave to go.
    916 		 * Also jolts him awake.
    917 		 */
    918 		(void) kill(nextslave, SIGUSR2);
    919 	}
    920 	printcachestats();
    921 	if (nread != 0)
    922 		quit("error reading command pipe: %s\n", strerror(errno));
    923 }
    924 
    925 /*
    926  * Since a read from a pipe may not return all we asked for,
    927  * loop until the count is satisfied (or error).
    928  */
    929 static ssize_t
    930 atomic_read(fd, buf, count)
    931 	int fd;
    932 	char *buf;
    933 	int count;
    934 {
    935 	ssize_t got, need = count;
    936 
    937 	while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
    938 		buf += got;
    939 	return (got < 0 ? got : count - need);
    940 }
    941 
    942 /*
    943  * Since a write may not write all we ask if we get a signal,
    944  * loop until the count is satisfied (or error).
    945  */
    946 static ssize_t
    947 atomic_write(fd, buf, count)
    948 	int fd;
    949 	char *buf;
    950 	int count;
    951 {
    952 	ssize_t got, need = count;
    953 
    954 	while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
    955 		buf += got;
    956 	return (got < 0 ? got : count - need);
    957 }
    958