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