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