]>
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.47 2001/06/18 10:58:28 stelian Exp $";
48 #include <compatlfs.h>
54 #include <compaterr.h>
64 #include <sys/types.h>
67 #include <sys/param.h>
68 #include <sys/socket.h>
73 #ifdef HAVE_EXT2FS_EXT2_FS_H
74 #include <ext2fs/ext2_fs.h>
76 #include <linux/ext2_fs.h>
78 #include <ext2fs/ext2fs.h>
79 #include <bsdcompat.h>
81 #include <sys/vnode.h>
84 #include <ufs/inode.h>
86 #include <ufs/ufs/dinode.h>
87 #include <ufs/ffs/fs.h>
88 #endif /* __linux__ */
90 #include <protocols/dumprestore.h>
94 #endif /* HAVE_ZLIB */
98 int writesize
; /* size of malloc()ed buffer for tape */
99 long lastspclrec
= -1; /* tape block number of last written header */
100 int trecno
= 0; /* next record to write in current block */
101 extern long blocksperfile
; /* number of blocks per output file */
102 long blocksthisvol
; /* number of blocks on current output file */
103 extern int ntrec
; /* blocking factor on tape */
104 extern int cartridge
;
109 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
111 static ssize_t atomic_read
__P((int, void *, size_t));
112 static ssize_t atomic_write
__P((int, const void *, size_t));
113 static void doslave
__P((int, int));
114 static void enslave
__P((void));
115 static void flushtape
__P((void));
116 static void killall
__P((void));
117 static void rollforward
__P((void));
118 static int system_command
__P((const char *, const char *, int));
121 * Concurrent dump mods (Caltech) - disk block reading and tape writing
122 * are exported to several slave processes. While one slave writes the
123 * tape, the others read disk blocks; they pass control of the tape in
124 * a ring via signals. The parent process traverses the filesystem and
125 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
126 * The following structure defines the instruction packets sent to slaves.
134 struct slave_results
{
135 ssize_t unclen
; /* uncompressed length */
136 ssize_t clen
; /* compressed length */
139 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
141 int tapea
; /* header number at start of this chunk */
142 int count
; /* count to next header (used for TS_TAPE */
144 int inode
; /* inode that we are currently dealing with */
145 int fd
; /* FD for this slave */
146 int pid
; /* PID for this slave */
147 int sent
; /* 1 == we've sent this slave requests */
148 int firstrec
; /* record number of this block */
149 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
150 struct req
*req
; /* buffer for requests */
154 char (*nextblock
)[TP_BSIZE
];
156 static time_t tstart_volume
; /* time of volume start */
157 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
159 int master
; /* pid of master, for sending error signals */
160 int tenths
; /* length of tape overhead per block written */
161 static int caught
; /* have we caught the signal to proceed? */
162 static int ready
; /* have we reached the lock point without having */
163 /* received the SIGUSR2 signal from the prev slave? */
164 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
165 /* SIGUSR2 arrives from the previous slave */
167 static int gtperr
= 0;
173 int pgoff
= getpagesize() - 1;
177 writesize
= ntrec
* TP_BSIZE
;
178 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
180 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
181 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
182 * repositioning after stopping, i.e, streaming mode, where the gap is
183 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
185 if (blocksperfile
== 0 && !unlimited
)
186 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
192 * Allocate tape buffer contiguous with the array of instruction
193 * packets, so flushtape() can write them together with one write().
194 * Align tape buffer on page boundary to speed up tape write().
196 for (i
= 0; i
<= SLAVES
; i
++) {
198 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
201 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
203 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
205 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
207 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
213 nextblock
= slp
->tblock
;
218 writerec(const void *dp
, int isspcl
)
221 slp
->req
[trecno
].dblk
= (daddr_t
)0;
222 slp
->req
[trecno
].count
= 1;
223 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
224 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
227 lastspclrec
= spcl
.c_tapea
;
235 dumpblock(daddr_t blkno
, int size
)
237 int avail
, tpblks
, dblkno
;
239 dblkno
= fsbtodb(sblock
, blkno
);
240 tpblks
= size
>> tp_bshift
;
241 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
242 slp
->req
[trecno
].dblk
= dblkno
;
243 slp
->req
[trecno
].count
= avail
;
245 spcl
.c_tapea
+= avail
;
248 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
260 msg("write error on %s: %s\n", tape
, strerror(errnum
));
261 quit("Cannot recover\n");
264 msg("write error %d blocks into volume %d: %s\n",
265 blocksthisvol
, tapeno
, strerror(errnum
));
266 broadcast("DUMP WRITE ERROR!\n");
267 if (query("Do you want to rewrite this volume?")) {
268 msg("Closing this volume. Prepare to restart with new media;\n");
269 msg("this dump volume will be rewritten.\n");
275 if (query("Do you want to start the next tape?"))
284 quit("Broken pipe\n");
289 * Update xferrate stats
298 ttaken
= tnow
- tstart_volume
;
299 blocks
= spcl
.c_tapea
- tapea_volume
;
300 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
302 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno
,
303 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
305 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
306 long txfrate
= volkb
/ ttaken
;
307 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
308 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
309 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
313 double rate
= .0005 + (double) blocks
/ (double) volkb
;
314 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
316 tapeno
, blocks
, volkb
, rate
);
323 mktimeest(time_t tnow
)
325 static char msgbuf
[128];
330 if (blockswritten
< 500)
332 if (blockswritten
> tapesize
)
333 tapesize
= blockswritten
;
334 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
335 / blockswritten
* tapesize
;
336 if (tnow
> tstart_volume
)
337 (void)snprintf(msgbuf
, sizeof(msgbuf
),
338 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
339 (blockswritten
* 100.0) / tapesize
,
340 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
341 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
343 (void)snprintf(msgbuf
, sizeof(msgbuf
),
344 "%3.2f%% done, finished in %d:%02d\n",
345 (blockswritten
* 100.0) / tapesize
,
346 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
354 * information message upon receipt of SIGINFO
357 statussig(int notused
)
359 int save_errno
= errno
;
362 buf
= mktimeest(time(NULL
));
364 write(STDERR_FILENO
, buf
, strlen(buf
));
374 struct slave_results returned
;
376 int siz
= (char *)nextblock
- (char *)slp
->req
;
378 slp
->req
[trecno
].count
= 0; /* Sentinel */
380 if (atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
381 quit("error writing command pipe: %s\n", strerror(errno
));
382 slp
->sent
= 1; /* we sent a request, read the response later */
384 lastfirstrec
= slp
->firstrec
;
386 if (++slp
>= &slaves
[SLAVES
])
389 /* Read results back from next slave */
391 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
392 != sizeof returned
) {
393 perror(" DUMP: error reading command pipe in master");
396 got
= returned
.unclen
;
397 bytes_written
+= returned
.clen
;
398 if (returned
.unclen
== returned
.clen
)
402 /* Check for errors or end of tape */
404 /* Check for errors */
408 msg("End of tape detected\n");
411 * Drain the results, don't care what the values were.
412 * If we read them here then trewind won't...
414 for (i
= 0; i
< SLAVES
; i
++) {
415 if (slaves
[i
].sent
) {
416 if (atomic_read( slaves
[i
].fd
,
417 (char *)&returned
, sizeof returned
)
418 != sizeof returned
) {
419 perror(" DUMP: error reading command pipe in master");
433 if (spcl
.c_type
!= TS_END
) {
434 for (i
= 0; i
< spcl
.c_count
; i
++)
435 if (spcl
.c_addr
[i
] != 0)
438 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
439 slp
->tapea
= spcl
.c_tapea
;
440 slp
->firstrec
= lastfirstrec
+ ntrec
;
442 nextblock
= slp
->tblock
;
444 asize
+= tenths
+ returned
.clen
/ density
;
445 blockswritten
+= ntrec
;
446 blocksthisvol
+= ntrec
;
447 if (!pipeout
&& !unlimited
&& (blocksperfile
?
448 (blocksthisvol
>= blocksperfile
) : (asize
> tsize
))) {
456 * Executes the command in a shell.
457 * Returns -1 if an error occured, the exit status of
458 * the command on success.
460 int system_command(const char *command
, const char *device
, int volnum
) {
462 char commandstr
[4096];
466 perror(" DUMP: unable to fork");
472 #if OLD_STYLE_FSCRIPT
473 snprintf(commandstr
, sizeof(commandstr
), "%s", command
);
475 snprintf(commandstr
, sizeof(commandstr
), "%s %s %d", command
, device
, volnum
);
477 commandstr
[sizeof(commandstr
) - 1] = '\0';
478 execl("/bin/sh", "sh", "-c", commandstr
, NULL
);
479 perror(" DUMP: unable to execute shell");
483 if (waitpid(pid
, &status
, 0) == -1) {
484 if (errno
!= EINTR
) {
485 perror(" DUMP: waitpid error");
489 if (WIFEXITED(status
))
490 return WEXITSTATUS(status
);
502 struct slave_results returned
;
504 for (f
= 0; f
< SLAVES
; f
++) {
506 * Drain the results, but unlike EOT we DO (or should) care
507 * what the return values were, since if we detect EOT after
508 * we think we've written the last blocks to the tape anyway,
509 * we have to replay those blocks with rollforward.
511 * fixme: punt for now.
513 if (slaves
[f
].sent
) {
514 if (atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
515 != sizeof returned
) {
516 perror(" DUMP: error reading command pipe in master");
519 got
= returned
.unclen
;
520 bytes_written
+= returned
.clen
;
521 if (returned
.unclen
== returned
.clen
)
529 msg("EOT detected in last 2 tape records!\n");
530 msg("Use a longer tape, decrease the size estimate\n");
531 quit("or use no size estimate at all.\n");
534 (void) close(slaves
[f
].fd
);
536 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
541 msg("Closing %s\n", tape
);
546 while (rmtopen(tape
, 0) < 0)
553 (void) close(tapefd
);
554 while ((f
= OPEN(tape
, 0)) < 0)
559 if (eot_script
&& spcl
.c_type
!= TS_END
) {
560 msg("Launching %s\n", eot_script
);
561 eot_code
= system_command(eot_script
, tape
, tapeno
);
563 if (eot_code
!= 0 && eot_code
!= 1) {
564 msg("Dump aborted by the end of tape script\n");
576 if (nexttape
|| Mflag
|| (eot_code
== 0) )
579 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
580 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
582 while (!query("Is the new volume mounted and ready to go?"))
583 if (query("Do you want to abort?")) {
592 register struct req
*p
, *q
, *prev
;
593 register struct slave
*tslp
;
594 int i
, size
, savedtapea
, got
;
595 union u_spcl
*ntb
, *otb
;
596 struct slave_results returned
;
601 tslp
= &slaves
[SLAVES
];
602 ntb
= (union u_spcl
*)tslp
->tblock
[1];
605 * Each of the N slaves should have requests that need to
606 * be replayed on the next tape. Use the extra slave buffers
607 * (slaves[SLAVES]) to construct request lists to be sent to
608 * each slave in turn.
610 for (i
= 0; i
< SLAVES
; i
++) {
612 otb
= (union u_spcl
*)slp
->tblock
;
615 * For each request in the current slave, copy it to tslp.
619 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
622 *ntb
++ = *otb
++; /* copy the datablock also */
627 quit("rollforward: protocol botch");
639 nextblock
= tslp
->tblock
;
640 savedtapea
= spcl
.c_tapea
;
641 spcl
.c_tapea
= slp
->tapea
;
643 spcl
.c_tapea
= savedtapea
;
644 lastspclrec
= savedtapea
- 1;
646 size
= (char *)ntb
- (char *)q
;
647 if (atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
648 perror(" DUMP: error writing command pipe");
653 lastfirstrec
= slp
->firstrec
;
655 if (++slp
>= &slaves
[SLAVES
])
660 if (prev
->dblk
!= 0) {
662 * If the last one was a disk block, make the
663 * first of this one be the last bit of that disk
666 q
->dblk
= prev
->dblk
+
667 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
668 ntb
= (union u_spcl
*)tslp
->tblock
;
671 * It wasn't a disk block. Copy the data to its
672 * new location in the buffer.
675 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
676 ntb
= (union u_spcl
*)tslp
->tblock
[1];
680 nextblock
= slp
->tblock
;
683 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
684 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
691 * Clear the first slaves' response. One hopes that it
692 * worked ok, otherwise the tape is much too short!
695 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
696 != sizeof returned
) {
697 perror(" DUMP: error reading command pipe in master");
700 got
= returned
.unclen
;
701 bytes_written
+= returned
.clen
;
702 if (returned
.clen
== returned
.unclen
)
710 quit("EOT detected at start of the tape!\n");
716 if (spcl
.c_type
!= TS_END
) {
717 for (i
= 0; i
< spcl
.c_count
; i
++)
718 if (spcl
.c_addr
[i
] != 0)
722 slp
->firstrec
= lastfirstrec
+ ntrec
;
723 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
725 asize
+= tenths
+ returned
.clen
/ density
;
726 blockswritten
+= ntrec
;
727 blocksthisvol
+= ntrec
;
732 * We implement taking and restoring checkpoints on the tape level.
733 * When each tape is opened, a new process is created by forking; this
734 * saves all of the necessary context in the parent. The child
735 * continues the dump; the parent waits around, saving the context.
736 * If the child returns X_REWRITE, then it had problems writing that tape;
737 * this causes the parent to fork again, duplicating the context, and
738 * everything continues as if nothing had happened.
741 startnewtape(int top
)
752 sigaddset(&sigs
, SIGINT
);
753 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
754 #else /* __linux__ */
756 void (*interrupt_save
)();
758 sig_t interrupt_save
;
760 interrupt_save
= signal(SIGINT
, SIG_IGN
);
761 #endif /* __linux__ */
763 parentpid
= getpid();
764 tapea_volume
= spcl
.c_tapea
;
765 tapea_bytes
= bytes_written
;
766 tstart_volume
= time(NULL
);
770 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
772 (void)signal(SIGINT
, interrupt_save
);
775 * All signals are inherited...
779 msg("Context save fork fails in parent %d\n", parentpid
);
785 * save the context by waiting
786 * until the child doing all of the work returns.
787 * don't catch the interrupt
790 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
792 signal(SIGINT
, SIG_IGN
);
795 msg("Tape: %d; parent process: %d child process %d\n",
796 tapeno
+1, parentpid
, childpid
);
798 while ((waitpid
= wait(&status
)) != childpid
)
799 if (waitpid
!= rshpid
)
800 msg("Parent %d waiting for child %d has another child %d return\n",
801 parentpid
, childpid
, waitpid
);
803 msg("Child %d returns LOB status %o\n",
804 childpid
, status
&0xFF);
806 status
= (status
>> 8) & 0xFF;
810 msg("Child %d finishes X_FINOK\n", childpid
);
813 msg("Child %d finishes X_ABORT\n", childpid
);
816 msg("Child %d finishes X_REWRITE\n", childpid
);
819 msg("Child %d finishes unknown %d\n",
830 goto restore_check_point
;
832 msg("Bad return code from dump: %d\n", status
);
836 } else { /* we are the child; just continue */
838 sleep(4); /* allow time for parent's message to get out */
839 msg("Child on Tape %d has parent %d, my pid = %d\n",
840 tapeno
+1, parentpid
, getpid());
843 * If we have a name like "/dev/rmt0,/dev/rmt1",
844 * use the name before the comma first, and save
845 * the remaining names for subsequent volumes.
847 tapeno
++; /* current tape sequence */
849 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
850 tape
[MAXPATHLEN
- 1] = '\0';
851 msg("Dumping volume %d on %s\n", tapeno
, tape
);
853 else if (nexttape
|| strchr(tapeprefix
, ',')) {
854 if (nexttape
&& *nexttape
)
855 tapeprefix
= nexttape
;
856 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
861 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
862 tape
[MAXPATHLEN
- 1] = '\0';
863 msg("Dumping volume %d on %s\n", tapeno
, tape
);
866 while ((tapefd
= (host
? rmtopen(tape
, 2) : pipeout
?
868 OPEN(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
870 while ((tapefd
= (pipeout
? fileno(stdout
) :
871 OPEN(tape
, O_RDWR
|O_CREAT
, 0666))) < 0)
874 msg("Cannot open output \"%s\".\n", tape
);
875 if (!query("Do you want to retry the open?"))
879 enslave(); /* Share open tape file descriptor with slaves */
884 newtape
++; /* new tape signal */
885 spcl
.c_count
= slp
->count
;
887 * measure firstrec in TP_BSIZE units since restore doesn't
888 * know the correct ntrec value...
890 spcl
.c_firstrec
= slp
->firstrec
;
892 spcl
.c_type
= TS_TAPE
;
893 spcl
.c_flags
|= DR_NEWHEADER
;
894 spcl
.c_ntrec
= ntrec
;
896 spcl
.c_flags
|= DR_COMPRESSED
;
897 writeheader((dump_ino_t
)slp
->inode
);
898 spcl
.c_flags
&=~ DR_NEWHEADER
;
899 msg("Volume %d started with block %ld at: %s", tapeno
,
900 spcl
.c_tapea
, ctime(&tstart_volume
));
902 msg("Volume %d begins with blocks from inode %d\n",
911 if (master
!= 0 && master
!= getpid())
912 /* Signals master to call dumpabort */
913 (void) kill(master
, SIGTERM
);
916 msg("The ENTIRE dump is aborted.\n");
929 msg("pid = %d exits with status %d\n", getpid(), status
);
935 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
941 siglongjmp(jmpbuf
, 1);
949 #ifdef LINUX_FORK_BUG
957 { struct sigaction sa
;
958 memset(&sa
, 0, sizeof sa
);
959 sigemptyset(&sa
.sa_mask
);
960 sa
.sa_handler
= dumpabort
;
961 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
962 sa
.sa_handler
= sigpipe
;
963 sigaction(SIGPIPE
, &sa
, NULL
);
964 sa
.sa_handler
= proceed
;
965 sa
.sa_flags
= SA_RESTART
;
966 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
969 for (i
= 0; i
< SLAVES
; i
++) {
970 if (i
== slp
- &slaves
[0]) {
976 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
977 (slaves
[i
].pid
= fork()) < 0)
978 quit("too many slaves, %d (recompile smaller): %s\n",
981 slaves
[i
].fd
= cmd
[1];
983 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
985 for (j
= 0; j
<= i
; j
++)
986 (void) close(slaves
[j
].fd
);
988 sigaddset(&sigs
, SIGINT
); /* Master handles this */
990 sigaddset(&sigs
, SIGINFO
);
992 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
994 #ifdef LINUX_FORK_BUG
995 if (atomic_write( cmd
[0], (char *) &i
, sizeof i
)
997 quit("master/slave protocol botched 3\n");
1006 #ifdef LINUX_FORK_BUG
1008 * Wait for all slaves to _actually_ start to circumvent a bug in
1009 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1010 * returned from fork() causes a SEGV in the child process
1012 for (i
= 0; i
< SLAVES
; i
++)
1013 if (atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1014 quit("master/slave protocol botched 4\n");
1017 for (i
= 0; i
< SLAVES
; i
++)
1018 (void) atomic_write( slaves
[i
].fd
,
1019 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1020 sizeof slaves
[0].pid
);
1030 for (i
= 0; i
< SLAVES
; i
++)
1031 if (slaves
[i
].pid
> 0) {
1032 (void) kill(slaves
[i
].pid
, SIGKILL
);
1038 * Synchronization - each process waits for a SIGUSR2 from the
1039 * previous process before writing to the tape, and sends SIGUSR2
1040 * to the next process when the tape write completes. On tape errors
1041 * a SIGUSR1 is sent to the master which then terminates all of the
1045 doslave(int cmd
, int slave_number
)
1048 int nextslave
, size
, eot_count
, bufsize
;
1049 volatile int wrote
= 0;
1052 struct tapebuf
*comp_buf
= NULL
;
1053 int compresult
, do_compress
= slave_number
> 0;
1054 unsigned long worklen
;
1055 #endif /* HAVE_ZLIB */
1056 struct slave_results returns
;
1062 union u_spcl
*uspclptr
;
1063 struct s_spcl
*spclptr
;
1064 #endif /* USE_QFA */
1067 * Need our own seek pointer.
1069 (void) close(diskfd
);
1070 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1071 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1074 retval
= dump_fs_open(disk
, &fs
);
1076 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1077 #endif /* __linux__ */
1080 * Need the pid of the next slave in the loop...
1082 if ((nread
= atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1083 != sizeof nextslave
) {
1084 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1088 /* if we're doing a compressed dump, allocate the compress buffer */
1090 comp_buf
= malloc(sizeof(struct tapebuf
) + TP_BSIZE
+ writesize
);
1091 if (comp_buf
== NULL
)
1092 quit("couldn't allocate a compress buffer.\n");
1093 comp_buf
->flags
= 0;
1095 #endif /* HAVE_ZLIB */
1098 * Get list of blocks to dump, read the blocks into tape buffer
1100 while ((nread
= atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1101 register struct req
*p
= slp
->req
;
1103 for (trecno
= 0; trecno
< ntrec
;
1104 trecno
+= p
->count
, p
+= p
->count
) {
1105 if (p
->dblk
) { /* read a disk block */
1106 bread(p
->dblk
, slp
->tblock
[trecno
],
1107 p
->count
* TP_BSIZE
);
1108 } else { /* read record from pipe */
1109 if (p
->count
!= 1 || atomic_read( cmd
,
1110 (char *)slp
->tblock
[trecno
],
1111 TP_BSIZE
) != TP_BSIZE
)
1112 quit("master/slave protocol botched.\n");
1116 /* Try to write the data... */
1120 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1121 bufsize
= writesize
; /* length to write */
1122 returns
.clen
= returns
.unclen
= bufsize
;
1126 * When writing a compressed dump, each block except
1127 * the first one on each tape is written
1128 * from struct tapebuf with an 4 byte prefix
1129 * followed by the data. This can be less than
1130 * writesize. Restore, on a short read, can compare the
1131 * length read to the compressed length in the header
1132 * to verify that the read was good. Blocks which don't
1133 * compress well are written uncompressed.
1134 * The first block written by each slave is not compressed
1135 * and does not have a prefix.
1138 if (compressed
&& do_compress
) {
1139 comp_buf
->length
= bufsize
;
1140 worklen
= TP_BSIZE
+ writesize
;
1141 compresult
= compress2(comp_buf
->buf
, &worklen
,
1142 (char *)slp
->tblock
[0], writesize
, compressed
);
1143 if (compresult
== Z_OK
&& worklen
<= (writesize
- 16)) {
1144 /* write the compressed buffer */
1145 comp_buf
->length
= worklen
;
1146 comp_buf
->compressed
= 1;
1147 buffer
= (char *) comp_buf
;
1148 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1151 /* write the data uncompressed */
1152 comp_buf
->length
= writesize
;
1153 comp_buf
->compressed
= 0;
1154 buffer
= (char *) comp_buf
;
1155 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1156 returns
.unclen
= returns
.clen
;
1157 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1160 /* compress the remaining blocks if we're compressing */
1161 do_compress
= compressed
;
1162 #endif /* HAVE_ZLIB */
1164 if (sigsetjmp(jmpbuf
, 1) == 0) {
1173 if (gTapeposfd
>= 0) {
1174 uspclptr
= (union u_spcl
*)&slp
->tblock
[0];
1175 spclptr
= &uspclptr
->s_spcl
;
1176 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1177 (spclptr
->c_type
== TS_INODE
)) {
1178 /* if an error occured previously don't
1181 if ((gtperr
= GetTapePos(&curtapepos
)) == 0) {
1183 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1185 sprintf(gTps
, "%ld\t%d\t%ld\n", (unsigned long)spclptr
->c_inumber
, tapeno
, curtapepos
);
1186 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != strlen(gTps
)) {
1187 warn("error writing tapepos file.\n");
1193 #endif /* USE_QFA */
1195 while (eot_count
< 10 && size
< bufsize
) {
1198 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1201 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1203 printf("slave %d wrote %d\n", slave_number
, wrote
);
1213 if (size
!= bufsize
)
1214 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1215 slave_number
, size
, bufsize
);
1219 * Handle ENOSPC as an EOT condition.
1221 if (wrote
< 0 && errno
== ENOSPC
) {
1227 returns
.clen
= returns
.unclen
= 0;
1230 * pass errno back to master for special handling
1233 returns
.unclen
= -errno
;
1236 * pass size of data and size of write back to master
1237 * (for EOT handling)
1239 (void) atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1242 * Signal the next slave to go.
1244 (void) kill(nextslave
, SIGUSR2
);
1247 quit("error reading command pipe: %s\n", strerror(errno
));
1251 * Since a read from a pipe may not return all we asked for,
1252 * or a write may not write all we ask if we get a signal,
1253 * loop until the count is satisfied (or error).
1256 atomic_read(int fd
, void *buf
, size_t count
)
1258 int got
, need
= count
;
1261 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1263 } while (got
== -1 && errno
== EINTR
);
1264 return (got
< 0 ? got
: count
- need
);
1268 * Since a read from a pipe may not return all we asked for,
1269 * or a write may not write all we ask if we get a signal,
1270 * loop until the count is satisfied (or error).
1273 atomic_write(int fd
, const void *buf
, size_t count
)
1275 int got
, need
= count
;
1278 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1280 } while (got
== -1 && errno
== EINTR
);
1281 return (got
< 0 ? got
: count
- need
);
1287 * read the current tape position
1290 GetTapePos(long *pos
)
1295 if (ioctl(tapefd
, MTIOCPOS
, pos
) == -1) {
1297 msg("[%ld] error: %d (getting tapepos: %ld)\n", getpid(),
1303 #endif /* USE_QFA */