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