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