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