]>
git.wh0rd.org - dump.git/blob - dump/tape.c
a0910f4f5d5943993d3b7d30f5f1b829e773c169
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.51 2001/07/18 13:12:33 stelian Exp $";
48 #include <compatlfs.h>
54 #include <compaterr.h>
65 #include <sys/types.h>
69 #include <sys/param.h>
70 #include <sys/socket.h>
75 #ifdef HAVE_EXT2FS_EXT2_FS_H
76 #include <ext2fs/ext2_fs.h>
78 #include <linux/ext2_fs.h>
80 #include <ext2fs/ext2fs.h>
81 #include <bsdcompat.h>
83 #include <sys/vnode.h>
86 #include <ufs/inode.h>
88 #include <ufs/ufs/dinode.h>
89 #include <ufs/ffs/fs.h>
90 #endif /* __linux__ */
92 #include <protocols/dumprestore.h>
96 #endif /* HAVE_ZLIB */
100 int writesize
; /* size of malloc()ed buffer for tape */
101 long lastspclrec
= -1; /* tape block number of last written header */
102 int trecno
= 0; /* next record to write in current block */
103 extern long blocksperfile
; /* number of blocks per output file */
104 long blocksthisvol
; /* number of blocks on current output file */
105 extern int ntrec
; /* blocking factor on tape */
106 extern int cartridge
;
111 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
113 static ssize_t atomic_read
__P((int, void *, size_t));
114 static ssize_t atomic_write
__P((int, const void *, size_t));
115 static void doslave
__P((int, int));
116 static void enslave
__P((void));
117 static void flushtape
__P((void));
118 static void killall
__P((void));
119 static void rollforward
__P((void));
122 * Concurrent dump mods (Caltech) - disk block reading and tape writing
123 * are exported to several slave processes. While one slave writes the
124 * tape, the others read disk blocks; they pass control of the tape in
125 * a ring via signals. The parent process traverses the filesystem and
126 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
127 * The following structure defines the instruction packets sent to slaves.
135 struct slave_results
{
136 ssize_t unclen
; /* uncompressed length */
137 ssize_t clen
; /* compressed length */
140 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
142 int tapea
; /* header number at start of this chunk */
143 int count
; /* count to next header (used for TS_TAPE */
145 int inode
; /* inode that we are currently dealing with */
146 int fd
; /* FD for this slave */
147 int pid
; /* PID for this slave */
148 int sent
; /* 1 == we've sent this slave requests */
149 int firstrec
; /* record number of this block */
150 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
151 struct req
*req
; /* buffer for requests */
155 char (*nextblock
)[TP_BSIZE
];
157 static time_t tstart_volume
; /* time of volume start */
158 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
160 int master
; /* pid of master, for sending error signals */
161 int tenths
; /* length of tape overhead per block written */
162 static int caught
; /* have we caught the signal to proceed? */
163 static int ready
; /* have we reached the lock point without having */
164 /* received the SIGUSR2 signal from the prev slave? */
165 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
166 /* SIGUSR2 arrives from the previous slave */
168 static int gtperr
= 0;
174 int pgoff
= getpagesize() - 1;
178 writesize
= ntrec
* TP_BSIZE
;
179 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
181 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
182 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
183 * repositioning after stopping, i.e, streaming mode, where the gap is
184 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
186 if (blocksperfile
== 0 && !unlimited
)
187 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
193 * Allocate tape buffer contiguous with the array of instruction
194 * packets, so flushtape() can write them together with one write().
195 * Align tape buffer on page boundary to speed up tape write().
197 for (i
= 0; i
<= SLAVES
; i
++) {
199 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
202 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
204 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
206 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
208 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
214 nextblock
= slp
->tblock
;
219 writerec(const void *dp
, int isspcl
)
222 slp
->req
[trecno
].dblk
= (daddr_t
)0;
223 slp
->req
[trecno
].count
= 1;
224 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
225 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
228 lastspclrec
= spcl
.c_tapea
;
236 dumpblock(daddr_t blkno
, int size
)
238 int avail
, tpblks
, dblkno
;
240 dblkno
= fsbtodb(sblock
, blkno
);
241 tpblks
= size
>> tp_bshift
;
242 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
243 slp
->req
[trecno
].dblk
= dblkno
;
244 slp
->req
[trecno
].count
= avail
;
246 spcl
.c_tapea
+= avail
;
249 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
261 msg("write error on %s: %s\n", tape
, strerror(errnum
));
262 quit("Cannot recover\n");
265 msg("write error %d blocks into volume %d: %s\n",
266 blocksthisvol
, tapeno
, strerror(errnum
));
267 broadcast("DUMP WRITE ERROR!\n");
268 if (query("Do you want to rewrite this volume?")) {
269 msg("Closing this volume. Prepare to restart with new media;\n");
270 msg("this dump volume will be rewritten.\n");
276 if (query("Do you want to start the next tape?"))
285 quit("Broken pipe\n");
290 * Update xferrate stats
299 ttaken
= tnow
- tstart_volume
;
300 blocks
= spcl
.c_tapea
- tapea_volume
;
301 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
303 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno
,
304 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
306 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
307 long txfrate
= volkb
/ ttaken
;
308 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
309 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
310 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
314 double rate
= .0005 + (double) blocks
/ (double) volkb
;
315 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
317 tapeno
, blocks
, volkb
, rate
);
324 mktimeest(time_t tnow
)
326 static char msgbuf
[128];
331 if (blockswritten
< 500)
333 if (blockswritten
> tapesize
)
334 tapesize
= blockswritten
;
335 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
336 / blockswritten
* tapesize
;
337 if (tnow
> tstart_volume
)
338 (void)snprintf(msgbuf
, sizeof(msgbuf
),
339 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
340 (blockswritten
* 100.0) / tapesize
,
341 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
342 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
344 (void)snprintf(msgbuf
, sizeof(msgbuf
),
345 "%3.2f%% done, finished in %d:%02d\n",
346 (blockswritten
* 100.0) / tapesize
,
347 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
355 * information message upon receipt of SIGINFO
358 statussig(int notused
)
360 int save_errno
= errno
;
363 buf
= mktimeest(time(NULL
));
365 write(STDERR_FILENO
, buf
, strlen(buf
));
375 struct slave_results returned
;
377 int siz
= (char *)nextblock
- (char *)slp
->req
;
379 slp
->req
[trecno
].count
= 0; /* Sentinel */
381 if (atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
382 quit("error writing command pipe: %s\n", strerror(errno
));
383 slp
->sent
= 1; /* we sent a request, read the response later */
385 lastfirstrec
= slp
->firstrec
;
387 if (++slp
>= &slaves
[SLAVES
])
390 /* Read results back from next slave */
392 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
393 != sizeof returned
) {
394 perror(" DUMP: error reading command pipe in master");
397 got
= returned
.unclen
;
398 bytes_written
+= returned
.clen
;
399 if (returned
.unclen
== returned
.clen
)
403 /* Check for errors or end of tape */
405 /* Check for errors */
409 msg("End of tape detected\n");
412 * Drain the results, don't care what the values were.
413 * If we read them here then trewind won't...
415 for (i
= 0; i
< SLAVES
; i
++) {
416 if (slaves
[i
].sent
) {
417 if (atomic_read( slaves
[i
].fd
,
418 (char *)&returned
, sizeof returned
)
419 != sizeof returned
) {
420 perror(" DUMP: error reading command pipe in master");
434 if (spcl
.c_type
!= TS_END
) {
435 for (i
= 0; i
< spcl
.c_count
; i
++)
436 if (spcl
.c_addr
[i
] != 0)
439 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
440 slp
->tapea
= spcl
.c_tapea
;
441 slp
->firstrec
= lastfirstrec
+ ntrec
;
443 nextblock
= slp
->tblock
;
445 asize
+= tenths
+ returned
.clen
/ density
;
446 csize
+= returned
.clen
;
447 blockswritten
+= ntrec
;
448 blocksthisvol
+= ntrec
;
449 if (!pipeout
&& !unlimited
) {
451 if ( compressed
? csize
>= blocksperfile
* 1024
452 : blocksthisvol
>= blocksperfile
) {
457 else if (asize
> tsize
) {
470 struct slave_results returned
;
472 for (f
= 0; f
< SLAVES
; f
++) {
474 * Drain the results, but unlike EOT we DO (or should) care
475 * what the return values were, since if we detect EOT after
476 * we think we've written the last blocks to the tape anyway,
477 * we have to replay those blocks with rollforward.
479 * fixme: punt for now.
481 if (slaves
[f
].sent
) {
482 if (atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
483 != sizeof returned
) {
484 perror(" DUMP: error reading command pipe in master");
487 got
= returned
.unclen
;
488 bytes_written
+= returned
.clen
;
489 if (returned
.unclen
== returned
.clen
)
497 msg("EOT detected in last 2 tape records!\n");
498 msg("Use a longer tape, decrease the size estimate\n");
499 quit("or use no size estimate at all.\n");
502 (void) close(slaves
[f
].fd
);
504 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
509 msg("Closing %s\n", tape
);
514 while (rmtopen(tape
, 0) < 0)
521 (void) close(tapefd
);
523 while ((f
= OPEN(tape
, 0)) < 0)
529 if (eot_script
&& spcl
.c_type
!= TS_END
) {
530 msg("Launching %s\n", eot_script
);
531 eot_code
= system_command(eot_script
, tape
, tapeno
);
533 if (eot_code
!= 0 && eot_code
!= 1) {
534 msg("Dump aborted by the end of tape script\n");
546 if (nexttape
|| Mflag
|| (eot_code
== 0) )
549 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
550 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
552 while (!query("Is the new volume mounted and ready to go?"))
553 if (query("Do you want to abort?")) {
562 register struct req
*p
, *q
, *prev
;
563 register struct slave
*tslp
;
564 int i
, size
, savedtapea
, got
;
565 union u_spcl
*ntb
, *otb
;
566 struct slave_results returned
;
571 tslp
= &slaves
[SLAVES
];
572 ntb
= (union u_spcl
*)tslp
->tblock
[1];
575 * Each of the N slaves should have requests that need to
576 * be replayed on the next tape. Use the extra slave buffers
577 * (slaves[SLAVES]) to construct request lists to be sent to
578 * each slave in turn.
580 for (i
= 0; i
< SLAVES
; i
++) {
582 otb
= (union u_spcl
*)slp
->tblock
;
585 * For each request in the current slave, copy it to tslp.
589 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
592 *ntb
++ = *otb
++; /* copy the datablock also */
597 quit("rollforward: protocol botch");
609 nextblock
= tslp
->tblock
;
610 savedtapea
= spcl
.c_tapea
;
611 spcl
.c_tapea
= slp
->tapea
;
613 spcl
.c_tapea
= savedtapea
;
614 lastspclrec
= savedtapea
- 1;
616 size
= (char *)ntb
- (char *)q
;
617 if (atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
618 perror(" DUMP: error writing command pipe");
623 lastfirstrec
= slp
->firstrec
;
625 if (++slp
>= &slaves
[SLAVES
])
630 if (prev
->dblk
!= 0) {
632 * If the last one was a disk block, make the
633 * first of this one be the last bit of that disk
636 q
->dblk
= prev
->dblk
+
637 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
638 ntb
= (union u_spcl
*)tslp
->tblock
;
641 * It wasn't a disk block. Copy the data to its
642 * new location in the buffer.
645 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
646 ntb
= (union u_spcl
*)tslp
->tblock
[1];
650 nextblock
= slp
->tblock
;
653 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
654 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
661 * Clear the first slaves' response. One hopes that it
662 * worked ok, otherwise the tape is much too short!
665 if (atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
666 != sizeof returned
) {
667 perror(" DUMP: error reading command pipe in master");
670 got
= returned
.unclen
;
671 bytes_written
+= returned
.clen
;
672 if (returned
.clen
== returned
.unclen
)
680 quit("EOT detected at start of the tape!\n");
686 if (spcl
.c_type
!= TS_END
) {
687 for (i
= 0; i
< spcl
.c_count
; i
++)
688 if (spcl
.c_addr
[i
] != 0)
692 slp
->firstrec
= lastfirstrec
+ ntrec
;
693 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
695 asize
+= tenths
+ returned
.clen
/ density
;
696 csize
+= returned
.clen
;
697 blockswritten
+= ntrec
;
698 blocksthisvol
+= ntrec
;
703 * We implement taking and restoring checkpoints on the tape level.
704 * When each tape is opened, a new process is created by forking; this
705 * saves all of the necessary context in the parent. The child
706 * continues the dump; the parent waits around, saving the context.
707 * If the child returns X_REWRITE, then it had problems writing that tape;
708 * this causes the parent to fork again, duplicating the context, and
709 * everything continues as if nothing had happened.
712 startnewtape(int top
)
723 sigaddset(&sigs
, SIGINT
);
724 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
725 #else /* __linux__ */
727 void (*interrupt_save
)();
729 sig_t interrupt_save
;
731 interrupt_save
= signal(SIGINT
, SIG_IGN
);
732 #endif /* __linux__ */
734 parentpid
= getpid();
735 tapea_volume
= spcl
.c_tapea
;
736 tapea_bytes
= bytes_written
;
737 tstart_volume
= time(NULL
);
741 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
743 (void)signal(SIGINT
, interrupt_save
);
746 * All signals are inherited...
750 msg("Context save fork fails in parent %d\n", parentpid
);
756 * save the context by waiting
757 * until the child doing all of the work returns.
758 * don't catch the interrupt
761 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
763 signal(SIGINT
, SIG_IGN
);
766 msg("Tape: %d; parent process: %d child process %d\n",
767 tapeno
+1, parentpid
, childpid
);
769 while ((waitpid
= wait(&status
)) != childpid
)
770 if (waitpid
!= rshpid
)
771 msg("Parent %d waiting for child %d has another child %d return\n",
772 parentpid
, childpid
, waitpid
);
774 msg("Child %d returns LOB status %o\n",
775 childpid
, status
&0xFF);
777 status
= (status
>> 8) & 0xFF;
781 msg("Child %d finishes X_FINOK\n", childpid
);
784 msg("Child %d finishes X_ABORT\n", childpid
);
787 msg("Child %d finishes X_REWRITE\n", childpid
);
790 msg("Child %d finishes unknown %d\n",
801 goto restore_check_point
;
803 msg("Bad return code from dump: %d\n", status
);
807 } else { /* we are the child; just continue */
809 sleep(4); /* allow time for parent's message to get out */
810 msg("Child on Tape %d has parent %d, my pid = %d\n",
811 tapeno
+1, parentpid
, getpid());
814 * If we have a name like "/dev/rmt0,/dev/rmt1",
815 * use the name before the comma first, and save
816 * the remaining names for subsequent volumes.
818 tapeno
++; /* current tape sequence */
820 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
821 tape
[MAXPATHLEN
- 1] = '\0';
822 msg("Dumping volume %d on %s\n", tapeno
, tape
);
824 else if (nexttape
|| strchr(tapeprefix
, ',')) {
825 if (nexttape
&& *nexttape
)
826 tapeprefix
= nexttape
;
827 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
832 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
833 tape
[MAXPATHLEN
- 1] = '\0';
834 msg("Dumping volume %d on %s\n", tapeno
, tape
);
837 while ((tapefd
= (host
? rmtopen(tape
, 2) : pipeout
?
839 OPEN(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
841 while ((tapefd
= (pipeout
? fileno(stdout
) :
842 OPEN(tape
, O_RDWR
|O_CREAT
, 0666))) < 0)
845 msg("Cannot open output \"%s\".\n", tape
);
846 if (!query("Do you want to retry the open?"))
850 enslave(); /* Share open tape file descriptor with slaves */
856 newtape
++; /* new tape signal */
857 spcl
.c_count
= slp
->count
;
859 * measure firstrec in TP_BSIZE units since restore doesn't
860 * know the correct ntrec value...
862 spcl
.c_firstrec
= slp
->firstrec
;
864 spcl
.c_type
= TS_TAPE
;
865 spcl
.c_flags
|= DR_NEWHEADER
;
866 spcl
.c_ntrec
= ntrec
;
868 spcl
.c_flags
|= DR_COMPRESSED
;
869 writeheader((dump_ino_t
)slp
->inode
);
870 spcl
.c_flags
&=~ DR_NEWHEADER
;
871 msg("Volume %d started with block %ld at: %s", tapeno
,
872 spcl
.c_tapea
, ctime(&tstart_volume
));
874 msg("Volume %d begins with blocks from inode %d\n",
883 if (master
!= 0 && master
!= getpid())
884 /* Signals master to call dumpabort */
885 (void) kill(master
, SIGTERM
);
888 msg("The ENTIRE dump is aborted.\n");
901 msg("pid = %d exits with status %d\n", getpid(), status
);
907 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
913 siglongjmp(jmpbuf
, 1);
921 #ifdef LINUX_FORK_BUG
929 { struct sigaction sa
;
930 memset(&sa
, 0, sizeof sa
);
931 sigemptyset(&sa
.sa_mask
);
932 sa
.sa_handler
= dumpabort
;
933 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
934 sa
.sa_handler
= sigpipe
;
935 sigaction(SIGPIPE
, &sa
, NULL
);
936 sa
.sa_handler
= proceed
;
937 sa
.sa_flags
= SA_RESTART
;
938 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
941 for (i
= 0; i
< SLAVES
; i
++) {
942 if (i
== slp
- &slaves
[0]) {
948 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
949 (slaves
[i
].pid
= fork()) < 0)
950 quit("too many slaves, %d (recompile smaller): %s\n",
953 slaves
[i
].fd
= cmd
[1];
955 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
957 for (j
= 0; j
<= i
; j
++)
958 (void) close(slaves
[j
].fd
);
960 sigaddset(&sigs
, SIGINT
); /* Master handles this */
962 sigaddset(&sigs
, SIGINFO
);
964 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
966 #ifdef LINUX_FORK_BUG
967 if (atomic_write( cmd
[0], (char *) &i
, sizeof i
)
969 quit("master/slave protocol botched 3\n");
978 #ifdef LINUX_FORK_BUG
980 * Wait for all slaves to _actually_ start to circumvent a bug in
981 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
982 * returned from fork() causes a SEGV in the child process
984 for (i
= 0; i
< SLAVES
; i
++)
985 if (atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
986 quit("master/slave protocol botched 4\n");
989 for (i
= 0; i
< SLAVES
; i
++)
990 (void) atomic_write( slaves
[i
].fd
,
991 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
992 sizeof slaves
[0].pid
);
1002 for (i
= 0; i
< SLAVES
; i
++)
1003 if (slaves
[i
].pid
> 0) {
1004 (void) kill(slaves
[i
].pid
, SIGKILL
);
1010 * Synchronization - each process waits for a SIGUSR2 from the
1011 * previous process before writing to the tape, and sends SIGUSR2
1012 * to the next process when the tape write completes. On tape errors
1013 * a SIGUSR1 is sent to the master which then terminates all of the
1017 doslave(int cmd
, int slave_number
)
1020 int nextslave
, size
, eot_count
, bufsize
;
1021 volatile int wrote
= 0;
1024 struct tapebuf
*comp_buf
= NULL
;
1025 int compresult
, do_compress
= slave_number
> 0;
1026 unsigned long worklen
;
1027 #endif /* HAVE_ZLIB */
1028 struct slave_results returns
;
1034 union u_spcl
*uspclptr
;
1035 struct s_spcl
*spclptr
;
1036 #endif /* USE_QFA */
1039 * Need our own seek pointer.
1041 (void) close(diskfd
);
1042 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1043 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1046 retval
= dump_fs_open(disk
, &fs
);
1048 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1049 #endif /* __linux__ */
1052 * Need the pid of the next slave in the loop...
1054 if ((nread
= atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1055 != sizeof nextslave
) {
1056 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1060 /* if we're doing a compressed dump, allocate the compress buffer */
1062 comp_buf
= malloc(sizeof(struct tapebuf
) + TP_BSIZE
+ writesize
);
1063 if (comp_buf
== NULL
)
1064 quit("couldn't allocate a compress buffer.\n");
1065 comp_buf
->flags
= 0;
1067 #endif /* HAVE_ZLIB */
1070 * Get list of blocks to dump, read the blocks into tape buffer
1072 while ((nread
= atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1073 register struct req
*p
= slp
->req
;
1075 for (trecno
= 0; trecno
< ntrec
;
1076 trecno
+= p
->count
, p
+= p
->count
) {
1077 if (p
->dblk
) { /* read a disk block */
1078 bread(p
->dblk
, slp
->tblock
[trecno
],
1079 p
->count
* TP_BSIZE
);
1080 } else { /* read record from pipe */
1081 if (p
->count
!= 1 || atomic_read( cmd
,
1082 (char *)slp
->tblock
[trecno
],
1083 TP_BSIZE
) != TP_BSIZE
)
1084 quit("master/slave protocol botched.\n");
1088 /* Try to write the data... */
1092 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1093 bufsize
= writesize
; /* length to write */
1094 returns
.clen
= returns
.unclen
= bufsize
;
1098 * When writing a compressed dump, each block except
1099 * the first one on each tape is written
1100 * from struct tapebuf with an 4 byte prefix
1101 * followed by the data. This can be less than
1102 * writesize. Restore, on a short read, can compare the
1103 * length read to the compressed length in the header
1104 * to verify that the read was good. Blocks which don't
1105 * compress well are written uncompressed.
1106 * The first block written by each slave is not compressed
1107 * and does not have a prefix.
1110 if (compressed
&& do_compress
) {
1111 comp_buf
->length
= bufsize
;
1112 worklen
= TP_BSIZE
+ writesize
;
1113 compresult
= compress2(comp_buf
->buf
, &worklen
,
1114 (char *)slp
->tblock
[0], writesize
, compressed
);
1115 if (compresult
== Z_OK
&& worklen
<= (writesize
- 16)) {
1116 /* write the compressed buffer */
1117 comp_buf
->length
= worklen
;
1118 comp_buf
->compressed
= 1;
1119 buffer
= (char *) comp_buf
;
1120 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1123 /* write the data uncompressed */
1124 comp_buf
->length
= writesize
;
1125 comp_buf
->compressed
= 0;
1126 buffer
= (char *) comp_buf
;
1127 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1128 returns
.unclen
= returns
.clen
;
1129 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1132 /* compress the remaining blocks if we're compressing */
1133 do_compress
= compressed
;
1134 #endif /* HAVE_ZLIB */
1136 if (sigsetjmp(jmpbuf
, 1) == 0) {
1145 if (gTapeposfd
>= 0) {
1146 uspclptr
= (union u_spcl
*)&slp
->tblock
[0];
1147 spclptr
= &uspclptr
->s_spcl
;
1148 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1149 (spclptr
->c_type
== TS_INODE
) &&
1150 (spclptr
->c_date
== gThisDumpDate
)) {
1151 /* if an error occured previously don't
1154 if ((gtperr
= GetTapePos(&curtapepos
)) == 0) {
1156 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1158 sprintf(gTps
, "%ld\t%d\t%ld\n", (unsigned long)spclptr
->c_inumber
, tapeno
, curtapepos
);
1159 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != strlen(gTps
)) {
1160 warn("error writing tapepos file.\n");
1166 #endif /* USE_QFA */
1168 while (eot_count
< 10 && size
< bufsize
) {
1171 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1174 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1176 printf("slave %d wrote %d\n", slave_number
, wrote
);
1186 if (size
!= bufsize
)
1187 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1188 slave_number
, size
, bufsize
);
1192 * Handle ENOSPC as an EOT condition.
1194 if (wrote
< 0 && errno
== ENOSPC
) {
1200 returns
.clen
= returns
.unclen
= 0;
1203 * pass errno back to master for special handling
1206 returns
.unclen
= -errno
;
1209 * pass size of data and size of write back to master
1210 * (for EOT handling)
1212 (void) atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1215 * Signal the next slave to go.
1217 (void) kill(nextslave
, SIGUSR2
);
1220 quit("error reading command pipe: %s\n", strerror(errno
));
1224 * Since a read from a pipe may not return all we asked for,
1225 * or a write may not write all we ask if we get a signal,
1226 * loop until the count is satisfied (or error).
1229 atomic_read(int fd
, void *buf
, size_t count
)
1231 int got
, need
= count
;
1234 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1236 } while (got
== -1 && errno
== EINTR
);
1237 return (got
< 0 ? got
: count
- need
);
1241 * Since a read from a pipe may not return all we asked for,
1242 * or a write may not write all we ask if we get a signal,
1243 * loop until the count is satisfied (or error).
1246 atomic_write(int fd
, const void *buf
, size_t count
)
1248 int got
, need
= count
;
1251 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1253 } while (got
== -1 && errno
== EINTR
);
1254 return (got
< 0 ? got
: count
- need
);
1260 * read the current tape position
1263 GetTapePos(long *pos
)
1268 if (ioctl(tapefd
, MTIOCPOS
, pos
) == -1) {
1270 msg("[%ld] error: %d (getting tapepos: %ld)\n", getpid(),
1276 #endif /* USE_QFA */