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