]>
git.wh0rd.org - dump.git/blob - dump/tape.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
10 * Copyright (c) 1980, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char rcsid
[] =
44 "$Id: tape.c,v 1.38 2001/03/20 20:25:27 stelian Exp $";
53 #include <compaterr.h>
63 #include <sys/types.h>
65 #include <linux/types.h>
67 #include <sys/param.h>
68 #include <sys/socket.h>
72 #include <linux/ext2_fs.h>
73 #include <ext2fs/ext2fs.h>
74 #include <bsdcompat.h>
76 #include <sys/vnode.h>
79 #include <ufs/inode.h>
81 #include <ufs/ufs/dinode.h>
82 #include <ufs/ffs/fs.h>
83 #endif /* __linux__ */
85 #include <protocols/dumprestore.h>
89 #endif /* HAVE_ZLIB */
93 int writesize
; /* size of malloc()ed buffer for tape */
94 long lastspclrec
= -1; /* tape block number of last written header */
95 int trecno
= 0; /* next record to write in current block */
96 extern long blocksperfile
; /* number of blocks per output file */
97 long blocksthisvol
; /* number of blocks on current output file */
98 extern int ntrec
; /* blocking factor on tape */
104 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
106 static ssize_t atomic_read
__P((int, void *, size_t));
107 static ssize_t atomic_write
__P((int, const void *, size_t));
108 static void doslave
__P((int, int));
109 static void enslave
__P((void));
110 static void flushtape
__P((void));
111 static void killall
__P((void));
112 static void rollforward
__P((void));
113 static int system_command
__P((const char *, const char *, int));
116 * Concurrent dump mods (Caltech) - disk block reading and tape writing
117 * are exported to several slave processes. While one slave writes the
118 * tape, the others read disk blocks; they pass control of the tape in
119 * a ring via signals. The parent process traverses the filesystem and
120 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
121 * The following structure defines the instruction packets sent to slaves.
129 struct slave_results
{
130 ssize_t unclen
; /* uncompressed length */
131 ssize_t clen
; /* compressed length */
134 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
136 int tapea
; /* header number at start of this chunk */
137 int count
; /* count to next header (used for TS_TAPE */
139 int inode
; /* inode that we are currently dealing with */
140 int fd
; /* FD for this slave */
141 int pid
; /* PID for this slave */
142 int sent
; /* 1 == we've sent this slave requests */
143 int firstrec
; /* record number of this block */
144 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
145 struct req
*req
; /* buffer for requests */
149 char (*nextblock
)[TP_BSIZE
];
151 static time_t tstart_volume
; /* time of volume start */
152 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
154 int master
; /* pid of master, for sending error signals */
155 int tenths
; /* length of tape overhead per block written */
156 static int caught
; /* have we caught the signal to proceed? */
157 static int ready
; /* have we reached the lock point without having */
158 /* received the SIGUSR2 signal from the prev slave? */
159 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
160 /* SIGUSR2 arrives from the previous slave */
165 int pgoff
= getpagesize() - 1;
169 writesize
= ntrec
* TP_BSIZE
;
170 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
172 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
173 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
174 * repositioning after stopping, i.e, streaming mode, where the gap is
175 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
177 if (blocksperfile
== 0 && !unlimited
)
178 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
184 * Allocate tape buffer contiguous with the array of instruction
185 * packets, so flushtape() can write them together with one write().
186 * Align tape buffer on page boundary to speed up tape write().
188 for (i
= 0; i
<= SLAVES
; i
++) {
190 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
193 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
195 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
197 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
199 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
205 nextblock
= slp
->tblock
;
210 writerec(const void *dp
, int isspcl
)
213 slp
->req
[trecno
].dblk
= (daddr_t
)0;
214 slp
->req
[trecno
].count
= 1;
215 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
216 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
219 lastspclrec
= spcl
.c_tapea
;
227 dumpblock(daddr_t blkno
, int size
)
229 int avail
, tpblks
, dblkno
;
231 dblkno
= fsbtodb(sblock
, blkno
);
232 tpblks
= size
>> tp_bshift
;
233 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
234 slp
->req
[trecno
].dblk
= dblkno
;
235 slp
->req
[trecno
].count
= avail
;
237 spcl
.c_tapea
+= avail
;
240 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
252 msg("write error on %s: %s\n", tape
, strerror(errnum
));
253 quit("Cannot recover\n");
256 msg("write error %d blocks into volume %d: %s\n", blocksthisvol
, tapeno
, strerror(errnum
));
257 broadcast("DUMP WRITE ERROR!\n");
258 if (query("Do you want to rewrite this volume?")) {
259 msg("Closing this volume. Prepare to restart with new media;\n");
260 msg("this dump volume will be rewritten.\n");
266 if (query("Do you want to start the next tape?"))
275 quit("Broken pipe\n");
280 * Update xferrate stats
289 ttaken
= tnow
- tstart_volume
;
290 blocks
= spcl
.c_tapea
- tapea_volume
;
291 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
293 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno
,
294 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
296 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
297 long txfrate
= volkb
/ ttaken
;
298 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
299 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
300 msg("Volume %d transfer rate: %ld KB/s\n", tapeno
,
304 double rate
= .0005 + (double) blocks
/ (double) volkb
;
305 msg("Volume %d %ldKB uncompressed, %ldKB compressed,"
307 tapeno
, blocks
, volkb
, rate
);
314 mktimeest(time_t tnow
)
316 static char msgbuf
[128];
321 if (blockswritten
< 500)
323 if (blockswritten
> tapesize
)
324 tapesize
= blockswritten
;
325 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
326 / blockswritten
* tapesize
;
327 if (tnow
> tstart_volume
)
328 (void)snprintf(msgbuf
, sizeof(msgbuf
),
329 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
330 (blockswritten
* 100.0) / tapesize
,
331 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
332 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
334 (void)snprintf(msgbuf
, sizeof(msgbuf
),
335 "%3.2f%% done, finished in %d:%02d\n",
336 (blockswritten
* 100.0) / tapesize
,
337 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
345 * information message upon receipt of SIGINFO
348 statussig(int notused
)
350 int save_errno
= errno
;
353 buf
= mktimeest(time(NULL
));
355 write(STDERR_FILENO
, buf
, strlen(buf
));
365 struct slave_results returned
;
367 int siz
= (char *)nextblock
- (char *)slp
->req
;
369 slp
->req
[trecno
].count
= 0; /* Sentinel */
371 if (atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
372 quit("error writing command pipe: %s\n", strerror(errno
));
373 slp
->sent
= 1; /* we sent a request, read the response later */
375 lastfirstrec
= slp
->firstrec
;
377 if (++slp
>= &slaves
[SLAVES
])
380 /* Read results back from next slave */
382 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
383 != sizeof returned
) {
384 perror(" DUMP: error reading command pipe in master");
387 got
= returned
.unclen
;
388 bytes_written
+= returned
.clen
;
389 if (returned
.unclen
== returned
.clen
)
393 /* Check for errors or end of tape */
395 /* Check for errors */
399 msg("End of tape detected\n");
402 * Drain the results, don't care what the values were.
403 * If we read them here then trewind won't...
405 for (i
= 0; i
< SLAVES
; i
++) {
406 if (slaves
[i
].sent
) {
407 if (atomic_read( slaves
[i
].fd
,
408 (char *)&returned
, sizeof returned
)
409 != sizeof returned
) {
410 perror(" DUMP: error reading command pipe in master");
424 if (spcl
.c_type
!= TS_END
) {
425 for (i
= 0; i
< spcl
.c_count
; i
++)
426 if (spcl
.c_addr
[i
] != 0)
429 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
430 slp
->tapea
= spcl
.c_tapea
;
431 slp
->firstrec
= lastfirstrec
+ ntrec
;
433 nextblock
= slp
->tblock
;
435 asize
+= tenths
+ returned
.clen
/ density
;
436 blockswritten
+= ntrec
;
437 blocksthisvol
+= ntrec
;
438 if (!pipeout
&& !unlimited
&& (blocksperfile
?
439 (blocksthisvol
>= blocksperfile
) : (asize
> tsize
))) {
447 * Executes the command in a shell.
448 * Returns -1 if an error occured, the exit status of
449 * the command on success.
451 int system_command(const char *command
, const char *device
, int volnum
) {
453 char commandstr
[4096];
457 perror(" DUMP: unable to fork");
463 #if OLD_STYLE_FSCRIPT
464 snprintf(commandstr
, sizeof(commandstr
), "%s", command
);
466 snprintf(commandstr
, sizeof(commandstr
), "%s %s %d", command
, device
, volnum
);
468 commandstr
[sizeof(commandstr
) - 1] = '\0';
469 execl("/bin/sh", "sh", "-c", commandstr
, NULL
);
470 perror(" DUMP: unable to execute shell");
474 if (waitpid(pid
, &status
, 0) == -1) {
475 if (errno
!= EINTR
) {
476 perror(" DUMP: waitpid error");
480 if (WIFEXITED(status
))
481 return WEXITSTATUS(status
);
493 struct slave_results returned
;
495 for (f
= 0; f
< SLAVES
; f
++) {
497 * Drain the results, but unlike EOT we DO (or should) care
498 * what the return values were, since if we detect EOT after
499 * we think we've written the last blocks to the tape anyway,
500 * we have to replay those blocks with rollforward.
502 * fixme: punt for now.
504 if (slaves
[f
].sent
) {
505 if (atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
506 != sizeof returned
) {
507 perror(" DUMP: error reading command pipe in master");
510 got
= returned
.unclen
;
511 bytes_written
+= returned
.clen
;
512 if (returned
.unclen
== returned
.clen
)
520 msg("EOT detected in last 2 tape records!\n");
521 msg("Use a longer tape, decrease the size estimate\n");
522 quit("or use no size estimate at all.\n");
525 (void) close(slaves
[f
].fd
);
527 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
532 msg("Closing %s\n", tape
);
537 while (rmtopen(tape
, 0) < 0)
544 (void) close(tapefd
);
545 while ((f
= open(tape
, 0)) < 0)
550 if (eot_script
&& spcl
.c_type
!= TS_END
) {
551 msg("Launching %s\n", eot_script
);
552 eot_code
= system_command(eot_script
, tape
, tapeno
);
554 if (eot_code
!= 0 && eot_code
!= 1) {
555 msg("Dump aborted by the end of tape script\n");
567 if (nexttape
|| Mflag
|| (eot_code
== 0) )
570 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
571 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
573 while (!query("Is the new volume mounted and ready to go?"))
574 if (query("Do you want to abort?")) {
583 register struct req
*p
, *q
, *prev
;
584 register struct slave
*tslp
;
585 int i
, size
, savedtapea
, got
;
586 union u_spcl
*ntb
, *otb
;
587 struct slave_results returned
;
592 tslp
= &slaves
[SLAVES
];
593 ntb
= (union u_spcl
*)tslp
->tblock
[1];
596 * Each of the N slaves should have requests that need to
597 * be replayed on the next tape. Use the extra slave buffers
598 * (slaves[SLAVES]) to construct request lists to be sent to
599 * each slave in turn.
601 for (i
= 0; i
< SLAVES
; i
++) {
603 otb
= (union u_spcl
*)slp
->tblock
;
606 * For each request in the current slave, copy it to tslp.
610 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
613 *ntb
++ = *otb
++; /* copy the datablock also */
618 quit("rollforward: protocol botch");
630 nextblock
= tslp
->tblock
;
631 savedtapea
= spcl
.c_tapea
;
632 spcl
.c_tapea
= slp
->tapea
;
634 spcl
.c_tapea
= savedtapea
;
635 lastspclrec
= savedtapea
- 1;
637 size
= (char *)ntb
- (char *)q
;
638 if (atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
639 perror(" DUMP: error writing command pipe");
644 lastfirstrec
= slp
->firstrec
;
646 if (++slp
>= &slaves
[SLAVES
])
651 if (prev
->dblk
!= 0) {
653 * If the last one was a disk block, make the
654 * first of this one be the last bit of that disk
657 q
->dblk
= prev
->dblk
+
658 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
659 ntb
= (union u_spcl
*)tslp
->tblock
;
662 * It wasn't a disk block. Copy the data to its
663 * new location in the buffer.
666 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
667 ntb
= (union u_spcl
*)tslp
->tblock
[1];
671 nextblock
= slp
->tblock
;
674 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
675 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
682 * Clear the first slaves' response. One hopes that it
683 * worked ok, otherwise the tape is much too short!
686 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
687 != sizeof returned
) {
688 perror(" DUMP: error reading command pipe in master");
691 got
= returned
.unclen
;
692 bytes_written
+= returned
.clen
;
693 if (returned
.clen
== returned
.unclen
)
701 quit("EOT detected at start of the tape!\n");
707 if (spcl
.c_type
!= TS_END
) {
708 for (i
= 0; i
< spcl
.c_count
; i
++)
709 if (spcl
.c_addr
[i
] != 0)
713 slp
->firstrec
= lastfirstrec
+ ntrec
;
714 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
716 asize
+= tenths
+ returned
.clen
/ density
;
717 blockswritten
+= ntrec
;
718 blocksthisvol
+= ntrec
;
723 * We implement taking and restoring checkpoints on the tape level.
724 * When each tape is opened, a new process is created by forking; this
725 * saves all of the necessary context in the parent. The child
726 * continues the dump; the parent waits around, saving the context.
727 * If the child returns X_REWRITE, then it had problems writing that tape;
728 * this causes the parent to fork again, duplicating the context, and
729 * everything continues as if nothing had happened.
732 startnewtape(int top
)
743 sigaddset(&sigs
, SIGINT
);
744 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
745 #else /* __linux__ */
747 void (*interrupt_save
)();
749 sig_t interrupt_save
;
751 interrupt_save
= signal(SIGINT
, SIG_IGN
);
752 #endif /* __linux__ */
754 parentpid
= getpid();
755 tapea_volume
= spcl
.c_tapea
;
756 tapea_bytes
= bytes_written
;
757 tstart_volume
= time(NULL
);
761 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
763 (void)signal(SIGINT
, interrupt_save
);
766 * All signals are inherited...
770 msg("Context save fork fails in parent %d\n", parentpid
);
776 * save the context by waiting
777 * until the child doing all of the work returns.
778 * don't catch the interrupt
781 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
783 signal(SIGINT
, SIG_IGN
);
786 msg("Tape: %d; parent process: %d child process %d\n",
787 tapeno
+1, parentpid
, childpid
);
789 while ((waitpid
= wait(&status
)) != childpid
)
790 if (waitpid
!= rshpid
)
791 msg("Parent %d waiting for child %d has another child %d return\n",
792 parentpid
, childpid
, waitpid
);
794 msg("Child %d returns LOB status %o\n",
795 childpid
, status
&0xFF);
797 status
= (status
>> 8) & 0xFF;
801 msg("Child %d finishes X_FINOK\n", childpid
);
804 msg("Child %d finishes X_ABORT\n", childpid
);
807 msg("Child %d finishes X_REWRITE\n", childpid
);
810 msg("Child %d finishes unknown %d\n",
821 goto restore_check_point
;
823 msg("Bad return code from dump: %d\n", status
);
827 } else { /* we are the child; just continue */
829 sleep(4); /* allow time for parent's message to get out */
830 msg("Child on Tape %d has parent %d, my pid = %d\n",
831 tapeno
+1, parentpid
, getpid());
834 * If we have a name like "/dev/rmt0,/dev/rmt1",
835 * use the name before the comma first, and save
836 * the remaining names for subsequent volumes.
838 tapeno
++; /* current tape sequence */
840 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
841 tape
[MAXPATHLEN
- 1] = '\0';
842 msg("Dumping volume %d on %s\n", tapeno
, tape
);
844 else if (nexttape
|| strchr(tapeprefix
, ',')) {
845 if (nexttape
&& *nexttape
)
846 tapeprefix
= nexttape
;
847 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
852 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
853 tape
[MAXPATHLEN
- 1] = '\0';
854 msg("Dumping volume %d on %s\n", tapeno
, tape
);
857 while ((tapefd
= (host
? rmtopen(tape
, 2) : pipeout
?
859 open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
861 while ((tapefd
= (pipeout
? fileno(stdout
) :
862 open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
865 msg("Cannot open output \"%s\".\n", tape
);
866 if (!query("Do you want to retry the open?"))
870 enslave(); /* Share open tape file descriptor with slaves */
875 newtape
++; /* new tape signal */
876 spcl
.c_count
= slp
->count
;
878 * measure firstrec in TP_BSIZE units since restore doesn't
879 * know the correct ntrec value...
881 spcl
.c_firstrec
= slp
->firstrec
;
883 spcl
.c_type
= TS_TAPE
;
884 spcl
.c_flags
|= DR_NEWHEADER
;
886 spcl
.c_flags
|= DR_COMPRESSED
;
887 writeheader((dump_ino_t
)slp
->inode
);
888 spcl
.c_flags
&=~ DR_NEWHEADER
;
889 msg("Volume %d started with block %ld at: %s", tapeno
,
890 spcl
.c_tapea
, ctime(&tstart_volume
));
892 msg("Volume %d begins with blocks from inode %d\n",
901 if (master
!= 0 && master
!= getpid())
902 /* Signals master to call dumpabort */
903 (void) kill(master
, SIGTERM
);
906 msg("The ENTIRE dump is aborted.\n");
919 msg("pid = %d exits with status %d\n", getpid(), status
);
925 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
931 siglongjmp(jmpbuf
, 1);
939 #ifdef LINUX_FORK_BUG
947 { struct sigaction sa
;
948 memset(&sa
, 0, sizeof sa
);
949 sigemptyset(&sa
.sa_mask
);
950 sa
.sa_handler
= dumpabort
;
951 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
952 sa
.sa_handler
= sigpipe
;
953 sigaction(SIGPIPE
, &sa
, NULL
);
954 sa
.sa_handler
= proceed
;
955 sa
.sa_flags
= SA_RESTART
;
956 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
959 for (i
= 0; i
< SLAVES
; i
++) {
960 if (i
== slp
- &slaves
[0]) {
966 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
967 (slaves
[i
].pid
= fork()) < 0)
968 quit("too many slaves, %d (recompile smaller): %s\n",
971 slaves
[i
].fd
= cmd
[1];
973 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
975 for (j
= 0; j
<= i
; j
++)
976 (void) close(slaves
[j
].fd
);
978 sigaddset(&sigs
, SIGINT
); /* Master handles this */
980 sigaddset(&sigs
, SIGINFO
);
982 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
984 #ifdef LINUX_FORK_BUG
985 if (atomic_write( cmd
[0], (char *) &i
, sizeof i
)
987 quit("master/slave protocol botched 3\n");
994 #ifdef LINUX_FORK_BUG
996 * Wait for all slaves to _actually_ start to circumvent a bug in
997 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
998 * returned from fork() causes a SEGV in the child process
1000 for (i
= 0; i
< SLAVES
; i
++)
1001 if (atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1002 quit("master/slave protocol botched 4\n");
1005 for (i
= 0; i
< SLAVES
; i
++)
1006 (void) atomic_write( slaves
[i
].fd
,
1007 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1008 sizeof slaves
[0].pid
);
1018 for (i
= 0; i
< SLAVES
; i
++)
1019 if (slaves
[i
].pid
> 0) {
1020 (void) kill(slaves
[i
].pid
, SIGKILL
);
1026 * Synchronization - each process waits for a SIGUSR2 from the
1027 * previous process before writing to the tape, and sends SIGUSR2
1028 * to the next process when the tape write completes. On tape errors
1029 * a SIGUSR1 is sent to the master which then terminates all of the
1033 doslave(int cmd
, int slave_number
)
1036 int nextslave
, size
, eot_count
, bufsize
;
1037 volatile int wrote
= 0;
1040 struct tapebuf
*comp_buf
= NULL
;
1041 int compresult
, do_compress
= 0;
1042 unsigned long worklen
;
1043 #endif /* HAVE_ZLIB */
1044 struct slave_results returns
;
1050 * Need our own seek pointer.
1052 (void) close(diskfd
);
1053 if ((diskfd
= open(disk
, O_RDONLY
)) < 0)
1054 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1057 retval
= dump_fs_open(disk
, &fs
);
1059 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1060 #endif /* __linux__ */
1063 * Need the pid of the next slave in the loop...
1065 if ((nread
= atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1066 != sizeof nextslave
) {
1067 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1071 /* if we're doing a compressed dump, allocate the compress buffer */
1073 comp_buf
= malloc(sizeof(struct tapebuf
) + TP_BSIZE
+ writesize
);
1074 if (comp_buf
== NULL
)
1075 quit("couldn't allocate a compress buffer.\n");
1076 comp_buf
->flags
= 0;
1078 #endif /* HAVE_ZLIB */
1081 * Get list of blocks to dump, read the blocks into tape buffer
1083 while ((nread
= atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1084 register struct req
*p
= slp
->req
;
1086 for (trecno
= 0; trecno
< ntrec
;
1087 trecno
+= p
->count
, p
+= p
->count
) {
1088 if (p
->dblk
) { /* read a disk block */
1089 bread(p
->dblk
, slp
->tblock
[trecno
],
1090 p
->count
* TP_BSIZE
);
1091 } else { /* read record from pipe */
1092 if (p
->count
!= 1 || atomic_read( cmd
,
1093 (char *)slp
->tblock
[trecno
],
1094 TP_BSIZE
) != TP_BSIZE
)
1095 quit("master/slave protocol botched.\n");
1099 /* Try to write the data... */
1103 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1104 bufsize
= writesize
; /* length to write */
1105 returns
.clen
= returns
.unclen
= bufsize
;
1109 * When writing a compressed dump, each block is
1110 * written from struct tapebuf with an 4 byte prefix
1111 * followed by the data. This can be less than
1112 * writesize. Restore, on a short read, can compare the
1113 * length read to the compressed length in the header
1114 * to verify that the read was good. Blocks which don't
1115 * compress well are written uncompressed.
1116 * The first block written by each slave is not compressed.
1120 comp_buf
->length
= bufsize
;
1121 worklen
= TP_BSIZE
+ writesize
;
1123 compresult
= compress2(comp_buf
->buf
, &worklen
,
1124 (char *)slp
->tblock
[0], writesize
, compressed
);
1125 if (compresult
== Z_OK
&& worklen
<= writesize
-32) {
1126 /* write the compressed buffer */
1127 comp_buf
->length
= worklen
;
1128 comp_buf
->compressed
= 1;
1129 buffer
= (char *) comp_buf
;
1130 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1133 /* write the data uncompressed */
1134 comp_buf
->length
= writesize
;
1135 comp_buf
->compressed
= 0;
1136 buffer
= (char *) comp_buf
;
1137 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1138 returns
.unclen
= returns
.clen
;
1139 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1142 /* compress the remaining blocks */
1143 do_compress
= compressed
;
1144 #endif /* HAVE_ZLIB */
1146 if (sigsetjmp(jmpbuf
, 1) == 0) {
1154 while (eot_count
< 10 && size
< bufsize
) {
1157 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1160 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1162 printf("slave %d wrote %d\n", slave_number
, wrote
);
1172 if (size
!= bufsize
)
1173 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1174 slave_number
, size
, bufsize
);
1178 * Handle ENOSPC as an EOT condition.
1180 if (wrote
< 0 && errno
== ENOSPC
) {
1186 returns
.clen
= returns
.unclen
= 0;
1189 * pass errno back to master for special handling
1192 returns
.unclen
= -errno
;
1195 * pass size of data and size of write back to master
1196 * (for EOT handling)
1198 (void) atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1201 * Signal the next slave to go.
1203 (void) kill(nextslave
, SIGUSR2
);
1206 quit("error reading command pipe: %s\n", strerror(errno
));
1210 * Since a read from a pipe may not return all we asked for,
1211 * or a write may not write all we ask if we get a signal,
1212 * loop until the count is satisfied (or error).
1215 atomic_read(int fd
, void *buf
, size_t count
)
1217 int got
, need
= count
;
1220 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1222 } while (got
== -1 && errno
== EINTR
);
1223 return (got
< 0 ? got
: count
- need
);
1227 * Since a read from a pipe may not return all we asked for,
1228 * or a write may not write all we ask if we get a signal,
1229 * loop until the count is satisfied (or error).
1232 atomic_write(int fd
, const void *buf
, size_t count
)
1234 int got
, need
= count
;
1237 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1239 } while (got
== -1 && errno
== EINTR
);
1240 return (got
< 0 ? got
: count
- need
);