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