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