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