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