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 <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char rcsid
[] =
40 "$Id: tape.c,v 1.75 2003/03/31 09:42:58 stelian Exp $";
44 #include <compatlfs.h>
50 #include <compaterr.h>
61 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <sys/socket.h>
70 #ifdef HAVE_EXT2FS_EXT2_FS_H
71 #include <ext2fs/ext2_fs.h>
73 #include <linux/ext2_fs.h>
75 #include <ext2fs/ext2fs.h>
77 #include <bsdcompat.h>
79 #include <sys/vnode.h>
82 #include <ufs/inode.h>
84 #include <ufs/ufs/dinode.h>
85 #include <ufs/ffs/fs.h>
86 #endif /* __linux__ */
88 #include <protocols/dumprestore.h>
92 #endif /* HAVE_ZLIB */
96 #endif /* HAVE_BZLIB */
100 #endif /* HAVE_LZO */
104 int writesize
; /* size of malloc()ed buffer for tape */
105 long lastspclrec
= -1; /* tape block number of last written header */
106 int trecno
= 0; /* next record to write in current block */
107 extern long blocksperfile
; /* number of blocks per output file */
108 long blocksthisvol
; /* number of blocks on current output file */
109 extern int ntrec
; /* blocking factor on tape */
110 extern int cartridge
;
113 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
114 static int magtapeout
; /* output is really a tape */
116 static ssize_t dump_atomic_read
__P((int, void *, size_t));
117 static ssize_t dump_atomic_write
__P((int, const void *, size_t));
119 static void doslave
__P((int, int, int));
121 static void doslave
__P((int, int));
123 static void enslave
__P((void));
124 static void flushtape
__P((void));
125 static void killall
__P((void));
126 static void rollforward
__P((void));
128 static int GetTapePos
__P((long long *));
129 static void MkTapeString
__P((struct s_spcl
*, long long));
133 * Concurrent dump mods (Caltech) - disk block reading and tape writing
134 * are exported to several slave processes. While one slave writes the
135 * tape, the others read disk blocks; they pass control of the tape in
136 * a ring via signals. The parent process traverses the filesystem and
137 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
138 * The following structure defines the instruction packets sent to slaves.
146 struct slave_results
{
147 ssize_t unclen
; /* uncompressed length */
148 ssize_t clen
; /* compressed length */
151 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
153 int tapea
; /* header number at start of this chunk */
154 int count
; /* count to next header (used for TS_TAPE */
156 int inode
; /* inode that we are currently dealing with */
157 int fd
; /* FD for this slave */
158 int pid
; /* PID for this slave */
159 int sent
; /* 1 == we've sent this slave requests */
160 int firstrec
; /* record number of this block */
161 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
162 struct req
*req
; /* buffer for requests */
166 char (*nextblock
)[TP_BSIZE
];
168 static time_t tstart_volume
; /* time of volume start */
169 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
171 int master
; /* pid of master, for sending error signals */
172 int tenths
; /* length of tape overhead per block written */
173 static int caught
; /* have we caught the signal to proceed? */
174 static int ready
; /* have we reached the lock point without having */
175 /* received the SIGUSR2 signal from the prev slave? */
176 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
177 /* SIGUSR2 arrives from the previous slave */
179 static int gtperr
= 0;
185 int pgoff
= getpagesize() - 1;
189 writesize
= ntrec
* TP_BSIZE
;
190 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
192 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
193 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
194 * repositioning after stopping, i.e, streaming mode, where the gap is
195 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
197 if (blocksperfile
== 0 && !unlimited
)
198 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
204 * Allocate tape buffer contiguous with the array of instruction
205 * packets, so flushtape() can write them together with one write().
206 * Align tape buffer on page boundary to speed up tape write().
208 for (i
= 0; i
<= SLAVES
; i
++) {
210 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
213 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
215 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
217 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
219 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
225 nextblock
= slp
->tblock
;
230 writerec(const void *dp
, int isspcl
)
233 slp
->req
[trecno
].dblk
= (daddr_t
)0;
234 slp
->req
[trecno
].count
= 1;
235 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
236 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
238 /* Need to write it to the archive file */
239 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
241 if (AfileActive
&& Afile
>= 0) {
242 /* When we dump an inode which is not a directory,
243 * it means we ended the archive contents */
244 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
245 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
249 tmp
= *(union u_spcl
*)dp
;
250 /* Write the record, _uncompressed_ */
252 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
255 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
256 msg("error writing archive file: %s\n",
263 lastspclrec
= spcl
.c_tapea
;
271 dumpblock(daddr_t blkno
, int size
)
273 int avail
, tpblks
, dblkno
;
275 dblkno
= fsbtodb(sblock
, blkno
);
276 tpblks
= size
>> tp_bshift
;
277 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
278 slp
->req
[trecno
].dblk
= dblkno
;
279 slp
->req
[trecno
].count
= avail
;
281 spcl
.c_tapea
+= avail
;
284 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
296 msg("write error on %s: %s\n", tape
, strerror(errnum
));
297 quit("Cannot recover\n");
300 msg("write error %d blocks into volume %d: %s\n",
301 blocksthisvol
, tapeno
, strerror(errnum
));
302 broadcast("DUMP WRITE ERROR!\n");
303 if (query("Do you want to rewrite this volume?")) {
304 msg("Closing this volume. Prepare to restart with new media;\n");
305 msg("this dump volume will be rewritten.\n");
311 if (query("Do you want to start the next tape?"))
317 sigpipe(UNUSED(int signo
))
320 quit("Broken pipe\n");
325 * Update xferrate stats
334 ttaken
= tnow
- tstart_volume
;
335 blocks
= spcl
.c_tapea
- tapea_volume
;
336 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
338 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
339 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
341 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
342 long txfrate
= volkb
/ ttaken
;
343 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
344 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
345 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
349 double rate
= .0005 + (double) blocks
/ (double) volkb
;
350 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
352 tapeno
, blocks
, volkb
, rate
);
359 mktimeest(time_t tnow
)
361 static char msgbuf
[128];
366 if (blockswritten
< 500)
368 if (blockswritten
> tapesize
)
369 tapesize
= blockswritten
;
370 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
371 / blockswritten
* tapesize
;
372 if (tnow
> tstart_volume
)
373 (void)snprintf(msgbuf
, sizeof(msgbuf
),
374 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
375 (blockswritten
* 100.0) / tapesize
,
376 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
377 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
379 (void)snprintf(msgbuf
, sizeof(msgbuf
),
380 "%3.2f%% done, finished in %d:%02d\n",
381 (blockswritten
* 100.0) / tapesize
,
382 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
390 * information message upon receipt of SIGINFO
393 statussig(int notused
)
395 int save_errno
= errno
;
398 buf
= mktimeest(time(NULL
));
400 write(STDERR_FILENO
, buf
, strlen(buf
));
410 struct slave_results returned
;
412 int siz
= (char *)nextblock
- (char *)slp
->req
;
414 slp
->req
[trecno
].count
= 0; /* Sentinel */
416 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
417 quit("error writing command pipe: %s\n", strerror(errno
));
418 slp
->sent
= 1; /* we sent a request, read the response later */
420 lastfirstrec
= slp
->firstrec
;
422 if (++slp
>= &slaves
[SLAVES
])
425 /* Read results back from next slave */
427 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
428 != sizeof returned
) {
429 perror(" DUMP: error reading command pipe in master");
432 got
= returned
.unclen
;
433 bytes_written
+= returned
.clen
;
434 if (returned
.unclen
== returned
.clen
)
438 /* Check for errors or end of tape */
440 /* Check for errors */
444 msg("End of tape detected\n");
447 * Drain the results, don't care what the values were.
448 * If we read them here then trewind won't...
450 for (i
= 0; i
< SLAVES
; i
++) {
451 if (slaves
[i
].sent
) {
452 if (dump_atomic_read( slaves
[i
].fd
,
453 (char *)&returned
, sizeof returned
)
454 != sizeof returned
) {
455 perror(" DUMP: error reading command pipe in master");
469 if (spcl
.c_type
!= TS_END
) {
470 for (i
= 0; i
< spcl
.c_count
; i
++)
471 if (spcl
.c_addr
[i
] != 0)
474 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
475 slp
->tapea
= spcl
.c_tapea
;
476 slp
->firstrec
= lastfirstrec
+ ntrec
;
478 nextblock
= slp
->tblock
;
480 asize
+= tenths
+ returned
.clen
/ density
;
481 blockswritten
+= ntrec
;
482 blocksthisvol
+= ntrec
;
483 if (!pipeout
&& !unlimited
) {
485 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfile
) * 1024)
486 : blocksthisvol
>= blocksperfile
) {
491 else if (asize
> tsize
) {
504 struct slave_results returned
;
506 for (f
= 0; f
< SLAVES
; f
++) {
508 * Drain the results, but unlike EOT we DO (or should) care
509 * what the return values were, since if we detect EOT after
510 * we think we've written the last blocks to the tape anyway,
511 * we have to replay those blocks with rollforward.
513 * fixme: punt for now.
515 if (slaves
[f
].sent
) {
516 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
517 != sizeof returned
) {
518 perror(" DUMP: error reading command pipe in master");
521 got
= returned
.unclen
;
522 bytes_written
+= returned
.clen
;
523 if (returned
.unclen
== returned
.clen
)
531 msg("EOT detected in last 2 tape records!\n");
532 msg("Use a longer tape, decrease the size estimate\n");
533 quit("or use no size estimate at all.\n");
536 (void) close(slaves
[f
].fd
);
538 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
543 msg("Closing %s\n", tape
);
548 while (rmtopen(tape
, O_RDONLY
) < 0)
555 (void) close(tapefd
);
557 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
572 if (nexttape
|| Mflag
)
575 msg("Launching %s\n", eot_script
);
576 eot_code
= system_command(eot_script
, tape
, tapeno
);
578 if (eot_code
!= 0 && eot_code
!= 1) {
579 msg("Dump aborted by the end of tape script\n");
585 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
586 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
588 while (!query("Is the new volume mounted and ready to go?"))
589 if (query("Do you want to abort?")) {
598 struct req
*p
, *q
= NULL
, *prev
;
600 int i
, size
, savedtapea
, got
;
601 union u_spcl
*ntb
, *otb
;
602 struct slave_results returned
;
607 tslp
= &slaves
[SLAVES
];
608 ntb
= (union u_spcl
*)tslp
->tblock
[1];
611 * Each of the N slaves should have requests that need to
612 * be replayed on the next tape. Use the extra slave buffers
613 * (slaves[SLAVES]) to construct request lists to be sent to
614 * each slave in turn.
616 for (i
= 0; i
< SLAVES
; i
++) {
618 otb
= (union u_spcl
*)slp
->tblock
;
621 * For each request in the current slave, copy it to tslp.
625 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
628 *ntb
++ = *otb
++; /* copy the datablock also */
633 quit("rollforward: protocol botch");
645 nextblock
= tslp
->tblock
;
646 savedtapea
= spcl
.c_tapea
;
647 spcl
.c_tapea
= slp
->tapea
;
649 spcl
.c_tapea
= savedtapea
;
650 lastspclrec
= savedtapea
- 1;
652 size
= (char *)ntb
- (char *)q
;
653 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
654 perror(" DUMP: error writing command pipe");
659 lastfirstrec
= slp
->firstrec
;
661 if (++slp
>= &slaves
[SLAVES
])
666 if (prev
->dblk
!= 0) {
668 * If the last one was a disk block, make the
669 * first of this one be the last bit of that disk
672 q
->dblk
= prev
->dblk
+
673 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
674 ntb
= (union u_spcl
*)tslp
->tblock
;
677 * It wasn't a disk block. Copy the data to its
678 * new location in the buffer.
681 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
682 ntb
= (union u_spcl
*)tslp
->tblock
[1];
686 nextblock
= slp
->tblock
;
689 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
690 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
697 * Clear the first slaves' response. One hopes that it
698 * worked ok, otherwise the tape is much too short!
701 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
702 != sizeof returned
) {
703 perror(" DUMP: error reading command pipe in master");
706 got
= returned
.unclen
;
707 bytes_written
+= returned
.clen
;
708 if (returned
.clen
== returned
.unclen
)
716 quit("EOT detected at start of the tape!\n");
722 if (spcl
.c_type
!= TS_END
) {
723 for (i
= 0; i
< spcl
.c_count
; i
++)
724 if (spcl
.c_addr
[i
] != 0)
728 slp
->firstrec
= lastfirstrec
+ ntrec
;
729 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
731 asize
+= tenths
+ returned
.clen
/ density
;
732 blockswritten
+= ntrec
;
733 blocksthisvol
+= ntrec
;
738 * We implement taking and restoring checkpoints on the tape level.
739 * When each tape is opened, a new process is created by forking; this
740 * saves all of the necessary context in the parent. The child
741 * continues the dump; the parent waits around, saving the context.
742 * If the child returns X_REWRITE, then it had problems writing that tape;
743 * this causes the parent to fork again, duplicating the context, and
744 * everything continues as if nothing had happened.
747 startnewtape(int top
)
758 sigaddset(&sigs
, SIGINT
);
759 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
760 #else /* __linux__ */
762 void (*interrupt_save
)();
764 sig_t interrupt_save
;
766 interrupt_save
= signal(SIGINT
, SIG_IGN
);
767 #endif /* __linux__ */
769 parentpid
= getpid();
770 tapea_volume
= spcl
.c_tapea
;
771 tapea_bytes
= bytes_written
;
772 tstart_volume
= time(NULL
);
776 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
778 (void)signal(SIGINT
, interrupt_save
);
781 * All signals are inherited...
785 msg("Context save fork fails in parent %d\n", parentpid
);
791 * save the context by waiting
792 * until the child doing all of the work returns.
793 * don't catch the interrupt
796 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
798 signal(SIGINT
, SIG_IGN
);
801 msg("Tape: %d; parent process: %d child process %d\n",
802 tapeno
+1, parentpid
, childpid
);
804 while ((waitpid
= wait(&status
)) != childpid
)
805 if (waitpid
!= rshpid
)
806 msg("Parent %d waiting for child %d has another child %d return\n",
807 parentpid
, childpid
, waitpid
);
809 msg("Child %d returns LOB status %o\n",
810 childpid
, status
&0xFF);
812 status
= (status
>> 8) & 0xFF;
816 msg("Child %d finishes X_FINOK\n", childpid
);
819 msg("Child %d finishes X_ABORT\n", childpid
);
822 msg("Child %d finishes X_REWRITE\n", childpid
);
825 msg("Child %d finishes unknown %d\n",
836 goto restore_check_point
;
838 msg("Bad return code from dump: %d\n", status
);
842 } else { /* we are the child; just continue */
844 sleep(4); /* allow time for parent's message to get out */
845 msg("Child on Tape %d has parent %d, my pid = %d\n",
846 tapeno
+1, parentpid
, getpid());
849 * If we have a name like "/dev/rmt0,/dev/rmt1",
850 * use the name before the comma first, and save
851 * the remaining names for subsequent volumes.
853 tapeno
++; /* current tape sequence */
855 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
856 tape
[MAXPATHLEN
- 1] = '\0';
857 msg("Dumping volume %d on %s\n", tapeno
, tape
);
859 else if (nexttape
|| strchr(tapeprefix
, ',')) {
860 if (nexttape
&& *nexttape
)
861 tapeprefix
= nexttape
;
862 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
867 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
868 tape
[MAXPATHLEN
- 1] = '\0';
869 msg("Dumping volume %d on %s\n", tapeno
, tape
);
872 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
874 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
876 while ((tapefd
= (pipeout
? fileno(stdout
) :
877 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
880 msg("Cannot open output \"%s\": %s\n", tape
,
882 if (!query("Do you want to retry the open?"))
889 struct mtget mt_stat
;
890 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
892 msg("Output is to %s\n",
893 magtapeout ? "tape" : "file/pipe");
897 enslave(); /* Share open tape file descriptor with slaves */
902 newtape
++; /* new tape signal */
903 spcl
.c_count
= slp
->count
;
905 * measure firstrec in TP_BSIZE units since restore doesn't
906 * know the correct ntrec value...
908 spcl
.c_firstrec
= slp
->firstrec
;
910 spcl
.c_type
= TS_TAPE
;
911 spcl
.c_flags
|= DR_NEWHEADER
;
912 spcl
.c_ntrec
= ntrec
;
914 spcl
.c_flags
|= DR_COMPRESSED
;
915 writeheader((dump_ino_t
)slp
->inode
);
916 spcl
.c_flags
&=~ DR_NEWHEADER
;
917 msg("Volume %d started with block %ld at: %s", tapeno
,
918 spcl
.c_tapea
, ctime(&tstart_volume
));
920 msg("Volume %d begins with blocks from inode %d\n",
922 if (tapeno
< (int)TP_NINOS
)
923 volinfo
[tapeno
] = slp
->inode
;
928 dumpabort(UNUSED(int signo
))
931 if (master
!= 0 && master
!= getpid())
932 /* Signals master to call dumpabort */
933 (void) kill(master
, SIGTERM
);
936 msg("The ENTIRE dump is aborted.\n");
949 msg("pid = %d exits with status %d\n", getpid(), status
);
955 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
958 proceed(UNUSED(int signo
))
961 siglongjmp(jmpbuf
, 1);
969 #ifdef LINUX_FORK_BUG
977 { struct sigaction sa
;
978 memset(&sa
, 0, sizeof sa
);
979 sigemptyset(&sa
.sa_mask
);
980 sa
.sa_handler
= dumpabort
;
981 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
982 sa
.sa_handler
= sigpipe
;
983 sigaction(SIGPIPE
, &sa
, NULL
);
984 sa
.sa_handler
= proceed
;
985 sa
.sa_flags
= SA_RESTART
;
986 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
989 for (i
= 0; i
< SLAVES
; i
++) {
990 if (i
== slp
- &slaves
[0]) {
996 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
997 (slaves
[i
].pid
= fork()) < 0)
998 quit("too many slaves, %d (recompile smaller): %s\n",
1001 slaves
[i
].fd
= cmd
[1];
1003 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1005 for (j
= 0; j
<= i
; j
++)
1006 (void) close(slaves
[j
].fd
);
1008 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1009 #if defined(SIGINFO)
1010 sigaddset(&sigs
, SIGINFO
);
1012 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1014 #ifdef LINUX_FORK_BUG
1015 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1017 quit("master/slave protocol botched 3\n");
1023 (slaves
[i
].pid
== slp
->pid
));
1030 #ifdef LINUX_FORK_BUG
1032 * Wait for all slaves to _actually_ start to circumvent a bug in
1033 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1034 * returned from fork() causes a SEGV in the child process
1036 for (i
= 0; i
< SLAVES
; i
++)
1037 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1038 quit("master/slave protocol botched 4\n");
1041 for (i
= 0; i
< SLAVES
; i
++)
1042 (void) dump_atomic_write( slaves
[i
].fd
,
1043 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1044 sizeof slaves
[0].pid
);
1054 for (i
= 0; i
< SLAVES
; i
++)
1055 if (slaves
[i
].pid
> 0) {
1056 (void) kill(slaves
[i
].pid
, SIGKILL
);
1062 * Synchronization - each process waits for a SIGUSR2 from the
1063 * previous process before writing to the tape, and sends SIGUSR2
1064 * to the next process when the tape write completes. On tape errors
1065 * a SIGUSR1 is sent to the master which then terminates all of the
1077 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1078 char * volatile buffer
;
1079 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1080 struct tapebuf
* volatile comp_buf
= NULL
;
1082 volatile int do_compress
= !first
;
1083 unsigned long worklen
;
1085 unsigned int worklen2
;
1088 lzo_align_t __LZO_MMODEL
*LZO_WorkMem
;
1090 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1091 struct slave_results returns
;
1096 long long curtapepos
;
1097 union u_spcl
*uspclptr
;
1098 struct s_spcl
*spclptr
;
1099 #endif /* USE_QFA */
1103 sigaddset(&set
, SIGUSR2
);
1104 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1108 * Need our own seek pointer.
1110 (void) close(diskfd
);
1111 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1112 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1115 (void)ioctl(diskfd
, BLKFLSBUF
);
1118 retval
= dump_fs_open(disk
, &fs
);
1120 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1121 #endif /* __linux__ */
1124 * Need the pid of the next slave in the loop...
1126 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1127 != sizeof nextslave
) {
1128 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1131 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1132 /* if we're doing a compressed dump, allocate the compress buffer */
1134 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1135 /* Add extra space to deal with compression enlarging the buffer */
1136 if (TP_BSIZE
> writesize
/64 + 19)
1139 bsiz
+= writesize
/64 + 19;
1140 comp_buf
= malloc(bsiz
);
1141 if (comp_buf
== NULL
)
1142 quit("couldn't allocate a compress buffer.\n");
1143 if (zipflag
== COMPRESS_ZLIB
)
1144 comp_buf
->flags
= COMPRESS_ZLIB
;
1145 else if (zipflag
== COMPRESS_BZLIB
)
1146 comp_buf
->flags
= COMPRESS_BZLIB
;
1147 else if (zipflag
== COMPRESS_LZO
) {
1148 comp_buf
->flags
= COMPRESS_LZO
;
1149 if (lzo_init() != LZO_E_OK
) quit("lzo_init failed\n");
1151 quit("internal error - unknown compression method: %d\n", zipflag
);
1154 LZO_WorkMem
= malloc(LZO1X_1_MEM_COMPRESS
);
1156 quit("couldn't allocate a compress buffer.\n");
1158 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1161 * Get list of blocks to dump, read the blocks into tape buffer
1163 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1164 struct req
*p
= slp
->req
;
1166 for (trecno
= 0; trecno
< ntrec
;
1167 trecno
+= p
->count
, p
+= p
->count
) {
1168 if (p
->dblk
) { /* read a disk block */
1169 bread(p
->dblk
, slp
->tblock
[trecno
],
1170 p
->count
* TP_BSIZE
);
1171 } else { /* read record from pipe */
1172 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1173 (char *)slp
->tblock
[trecno
],
1174 TP_BSIZE
) != TP_BSIZE
)
1175 quit("master/slave protocol botched.\n");
1179 /* Try to write the data... */
1183 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1184 bufsize
= writesize
; /* length to write */
1185 returns
.clen
= returns
.unclen
= bufsize
;
1187 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1189 * When writing a compressed dump, each block except
1190 * the first one on each tape is written
1191 * from struct tapebuf with an 4 byte prefix
1192 * followed by the data. This can be less than
1193 * writesize. Restore, on a short read, can compare the
1194 * length read to the compressed length in the header
1195 * to verify that the read was good. Blocks which don't
1196 * compress well are written uncompressed.
1197 * The first block written by each slave is not compressed
1198 * and does not have a prefix.
1201 if (compressed
&& do_compress
) {
1202 comp_buf
->length
= bufsize
;
1203 worklen
= TP_BSIZE
+ writesize
;
1206 if (zipflag
== COMPRESS_ZLIB
) {
1207 compresult
= compress2(comp_buf
->buf
,
1209 (char *)slp
->tblock
[0],
1212 if (compresult
== Z_OK
)
1217 #endif /* HAVE_ZLIB */
1219 if (zipflag
== COMPRESS_BZLIB
) {
1221 compresult
= BZ2_bzBuffToBuffCompress(
1224 (char *)slp
->tblock
[0],
1229 if (compresult
== BZ_OK
)
1235 #endif /* HAVE_BZLIB */
1237 if (zipflag
== COMPRESS_LZO
) {
1238 compresult
= lzo1x_1_compress((char *)slp
->tblock
[0],writesize
,
1240 (lzo_uintp
)&worklen
,
1242 if (compresult
== LZO_E_OK
)
1247 #endif /* HAVE_LZO */
1248 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1249 /* write the compressed buffer */
1250 comp_buf
->length
= worklen
;
1251 comp_buf
->compressed
= 1;
1252 buffer
= (char *) comp_buf
;
1253 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1256 /* write the data uncompressed */
1257 comp_buf
->length
= writesize
;
1258 comp_buf
->compressed
= 0;
1259 buffer
= (char *) comp_buf
;
1260 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1261 returns
.unclen
= returns
.clen
;
1262 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1265 /* compress the remaining blocks if we're compressing */
1266 do_compress
= compressed
;
1267 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1269 if (sigsetjmp(jmpbuf
, 1) == 0) {
1278 if (gTapeposfd
>= 0) {
1281 for (i
= 0; i
< ntrec
; ++i
) {
1282 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1283 spclptr
= &uspclptr
->s_spcl
;
1284 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1285 (spclptr
->c_type
== TS_INODE
) &&
1286 ((spclptr
->c_dinode
.di_mode
& S_IFMT
) != IFDIR
) &&
1287 (spclptr
->c_date
== gThisDumpDate
)) {
1288 /* if an error occured previously don't
1293 gtperr
= GetTapePos(&curtapepos
);
1296 MkTapeString(spclptr
, curtapepos
);
1300 #endif /* USE_QFA */
1302 while (eot_count
< 10 && size
< bufsize
) {
1305 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1308 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1310 printf("slave %d wrote %d\n", slave_number
, wrote
);
1320 if (size
!= bufsize
)
1321 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1322 slave_number
, size
, bufsize
);
1326 * Handle ENOSPC as an EOT condition.
1328 if (wrote
< 0 && errno
== ENOSPC
) {
1334 returns
.clen
= returns
.unclen
= 0;
1337 * pass errno back to master for special handling
1340 returns
.unclen
= -errno
;
1343 * pass size of data and size of write back to master
1344 * (for EOT handling)
1346 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1349 * Signal the next slave to go.
1351 (void) kill(nextslave
, SIGUSR2
);
1354 quit("error reading command pipe: %s\n", strerror(errno
));
1358 * Since a read from a pipe may not return all we asked for,
1359 * or a write may not write all we ask if we get a signal,
1360 * loop until the count is satisfied (or error).
1363 dump_atomic_read(int fd
, void *buf
, size_t count
)
1365 int got
, need
= count
;
1368 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1370 } while (got
== -1 && errno
== EINTR
);
1371 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1375 * Since a read from a pipe may not return all we asked for,
1376 * or a write may not write all we ask if we get a signal,
1377 * loop until the count is satisfied (or error).
1380 dump_atomic_write(int fd
, const void *buf
, size_t count
)
1382 int got
, need
= count
;
1385 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1387 } while (got
== -1 && errno
== EINTR
);
1388 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1394 * read the current tape position
1397 GetTapePos(long long *pos
)
1403 *pos
= (long long) rmtseek(0, SEEK_CUR
);
1412 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1413 *pos
= (long long)mtpos
;
1416 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1422 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1430 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
) {
1433 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1436 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1437 (unsigned long)spclptr
->c_inumber
,
1440 gTps
[sizeof(gTps
) - 1] = '\0';
1441 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1442 warn("error writing tapepos file.\n");
1445 #endif /* USE_QFA */