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