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