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.74 2003/03/30 15:40:37 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 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
;
109 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
110 static int magtapeout
; /* output is really a tape */
112 static ssize_t dump_atomic_read
__P((int, void *, size_t));
113 static ssize_t dump_atomic_write
__P((int, const void *, size_t));
115 static void doslave
__P((int, int, int));
117 static void doslave
__P((int, int));
119 static void enslave
__P((void));
120 static void flushtape
__P((void));
121 static void killall
__P((void));
122 static void rollforward
__P((void));
124 static int GetTapePos
__P((long long *));
125 static void MkTapeString
__P((struct s_spcl
*, long long));
129 * Concurrent dump mods (Caltech) - disk block reading and tape writing
130 * are exported to several slave processes. While one slave writes the
131 * tape, the others read disk blocks; they pass control of the tape in
132 * a ring via signals. The parent process traverses the filesystem and
133 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
134 * The following structure defines the instruction packets sent to slaves.
142 struct slave_results
{
143 ssize_t unclen
; /* uncompressed length */
144 ssize_t clen
; /* compressed length */
147 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
149 int tapea
; /* header number at start of this chunk */
150 int count
; /* count to next header (used for TS_TAPE */
152 int inode
; /* inode that we are currently dealing with */
153 int fd
; /* FD for this slave */
154 int pid
; /* PID for this slave */
155 int sent
; /* 1 == we've sent this slave requests */
156 int firstrec
; /* record number of this block */
157 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
158 struct req
*req
; /* buffer for requests */
162 char (*nextblock
)[TP_BSIZE
];
164 static time_t tstart_volume
; /* time of volume start */
165 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
167 int master
; /* pid of master, for sending error signals */
168 int tenths
; /* length of tape overhead per block written */
169 static int caught
; /* have we caught the signal to proceed? */
170 static int ready
; /* have we reached the lock point without having */
171 /* received the SIGUSR2 signal from the prev slave? */
172 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
173 /* SIGUSR2 arrives from the previous slave */
175 static int gtperr
= 0;
181 int pgoff
= getpagesize() - 1;
185 writesize
= ntrec
* TP_BSIZE
;
186 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
188 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
189 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
190 * repositioning after stopping, i.e, streaming mode, where the gap is
191 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
193 if (blocksperfile
== 0 && !unlimited
)
194 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
200 * Allocate tape buffer contiguous with the array of instruction
201 * packets, so flushtape() can write them together with one write().
202 * Align tape buffer on page boundary to speed up tape write().
204 for (i
= 0; i
<= SLAVES
; i
++) {
206 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
209 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
211 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
213 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
215 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
221 nextblock
= slp
->tblock
;
226 writerec(const void *dp
, int isspcl
)
229 slp
->req
[trecno
].dblk
= (daddr_t
)0;
230 slp
->req
[trecno
].count
= 1;
231 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
232 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
234 /* Need to write it to the archive file */
235 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
237 if (AfileActive
&& Afile
>= 0) {
238 /* When we dump an inode which is not a directory,
239 * it means we ended the archive contents */
240 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
241 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
245 tmp
= *(union u_spcl
*)dp
;
246 /* Write the record, _uncompressed_ */
248 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
251 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
252 msg("error writing archive file: %s\n",
259 lastspclrec
= spcl
.c_tapea
;
267 dumpblock(daddr_t blkno
, int size
)
269 int avail
, tpblks
, dblkno
;
271 dblkno
= fsbtodb(sblock
, blkno
);
272 tpblks
= size
>> tp_bshift
;
273 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
274 slp
->req
[trecno
].dblk
= dblkno
;
275 slp
->req
[trecno
].count
= avail
;
277 spcl
.c_tapea
+= avail
;
280 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
292 msg("write error on %s: %s\n", tape
, strerror(errnum
));
293 quit("Cannot recover\n");
296 msg("write error %d blocks into volume %d: %s\n",
297 blocksthisvol
, tapeno
, strerror(errnum
));
298 broadcast("DUMP WRITE ERROR!\n");
299 if (query("Do you want to rewrite this volume?")) {
300 msg("Closing this volume. Prepare to restart with new media;\n");
301 msg("this dump volume will be rewritten.\n");
307 if (query("Do you want to start the next tape?"))
313 sigpipe(UNUSED(int signo
))
316 quit("Broken pipe\n");
321 * Update xferrate stats
330 ttaken
= tnow
- tstart_volume
;
331 blocks
= spcl
.c_tapea
- tapea_volume
;
332 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
334 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
335 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
337 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
338 long txfrate
= volkb
/ ttaken
;
339 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
340 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
341 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
345 double rate
= .0005 + (double) blocks
/ (double) volkb
;
346 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
348 tapeno
, blocks
, volkb
, rate
);
355 mktimeest(time_t tnow
)
357 static char msgbuf
[128];
362 if (blockswritten
< 500)
364 if (blockswritten
> tapesize
)
365 tapesize
= blockswritten
;
366 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
367 / blockswritten
* tapesize
;
368 if (tnow
> tstart_volume
)
369 (void)snprintf(msgbuf
, sizeof(msgbuf
),
370 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
371 (blockswritten
* 100.0) / tapesize
,
372 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
373 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
375 (void)snprintf(msgbuf
, sizeof(msgbuf
),
376 "%3.2f%% done, finished in %d:%02d\n",
377 (blockswritten
* 100.0) / tapesize
,
378 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
386 * information message upon receipt of SIGINFO
389 statussig(int notused
)
391 int save_errno
= errno
;
394 buf
= mktimeest(time(NULL
));
396 write(STDERR_FILENO
, buf
, strlen(buf
));
406 struct slave_results returned
;
408 int siz
= (char *)nextblock
- (char *)slp
->req
;
410 slp
->req
[trecno
].count
= 0; /* Sentinel */
412 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
413 quit("error writing command pipe: %s\n", strerror(errno
));
414 slp
->sent
= 1; /* we sent a request, read the response later */
416 lastfirstrec
= slp
->firstrec
;
418 if (++slp
>= &slaves
[SLAVES
])
421 /* Read results back from next slave */
423 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
424 != sizeof returned
) {
425 perror(" DUMP: error reading command pipe in master");
428 got
= returned
.unclen
;
429 bytes_written
+= returned
.clen
;
430 if (returned
.unclen
== returned
.clen
)
434 /* Check for errors or end of tape */
436 /* Check for errors */
440 msg("End of tape detected\n");
443 * Drain the results, don't care what the values were.
444 * If we read them here then trewind won't...
446 for (i
= 0; i
< SLAVES
; i
++) {
447 if (slaves
[i
].sent
) {
448 if (dump_atomic_read( slaves
[i
].fd
,
449 (char *)&returned
, sizeof returned
)
450 != sizeof returned
) {
451 perror(" DUMP: error reading command pipe in master");
465 if (spcl
.c_type
!= TS_END
) {
466 for (i
= 0; i
< spcl
.c_count
; i
++)
467 if (spcl
.c_addr
[i
] != 0)
470 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
471 slp
->tapea
= spcl
.c_tapea
;
472 slp
->firstrec
= lastfirstrec
+ ntrec
;
474 nextblock
= slp
->tblock
;
476 asize
+= tenths
+ returned
.clen
/ density
;
477 blockswritten
+= ntrec
;
478 blocksthisvol
+= ntrec
;
479 if (!pipeout
&& !unlimited
) {
481 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfile
) * 1024)
482 : blocksthisvol
>= blocksperfile
) {
487 else if (asize
> tsize
) {
500 struct slave_results returned
;
502 for (f
= 0; f
< SLAVES
; f
++) {
504 * Drain the results, but unlike EOT we DO (or should) care
505 * what the return values were, since if we detect EOT after
506 * we think we've written the last blocks to the tape anyway,
507 * we have to replay those blocks with rollforward.
509 * fixme: punt for now.
511 if (slaves
[f
].sent
) {
512 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
513 != sizeof returned
) {
514 perror(" DUMP: error reading command pipe in master");
517 got
= returned
.unclen
;
518 bytes_written
+= returned
.clen
;
519 if (returned
.unclen
== returned
.clen
)
527 msg("EOT detected in last 2 tape records!\n");
528 msg("Use a longer tape, decrease the size estimate\n");
529 quit("or use no size estimate at all.\n");
532 (void) close(slaves
[f
].fd
);
534 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
539 msg("Closing %s\n", tape
);
544 while (rmtopen(tape
, O_RDONLY
) < 0)
551 (void) close(tapefd
);
553 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
568 if (nexttape
|| Mflag
)
571 msg("Launching %s\n", eot_script
);
572 eot_code
= system_command(eot_script
, tape
, tapeno
);
574 if (eot_code
!= 0 && eot_code
!= 1) {
575 msg("Dump aborted by the end of tape script\n");
581 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
582 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
584 while (!query("Is the new volume mounted and ready to go?"))
585 if (query("Do you want to abort?")) {
594 struct req
*p
, *q
= NULL
, *prev
;
596 int i
, size
, savedtapea
, got
;
597 union u_spcl
*ntb
, *otb
;
598 struct slave_results returned
;
603 tslp
= &slaves
[SLAVES
];
604 ntb
= (union u_spcl
*)tslp
->tblock
[1];
607 * Each of the N slaves should have requests that need to
608 * be replayed on the next tape. Use the extra slave buffers
609 * (slaves[SLAVES]) to construct request lists to be sent to
610 * each slave in turn.
612 for (i
= 0; i
< SLAVES
; i
++) {
614 otb
= (union u_spcl
*)slp
->tblock
;
617 * For each request in the current slave, copy it to tslp.
621 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
624 *ntb
++ = *otb
++; /* copy the datablock also */
629 quit("rollforward: protocol botch");
641 nextblock
= tslp
->tblock
;
642 savedtapea
= spcl
.c_tapea
;
643 spcl
.c_tapea
= slp
->tapea
;
645 spcl
.c_tapea
= savedtapea
;
646 lastspclrec
= savedtapea
- 1;
648 size
= (char *)ntb
- (char *)q
;
649 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
650 perror(" DUMP: error writing command pipe");
655 lastfirstrec
= slp
->firstrec
;
657 if (++slp
>= &slaves
[SLAVES
])
662 if (prev
->dblk
!= 0) {
664 * If the last one was a disk block, make the
665 * first of this one be the last bit of that disk
668 q
->dblk
= prev
->dblk
+
669 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
670 ntb
= (union u_spcl
*)tslp
->tblock
;
673 * It wasn't a disk block. Copy the data to its
674 * new location in the buffer.
677 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
678 ntb
= (union u_spcl
*)tslp
->tblock
[1];
682 nextblock
= slp
->tblock
;
685 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
686 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
693 * Clear the first slaves' response. One hopes that it
694 * worked ok, otherwise the tape is much too short!
697 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
698 != sizeof returned
) {
699 perror(" DUMP: error reading command pipe in master");
702 got
= returned
.unclen
;
703 bytes_written
+= returned
.clen
;
704 if (returned
.clen
== returned
.unclen
)
712 quit("EOT detected at start of the tape!\n");
718 if (spcl
.c_type
!= TS_END
) {
719 for (i
= 0; i
< spcl
.c_count
; i
++)
720 if (spcl
.c_addr
[i
] != 0)
724 slp
->firstrec
= lastfirstrec
+ ntrec
;
725 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
727 asize
+= tenths
+ returned
.clen
/ density
;
728 blockswritten
+= ntrec
;
729 blocksthisvol
+= ntrec
;
734 * We implement taking and restoring checkpoints on the tape level.
735 * When each tape is opened, a new process is created by forking; this
736 * saves all of the necessary context in the parent. The child
737 * continues the dump; the parent waits around, saving the context.
738 * If the child returns X_REWRITE, then it had problems writing that tape;
739 * this causes the parent to fork again, duplicating the context, and
740 * everything continues as if nothing had happened.
743 startnewtape(int top
)
754 sigaddset(&sigs
, SIGINT
);
755 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
756 #else /* __linux__ */
758 void (*interrupt_save
)();
760 sig_t interrupt_save
;
762 interrupt_save
= signal(SIGINT
, SIG_IGN
);
763 #endif /* __linux__ */
765 parentpid
= getpid();
766 tapea_volume
= spcl
.c_tapea
;
767 tapea_bytes
= bytes_written
;
768 tstart_volume
= time(NULL
);
772 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
774 (void)signal(SIGINT
, interrupt_save
);
777 * All signals are inherited...
781 msg("Context save fork fails in parent %d\n", parentpid
);
787 * save the context by waiting
788 * until the child doing all of the work returns.
789 * don't catch the interrupt
792 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
794 signal(SIGINT
, SIG_IGN
);
797 msg("Tape: %d; parent process: %d child process %d\n",
798 tapeno
+1, parentpid
, childpid
);
800 while ((waitpid
= wait(&status
)) != childpid
)
801 if (waitpid
!= rshpid
)
802 msg("Parent %d waiting for child %d has another child %d return\n",
803 parentpid
, childpid
, waitpid
);
805 msg("Child %d returns LOB status %o\n",
806 childpid
, status
&0xFF);
808 status
= (status
>> 8) & 0xFF;
812 msg("Child %d finishes X_FINOK\n", childpid
);
815 msg("Child %d finishes X_ABORT\n", childpid
);
818 msg("Child %d finishes X_REWRITE\n", childpid
);
821 msg("Child %d finishes unknown %d\n",
832 goto restore_check_point
;
834 msg("Bad return code from dump: %d\n", status
);
838 } else { /* we are the child; just continue */
840 sleep(4); /* allow time for parent's message to get out */
841 msg("Child on Tape %d has parent %d, my pid = %d\n",
842 tapeno
+1, parentpid
, getpid());
845 * If we have a name like "/dev/rmt0,/dev/rmt1",
846 * use the name before the comma first, and save
847 * the remaining names for subsequent volumes.
849 tapeno
++; /* current tape sequence */
851 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
852 tape
[MAXPATHLEN
- 1] = '\0';
853 msg("Dumping volume %d on %s\n", tapeno
, tape
);
855 else if (nexttape
|| strchr(tapeprefix
, ',')) {
856 if (nexttape
&& *nexttape
)
857 tapeprefix
= nexttape
;
858 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
863 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
864 tape
[MAXPATHLEN
- 1] = '\0';
865 msg("Dumping volume %d on %s\n", tapeno
, tape
);
868 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
870 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
872 while ((tapefd
= (pipeout
? fileno(stdout
) :
873 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
876 msg("Cannot open output \"%s\": %s\n", tape
,
878 if (!query("Do you want to retry the open?"))
885 struct mtget mt_stat
;
886 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
888 msg("Output is to %s\n",
889 magtapeout ? "tape" : "file/pipe");
893 enslave(); /* Share open tape file descriptor with slaves */
898 newtape
++; /* new tape signal */
899 spcl
.c_count
= slp
->count
;
901 * measure firstrec in TP_BSIZE units since restore doesn't
902 * know the correct ntrec value...
904 spcl
.c_firstrec
= slp
->firstrec
;
906 spcl
.c_type
= TS_TAPE
;
907 spcl
.c_flags
|= DR_NEWHEADER
;
908 spcl
.c_ntrec
= ntrec
;
910 spcl
.c_flags
|= DR_COMPRESSED
;
911 writeheader((dump_ino_t
)slp
->inode
);
912 spcl
.c_flags
&=~ DR_NEWHEADER
;
913 msg("Volume %d started with block %ld at: %s", tapeno
,
914 spcl
.c_tapea
, ctime(&tstart_volume
));
916 msg("Volume %d begins with blocks from inode %d\n",
918 if (tapeno
< (int)TP_NINOS
)
919 volinfo
[tapeno
] = slp
->inode
;
924 dumpabort(UNUSED(int signo
))
927 if (master
!= 0 && master
!= getpid())
928 /* Signals master to call dumpabort */
929 (void) kill(master
, SIGTERM
);
932 msg("The ENTIRE dump is aborted.\n");
945 msg("pid = %d exits with status %d\n", getpid(), status
);
951 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
954 proceed(UNUSED(int signo
))
957 siglongjmp(jmpbuf
, 1);
965 #ifdef LINUX_FORK_BUG
973 { struct sigaction sa
;
974 memset(&sa
, 0, sizeof sa
);
975 sigemptyset(&sa
.sa_mask
);
976 sa
.sa_handler
= dumpabort
;
977 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
978 sa
.sa_handler
= sigpipe
;
979 sigaction(SIGPIPE
, &sa
, NULL
);
980 sa
.sa_handler
= proceed
;
981 sa
.sa_flags
= SA_RESTART
;
982 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
985 for (i
= 0; i
< SLAVES
; i
++) {
986 if (i
== slp
- &slaves
[0]) {
992 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
993 (slaves
[i
].pid
= fork()) < 0)
994 quit("too many slaves, %d (recompile smaller): %s\n",
997 slaves
[i
].fd
= cmd
[1];
999 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1001 for (j
= 0; j
<= i
; j
++)
1002 (void) close(slaves
[j
].fd
);
1004 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1005 #if defined(SIGINFO)
1006 sigaddset(&sigs
, SIGINFO
);
1008 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1010 #ifdef LINUX_FORK_BUG
1011 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1013 quit("master/slave protocol botched 3\n");
1019 (slaves
[i
].pid
== slp
->pid
));
1026 #ifdef LINUX_FORK_BUG
1028 * Wait for all slaves to _actually_ start to circumvent a bug in
1029 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1030 * returned from fork() causes a SEGV in the child process
1032 for (i
= 0; i
< SLAVES
; i
++)
1033 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1034 quit("master/slave protocol botched 4\n");
1037 for (i
= 0; i
< SLAVES
; i
++)
1038 (void) dump_atomic_write( slaves
[i
].fd
,
1039 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1040 sizeof slaves
[0].pid
);
1050 for (i
= 0; i
< SLAVES
; i
++)
1051 if (slaves
[i
].pid
> 0) {
1052 (void) kill(slaves
[i
].pid
, SIGKILL
);
1058 * Synchronization - each process waits for a SIGUSR2 from the
1059 * previous process before writing to the tape, and sends SIGUSR2
1060 * to the next process when the tape write completes. On tape errors
1061 * a SIGUSR1 is sent to the master which then terminates all of the
1073 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1074 char * volatile buffer
;
1075 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1076 struct tapebuf
* volatile comp_buf
= NULL
;
1078 volatile int do_compress
= !first
;
1079 unsigned long worklen
;
1081 unsigned int worklen2
;
1083 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1084 struct slave_results returns
;
1089 long long curtapepos
;
1090 union u_spcl
*uspclptr
;
1091 struct s_spcl
*spclptr
;
1092 #endif /* USE_QFA */
1096 sigaddset(&set
, SIGUSR2
);
1097 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1101 * Need our own seek pointer.
1103 (void) close(diskfd
);
1104 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1105 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1108 (void)ioctl(diskfd
, BLKFLSBUF
);
1111 retval
= dump_fs_open(disk
, &fs
);
1113 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1114 #endif /* __linux__ */
1117 * Need the pid of the next slave in the loop...
1119 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1120 != sizeof nextslave
) {
1121 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1124 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1125 /* if we're doing a compressed dump, allocate the compress buffer */
1127 comp_buf
= malloc(sizeof(struct tapebuf
) + TP_BSIZE
+ writesize
);
1128 if (comp_buf
== NULL
)
1129 quit("couldn't allocate a compress buffer.\n");
1131 comp_buf
->flags
= COMPRESS_BZLIB
;
1133 comp_buf
->flags
= COMPRESS_ZLIB
;
1135 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1138 * Get list of blocks to dump, read the blocks into tape buffer
1140 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1141 struct req
*p
= slp
->req
;
1143 for (trecno
= 0; trecno
< ntrec
;
1144 trecno
+= p
->count
, p
+= p
->count
) {
1145 if (p
->dblk
) { /* read a disk block */
1146 bread(p
->dblk
, slp
->tblock
[trecno
],
1147 p
->count
* TP_BSIZE
);
1148 } else { /* read record from pipe */
1149 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1150 (char *)slp
->tblock
[trecno
],
1151 TP_BSIZE
) != TP_BSIZE
)
1152 quit("master/slave protocol botched.\n");
1156 /* Try to write the data... */
1160 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1161 bufsize
= writesize
; /* length to write */
1162 returns
.clen
= returns
.unclen
= bufsize
;
1164 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB)
1166 * When writing a compressed dump, each block except
1167 * the first one on each tape is written
1168 * from struct tapebuf with an 4 byte prefix
1169 * followed by the data. This can be less than
1170 * writesize. Restore, on a short read, can compare the
1171 * length read to the compressed length in the header
1172 * to verify that the read was good. Blocks which don't
1173 * compress well are written uncompressed.
1174 * The first block written by each slave is not compressed
1175 * and does not have a prefix.
1178 if (compressed
&& do_compress
) {
1179 comp_buf
->length
= bufsize
;
1180 worklen
= TP_BSIZE
+ writesize
;
1184 compresult
= compress2(comp_buf
->buf
,
1186 (char *)slp
->tblock
[0],
1189 if (compresult
== Z_OK
)
1194 #endif /* HAVE_ZLIB */
1198 compresult
= BZ2_bzBuffToBuffCompress(
1201 (char *)slp
->tblock
[0],
1206 if (compresult
== BZ_OK
)
1212 #endif /* HAVE_BZLIB */
1213 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1214 /* write the compressed buffer */
1215 comp_buf
->length
= worklen
;
1216 comp_buf
->compressed
= 1;
1217 buffer
= (char *) comp_buf
;
1218 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1221 /* write the data uncompressed */
1222 comp_buf
->length
= writesize
;
1223 comp_buf
->compressed
= 0;
1224 buffer
= (char *) comp_buf
;
1225 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1226 returns
.unclen
= returns
.clen
;
1227 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1230 /* compress the remaining blocks if we're compressing */
1231 do_compress
= compressed
;
1232 #endif /* HAVE_ZLIB || HAVE_BZLIB */
1234 if (sigsetjmp(jmpbuf
, 1) == 0) {
1243 if (gTapeposfd
>= 0) {
1246 for (i
= 0; i
< ntrec
; ++i
) {
1247 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1248 spclptr
= &uspclptr
->s_spcl
;
1249 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1250 (spclptr
->c_type
== TS_INODE
) &&
1251 ((spclptr
->c_dinode
.di_mode
& S_IFMT
) != IFDIR
) &&
1252 (spclptr
->c_date
== gThisDumpDate
)) {
1253 /* if an error occured previously don't
1258 gtperr
= GetTapePos(&curtapepos
);
1261 MkTapeString(spclptr
, curtapepos
);
1265 #endif /* USE_QFA */
1267 while (eot_count
< 10 && size
< bufsize
) {
1270 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1273 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1275 printf("slave %d wrote %d\n", slave_number
, wrote
);
1285 if (size
!= bufsize
)
1286 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1287 slave_number
, size
, bufsize
);
1291 * Handle ENOSPC as an EOT condition.
1293 if (wrote
< 0 && errno
== ENOSPC
) {
1299 returns
.clen
= returns
.unclen
= 0;
1302 * pass errno back to master for special handling
1305 returns
.unclen
= -errno
;
1308 * pass size of data and size of write back to master
1309 * (for EOT handling)
1311 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1314 * Signal the next slave to go.
1316 (void) kill(nextslave
, SIGUSR2
);
1319 quit("error reading command pipe: %s\n", strerror(errno
));
1323 * Since a read from a pipe may not return all we asked for,
1324 * or a write may not write all we ask if we get a signal,
1325 * loop until the count is satisfied (or error).
1328 dump_atomic_read(int fd
, void *buf
, size_t count
)
1330 int got
, need
= count
;
1333 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1335 } while (got
== -1 && errno
== EINTR
);
1336 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1340 * Since a read from a pipe may not return all we asked for,
1341 * or a write may not write all we ask if we get a signal,
1342 * loop until the count is satisfied (or error).
1345 dump_atomic_write(int fd
, const void *buf
, size_t count
)
1347 int got
, need
= count
;
1350 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1352 } while (got
== -1 && errno
== EINTR
);
1353 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1359 * read the current tape position
1362 GetTapePos(long long *pos
)
1368 *pos
= (long long) rmtseek(0, SEEK_CUR
);
1377 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1378 *pos
= (long long)mtpos
;
1381 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1387 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1395 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
) {
1398 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1401 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1402 (unsigned long)spclptr
->c_inumber
,
1405 gTps
[sizeof(gTps
) - 1] = '\0';
1406 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1407 warn("error writing tapepos file.\n");
1410 #endif /* USE_QFA */