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