tape.c revision 1.23 1 /* $NetBSD: tape.c,v 1.23 2000/10/11 20:25:29 he 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.23 2000/10/11 20:25:29 he 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 %ld 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 (int) (ttaken / 3600), (int) ((ttaken % 3600) / 60),
267 (int) (ttaken % 60));
268 msg("Volume %d transfer rate: %d KB/s\n", tapeno,
269 (int) (blocks / ttaken));
270 xferrate += blocks / ttaken;
271 }
272 return(tnow);
273 }
274
275 /*
276 * statussig --
277 * information message upon receipt of SIGINFO
278 * (derived from optr.c::timeest())
279 */
280 void
281 statussig(notused)
282 int notused;
283 {
284 time_t tnow, deltat;
285 char msgbuf[128];
286
287 if (blockswritten < 500)
288 return;
289 (void) time((time_t *) &tnow);
290 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
291 / blockswritten * tapesize;
292 (void)snprintf(msgbuf, sizeof(msgbuf),
293 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
294 (blockswritten * 100.0) / tapesize,
295 (long)((iswap32(spcl.c_tapea) - tapea_volume) / (tnow - tstart_volume)),
296 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
297 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
298 }
299
300 static void
301 flushtape()
302 {
303 int i, blks, got;
304 long lastfirstrec;
305
306 int siz = (char *)nextblock - (char *)slp->req;
307
308 slp->req[trecno].count = 0; /* Sentinel */
309
310 if (atomic_write(slp->fd, (char *)slp->req, siz) != siz)
311 quit("error writing command pipe: %s\n", strerror(errno));
312 slp->sent = 1; /* we sent a request, read the response later */
313
314 lastfirstrec = slp->firstrec;
315
316 if (++slp >= &slaves[SLAVES])
317 slp = &slaves[0];
318
319 /* Read results back from next slave */
320 if (slp->sent) {
321 if (atomic_read(slp->fd, (char *)&got, sizeof got)
322 != sizeof got) {
323 perror(" DUMP: error reading command pipe in master");
324 dumpabort(0);
325 }
326 slp->sent = 0;
327
328 /* Check for end of tape */
329 if (got < writesize) {
330 msg("End of tape detected\n");
331
332 /*
333 * Drain the results, don't care what the values were.
334 * If we read them here then trewind won't...
335 */
336 for (i = 0; i < SLAVES; i++) {
337 if (slaves[i].sent) {
338 if (atomic_read(slaves[i].fd,
339 (char *)&got, sizeof got)
340 != sizeof got) {
341 perror(" DUMP: error reading command pipe in master");
342 dumpabort(0);
343 }
344 slaves[i].sent = 0;
345 }
346 }
347
348 close_rewind();
349 rollforward();
350 return;
351 }
352 }
353
354 blks = 0;
355 if (iswap32(spcl.c_type) != TS_END) {
356 for (i = 0; i < iswap32(spcl.c_count); i++)
357 if (spcl.c_addr[i] != 0)
358 blks++;
359 }
360 slp->count = lastspclrec + blks + 1 - iswap32(spcl.c_tapea);
361 slp->tapea = iswap32(spcl.c_tapea);
362 slp->firstrec = lastfirstrec + ntrec;
363 slp->inode = curino;
364 nextblock = slp->tblock;
365 trecno = 0;
366 asize += tenths;
367 blockswritten += ntrec;
368 blocksthisvol += ntrec;
369 if (!pipeout && (blocksperfile ?
370 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
371 close_rewind();
372 startnewtape(0);
373 }
374 timeest();
375 }
376
377 void
378 trewind()
379 {
380 int f;
381 int got;
382
383 for (f = 0; f < SLAVES; f++) {
384 /*
385 * Drain the results, but unlike EOT we DO (or should) care
386 * what the return values were, since if we detect EOT after
387 * we think we've written the last blocks to the tape anyway,
388 * we have to replay those blocks with rollforward.
389 *
390 * fixme: punt for now.
391 */
392 if (slaves[f].sent) {
393 if (atomic_read(slaves[f].fd, (char *)&got, sizeof got)
394 != sizeof got) {
395 perror(" DUMP: error reading command pipe in master");
396 dumpabort(0);
397 }
398 slaves[f].sent = 0;
399 if (got != writesize) {
400 msg("EOT detected in last 2 tape records!\n");
401 msg("Use a longer tape, decrease the size estimate\n");
402 quit("or use no size estimate at all.\n");
403 }
404 }
405 (void) close(slaves[f].fd);
406 }
407 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
408 /* void */;
409
410 if (pipeout)
411 return;
412
413 msg("Closing %s\n", tape);
414
415 #ifdef RDUMP
416 if (host) {
417 rmtclose();
418 while (rmtopen(tape, 0) < 0)
419 sleep(10);
420 rmtclose();
421 return;
422 }
423 #endif
424 (void) close(tapefd);
425 while ((f = open(tape, 0)) < 0)
426 sleep (10);
427 (void) close(f);
428 }
429
430 void
431 close_rewind()
432 {
433 trewind();
434 (void)do_stats();
435 if (nexttape)
436 return;
437 if (!nogripe) {
438 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
439 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
440 }
441 while (!query("Is the new volume mounted and ready to go?"))
442 if (query("Do you want to abort?")) {
443 dumpabort(0);
444 /*NOTREACHED*/
445 }
446 }
447
448 void
449 rollforward()
450 {
451 struct req *p, *q, *prev;
452 struct slave *tslp;
453 int i, size, savedtapea, got;
454 union u_spcl *ntb, *otb;
455 tslp = &slaves[SLAVES];
456 ntb = (union u_spcl *)tslp->tblock[1];
457
458 /*
459 * Each of the N slaves should have requests that need to
460 * be replayed on the next tape. Use the extra slave buffers
461 * (slaves[SLAVES]) to construct request lists to be sent to
462 * each slave in turn.
463 */
464 for (i = 0; i < SLAVES; i++) {
465 q = &tslp->req[1];
466 otb = (union u_spcl *)slp->tblock;
467
468 /*
469 * For each request in the current slave, copy it to tslp.
470 */
471
472 prev = NULL;
473 for (p = slp->req; p->count > 0; p += p->count) {
474 *q = *p;
475 if (p->dblk == 0)
476 *ntb++ = *otb++; /* copy the datablock also */
477 prev = q;
478 q += q->count;
479 }
480 if (prev == NULL)
481 quit("rollforward: protocol botch");
482 if (prev->dblk != 0)
483 prev->count -= 1;
484 else
485 ntb--;
486 q -= 1;
487 q->count = 0;
488 q = &tslp->req[0];
489 if (i == 0) {
490 q->dblk = 0;
491 q->count = 1;
492 trecno = 0;
493 nextblock = tslp->tblock;
494 savedtapea = iswap32(spcl.c_tapea);
495 spcl.c_tapea = iswap32(slp->tapea);
496 startnewtape(0);
497 spcl.c_tapea = iswap32(savedtapea);
498 lastspclrec = savedtapea - 1;
499 }
500 size = (char *)ntb - (char *)q;
501 if (atomic_write(slp->fd, (char *)q, size) != size) {
502 perror(" DUMP: error writing command pipe");
503 dumpabort(0);
504 }
505 slp->sent = 1;
506 if (++slp >= &slaves[SLAVES])
507 slp = &slaves[0];
508
509 q->count = 1;
510
511 if (prev->dblk != 0) {
512 /*
513 * If the last one was a disk block, make the
514 * first of this one be the last bit of that disk
515 * block...
516 */
517 q->dblk = prev->dblk +
518 prev->count * (TP_BSIZE / DEV_BSIZE);
519 ntb = (union u_spcl *)tslp->tblock;
520 } else {
521 /*
522 * It wasn't a disk block. Copy the data to its
523 * new location in the buffer.
524 */
525 q->dblk = 0;
526 *((union u_spcl *)tslp->tblock) = *ntb;
527 ntb = (union u_spcl *)tslp->tblock[1];
528 }
529 }
530 slp->req[0] = *q;
531 nextblock = slp->tblock;
532 if (q->dblk == 0)
533 nextblock++;
534 trecno = 1;
535
536 /*
537 * Clear the first slaves' response. One hopes that it
538 * worked ok, otherwise the tape is much too short!
539 */
540 if (slp->sent) {
541 if (atomic_read(slp->fd, (char *)&got, sizeof got)
542 != sizeof got) {
543 perror(" DUMP: error reading command pipe in master");
544 dumpabort(0);
545 }
546 slp->sent = 0;
547
548 if (got != writesize) {
549 quit("EOT detected at start of the tape!\n");
550 }
551 }
552 }
553
554 /*
555 * We implement taking and restoring checkpoints on the tape level.
556 * When each tape is opened, a new process is created by forking; this
557 * saves all of the necessary context in the parent. The child
558 * continues the dump; the parent waits around, saving the context.
559 * If the child returns X_REWRITE, then it had problems writing that tape;
560 * this causes the parent to fork again, duplicating the context, and
561 * everything continues as if nothing had happened.
562 */
563 void
564 startnewtape(top)
565 int top;
566 {
567 int parentpid;
568 int childpid;
569 int status;
570 int waitpid;
571 char *p;
572 #ifdef sunos
573 void (*interrupt_save)();
574 #else
575 sig_t interrupt_save;
576 #endif
577
578 interrupt_save = signal(SIGINT, SIG_IGN);
579 parentpid = getpid();
580 tapea_volume = iswap32(spcl.c_tapea);
581 (void)time(&tstart_volume);
582
583 restore_check_point:
584 (void)signal(SIGINT, interrupt_save);
585 /*
586 * All signals are inherited...
587 */
588 childpid = fork();
589 if (childpid < 0) {
590 msg("Context save fork fails in parent %d\n", parentpid);
591 Exit(X_ABORT);
592 }
593 if (childpid != 0) {
594 /*
595 * PARENT:
596 * save the context by waiting
597 * until the child doing all of the work returns.
598 * don't catch the interrupt
599 */
600 signal(SIGINT, SIG_IGN);
601 #ifdef TDEBUG
602 msg("Tape: %d; parent process: %d child process %d\n",
603 tapeno+1, parentpid, childpid);
604 #endif /* TDEBUG */
605 while ((waitpid = wait(&status)) != childpid)
606 msg("Parent %d waiting for child %d has another child %d return\n",
607 parentpid, childpid, waitpid);
608 if (status & 0xFF) {
609 msg("Child %d returns LOB status %o\n",
610 childpid, status&0xFF);
611 }
612 status = (status >> 8) & 0xFF;
613 #ifdef TDEBUG
614 switch(status) {
615 case X_FINOK:
616 msg("Child %d finishes X_FINOK\n", childpid);
617 break;
618 case X_ABORT:
619 msg("Child %d finishes X_ABORT\n", childpid);
620 break;
621 case X_REWRITE:
622 msg("Child %d finishes X_REWRITE\n", childpid);
623 break;
624 default:
625 msg("Child %d finishes unknown %d\n",
626 childpid, status);
627 break;
628 }
629 #endif /* TDEBUG */
630 switch(status) {
631 case X_FINOK:
632 Exit(X_FINOK);
633 case X_ABORT:
634 Exit(X_ABORT);
635 case X_REWRITE:
636 goto restore_check_point;
637 default:
638 msg("Bad return code from dump: %d\n", status);
639 Exit(X_ABORT);
640 }
641 /*NOTREACHED*/
642 } else { /* we are the child; just continue */
643 #ifdef TDEBUG
644 sleep(4); /* allow time for parent's message to get out */
645 msg("Child on Tape %d has parent %d, my pid = %d\n",
646 tapeno+1, parentpid, getpid());
647 #endif /* TDEBUG */
648 /*
649 * If we have a name like "/dev/rst0,/dev/rst1",
650 * use the name before the comma first, and save
651 * the remaining names for subsequent volumes.
652 */
653 tapeno++; /* current tape sequence */
654 if (nexttape || strchr(tape, ',')) {
655 if (nexttape && *nexttape)
656 tape = nexttape;
657 if ((p = strchr(tape, ',')) != NULL) {
658 *p = '\0';
659 nexttape = p + 1;
660 } else
661 nexttape = NULL;
662 msg("Dumping volume %d on %s\n", tapeno, tape);
663 }
664 #ifdef RDUMP
665 while ((tapefd = (host ? rmtopen(tape, 2) :
666 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
667 #else
668 while ((tapefd = (pipeout ? 1 :
669 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
670 #endif
671 {
672 msg("Cannot open output \"%s\".\n", tape);
673 if (!query("Do you want to retry the open?"))
674 dumpabort(0);
675 }
676
677 enslave(); /* Share open tape file descriptor with slaves */
678
679 asize = 0;
680 blocksthisvol = 0;
681 if (top)
682 newtape++; /* new tape signal */
683 spcl.c_count = iswap32(slp->count);
684 /*
685 * measure firstrec in TP_BSIZE units since restore doesn't
686 * know the correct ntrec value...
687 */
688 spcl.c_firstrec = iswap32(slp->firstrec);
689 spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
690 spcl.c_type = iswap32(TS_TAPE);
691 spcl.c_flags = iswap32(iswap32(spcl.c_flags) | DR_NEWHEADER);
692 writeheader((ino_t)slp->inode);
693 spcl.c_flags = iswap32(iswap32(spcl.c_flags) & ~ DR_NEWHEADER);
694 msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
695 if (tapeno > 1)
696 msg("Volume %d begins with blocks from inode %d\n",
697 tapeno, slp->inode);
698 }
699 }
700
701 void
702 dumpabort(signo)
703 int signo;
704 {
705
706 if (master != 0 && master != getpid())
707 /* Signals master to call dumpabort */
708 (void) kill(master, SIGTERM);
709 else {
710 killall();
711 msg("The ENTIRE dump is aborted.\n");
712 }
713 #ifdef RDUMP
714 rmtclose();
715 #endif
716 Exit(X_ABORT);
717 }
718
719 void
720 Exit(status)
721 int status;
722 {
723
724 #ifdef TDEBUG
725 msg("pid = %d exits with status %d\n", getpid(), status);
726 #endif /* TDEBUG */
727 exit(status);
728 }
729
730 /*
731 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
732 */
733 static void
734 proceed(signo)
735 int signo;
736 {
737
738 if (ready)
739 longjmp(jmpbuf, 1);
740 caught++;
741 }
742
743 void
744 enslave()
745 {
746 int cmd[2];
747 int i, j;
748
749 master = getpid();
750
751 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
752 signal(SIGPIPE, sigpipe);
753 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
754 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
755
756 for (i = 0; i < SLAVES; i++) {
757 if (i == slp - &slaves[0]) {
758 caught = 1;
759 } else {
760 caught = 0;
761 }
762
763 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
764 (slaves[i].pid = fork()) < 0)
765 quit("too many slaves, %d (recompile smaller): %s\n",
766 i, strerror(errno));
767
768 slaves[i].fd = cmd[1];
769 slaves[i].sent = 0;
770 if (slaves[i].pid == 0) { /* Slave starts up here */
771 for (j = 0; j <= i; j++)
772 (void) close(slaves[j].fd);
773 signal(SIGINT, SIG_IGN); /* Master handles this */
774 signal(SIGINFO, SIG_IGN);
775 doslave(cmd[0], i);
776 Exit(X_FINOK);
777 }
778 }
779
780 for (i = 0; i < SLAVES; i++)
781 (void) atomic_write(slaves[i].fd,
782 (char *) &slaves[(i + 1) % SLAVES].pid,
783 sizeof slaves[0].pid);
784
785 master = 0;
786 }
787
788 void
789 killall()
790 {
791 int i;
792
793 for (i = 0; i < SLAVES; i++)
794 if (slaves[i].pid > 0)
795 (void) kill(slaves[i].pid, SIGKILL);
796 }
797
798 /*
799 * Synchronization - each process has a lockfile, and shares file
800 * descriptors to the following process's lockfile. When our write
801 * completes, we release our lock on the following process's lock-
802 * file, allowing the following process to lock it and proceed. We
803 * get the lock back for the next cycle by swapping descriptors.
804 */
805 static void
806 doslave(cmd, slave_number)
807 int cmd;
808 int slave_number;
809 {
810 int nread;
811 int nextslave, size, wrote, eot_count;
812 sigset_t sigset;
813
814 /*
815 * Need our own seek pointer.
816 */
817 (void) close(diskfd);
818 if ((diskfd = open(disk, O_RDONLY)) < 0)
819 quit("slave couldn't reopen disk: %s\n", strerror(errno));
820
821 /*
822 * Need the pid of the next slave in the loop...
823 */
824 if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
825 != sizeof nextslave) {
826 quit("master/slave protocol botched - didn't get pid of next slave.\n");
827 }
828
829 /*
830 * Get list of blocks to dump, read the blocks into tape buffer
831 */
832 while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
833 struct req *p = slp->req;
834
835 for (trecno = 0; trecno < ntrec;
836 trecno += p->count, p += p->count) {
837 if (p->dblk) {
838 bread(p->dblk, slp->tblock[trecno],
839 p->count * TP_BSIZE);
840 } else {
841 if (p->count != 1 || atomic_read(cmd,
842 (char *)slp->tblock[trecno],
843 TP_BSIZE) != TP_BSIZE)
844 quit("master/slave protocol botched.\n");
845 }
846 }
847 if (setjmp(jmpbuf) == 0) {
848 ready = 1;
849 if (!caught)
850 (void) pause();
851 }
852 ready = 0;
853 caught = 0;
854
855 /* Try to write the data... */
856 eot_count = 0;
857 size = 0;
858
859 while (eot_count < 10 && size < writesize) {
860 #ifdef RDUMP
861 if (host)
862 wrote = rmtwrite(slp->tblock[0]+size,
863 writesize-size);
864 else
865 #endif
866 wrote = write(tapefd, slp->tblock[0]+size,
867 writesize-size);
868 #ifdef WRITEDEBUG
869 fprintf(stderr, "slave %d wrote %d\n", slave_number, wrote);
870 #endif
871 if (wrote < 0)
872 break;
873 if (wrote == 0)
874 eot_count++;
875 size += wrote;
876 }
877
878 #ifdef WRITEDEBUG
879 if (size != writesize)
880 fprintf(stderr, "slave %d only wrote %d out of %d bytes and gave up.\n",
881 slave_number, size, writesize);
882 #endif
883
884 if (eot_count > 0)
885 size = 0;
886
887 /*
888 * fixme: Pyramids running OSx return ENOSPC
889 * at EOT on 1/2 inch drives.
890 */
891 if (size < 0) {
892 (void) kill(master, SIGUSR1);
893 sigemptyset(&sigset);
894 for (;;)
895 sigsuspend(&sigset);
896 } else {
897 /*
898 * pass size of write back to master
899 * (for EOT handling)
900 */
901 (void) atomic_write(cmd, (char *)&size, sizeof size);
902 }
903
904 /*
905 * If partial write, don't want next slave to go.
906 * Also jolts him awake.
907 */
908 (void) kill(nextslave, SIGUSR2);
909 }
910 printcachestats();
911 if (nread != 0)
912 quit("error reading command pipe: %s\n", strerror(errno));
913 }
914
915 /*
916 * Since a read from a pipe may not return all we asked for,
917 * loop until the count is satisfied (or error).
918 */
919 static ssize_t
920 atomic_read(fd, buf, count)
921 int fd;
922 char *buf;
923 int count;
924 {
925 ssize_t got, need = count;
926
927 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
928 buf += got;
929 return (got < 0 ? got : count - need);
930 }
931
932 /*
933 * Since a write may not write all we ask if we get a signal,
934 * loop until the count is satisfied (or error).
935 */
936 static ssize_t
937 atomic_write(fd, buf, count)
938 int fd;
939 char *buf;
940 int count;
941 {
942 ssize_t got, need = count;
943
944 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
945 buf += got;
946 return (got < 0 ? got : count - need);
947 }
948