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