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