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