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