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