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