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