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