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