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