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