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.79 2003/11/22 16:52:16 stelian Exp $";
44 #include <compatlfs.h>
50 #include <compaterr.h>
61 #include <sys/types.h>
63 #include <linux/fs.h> /* for definition of BLKFLSBUF */
66 #include <sys/param.h>
67 #include <sys/socket.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
74 #include <linux/ext2_fs.h>
76 #include <ext2fs/ext2fs.h>
78 #include <bsdcompat.h>
80 #include <sys/vnode.h>
83 #include <ufs/inode.h>
85 #include <ufs/ufs/dinode.h>
86 #include <ufs/ffs/fs.h>
87 #endif /* __linux__ */
89 #include <protocols/dumprestore.h>
93 #endif /* HAVE_ZLIB */
97 #endif /* HAVE_BZLIB */
101 #endif /* HAVE_LZO */
105 int writesize
; /* size of malloc()ed buffer for tape */
106 long lastspclrec
= -1; /* tape block number of last written header */
107 int trecno
= 0; /* next record to write in current block */
108 extern long blocksperfile
; /* number of blocks per output file */
109 long blocksthisvol
; /* number of blocks on current output file */
110 extern int ntrec
; /* blocking factor on tape */
111 extern int cartridge
;
115 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
116 static int magtapeout
; /* output is really a tape */
118 static ssize_t dump_atomic_read
__P((int, void *, size_t));
119 static ssize_t dump_atomic_write
__P((int, const void *, size_t));
121 static void doslave
__P((int, int, int));
123 static void doslave
__P((int, int));
125 static void enslave
__P((void));
126 static void flushtape
__P((void));
127 static void killall
__P((void));
128 static void rollforward
__P((void));
130 static int GetTapePos
__P((long long *));
131 static int MkTapeString
__P((struct s_spcl
*, long long));
132 #define FILESQFAPOS 20
136 * Concurrent dump mods (Caltech) - disk block reading and tape writing
137 * are exported to several slave processes. While one slave writes the
138 * tape, the others read disk blocks; they pass control of the tape in
139 * a ring via signals. The parent process traverses the filesystem and
140 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
141 * The following structure defines the instruction packets sent to slaves.
149 struct slave_results
{
150 ssize_t unclen
; /* uncompressed length */
151 ssize_t clen
; /* compressed length */
154 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
156 int tapea
; /* header number at start of this chunk */
157 int count
; /* count to next header (used for TS_TAPE */
159 int inode
; /* inode that we are currently dealing with */
160 int fd
; /* FD for this slave */
161 int pid
; /* PID for this slave */
162 int sent
; /* 1 == we've sent this slave requests */
163 int firstrec
; /* record number of this block */
164 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
165 struct req
*req
; /* buffer for requests */
169 char (*nextblock
)[TP_BSIZE
];
171 static time_t tstart_volume
; /* time of volume start */
172 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
174 int master
; /* pid of master, for sending error signals */
175 int tenths
; /* length of tape overhead per block written */
176 static int caught
; /* have we caught the signal to proceed? */
177 static int ready
; /* have we reached the lock point without having */
178 /* received the SIGUSR2 signal from the prev slave? */
179 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
180 /* SIGUSR2 arrives from the previous slave */
182 static int gtperr
= 0;
188 int pgoff
= getpagesize() - 1;
192 writesize
= ntrec
* TP_BSIZE
;
193 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
195 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
196 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
197 * repositioning after stopping, i.e, streaming mode, where the gap is
198 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
200 if (blocksperfile
== 0 && !unlimited
)
201 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
207 * Allocate tape buffer contiguous with the array of instruction
208 * packets, so flushtape() can write them together with one write().
209 * Align tape buffer on page boundary to speed up tape write().
211 for (i
= 0; i
<= SLAVES
; i
++) {
213 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
216 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
218 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
220 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
222 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
228 nextblock
= slp
->tblock
;
233 writerec(const void *dp
, int isspcl
)
236 slp
->req
[trecno
].dblk
= (daddr_t
)0;
237 slp
->req
[trecno
].count
= 1;
238 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
239 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
241 /* Need to write it to the archive file */
242 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
244 if (AfileActive
&& Afile
>= 0) {
245 /* When we dump an inode which is not a directory,
246 * it means we ended the archive contents */
247 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
248 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
252 tmp
= *(union u_spcl
*)dp
;
253 /* Write the record, _uncompressed_ */
255 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
258 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
259 msg("error writing archive file: %s\n",
266 lastspclrec
= spcl
.c_tapea
;
274 dumpblock(daddr_t blkno
, int size
)
276 int avail
, tpblks
, dblkno
;
278 dblkno
= fsbtodb(sblock
, blkno
);
279 tpblks
= size
>> tp_bshift
;
280 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
281 slp
->req
[trecno
].dblk
= dblkno
;
282 slp
->req
[trecno
].count
= avail
;
284 spcl
.c_tapea
+= avail
;
287 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
299 msg("write error on %s: %s\n", tape
, strerror(errnum
));
300 quit("Cannot recover\n");
303 msg("write error %d blocks into volume %d: %s\n",
304 blocksthisvol
, tapeno
, strerror(errnum
));
305 broadcast("DUMP WRITE ERROR!\n");
306 if (query("Do you want to rewrite this volume?")) {
307 msg("Closing this volume. Prepare to restart with new media;\n");
308 msg("this dump volume will be rewritten.\n");
314 if (query("Do you want to start the next tape?"))
320 sigpipe(UNUSED(int signo
))
323 quit("Broken pipe\n");
328 * Update xferrate stats
337 ttaken
= tnow
- tstart_volume
;
338 blocks
= spcl
.c_tapea
- tapea_volume
;
339 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
341 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
342 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
344 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
345 long txfrate
= volkb
/ ttaken
;
346 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
347 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
348 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
352 double rate
= .0005 + (double) blocks
/ (double) volkb
;
353 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
355 tapeno
, blocks
, volkb
, rate
);
362 mktimeest(time_t tnow
)
364 static char msgbuf
[128];
369 if (blockswritten
< 500)
371 if (blockswritten
> tapesize
)
372 tapesize
= blockswritten
;
373 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
374 / blockswritten
* tapesize
;
375 if (tnow
> tstart_volume
)
376 (void)snprintf(msgbuf
, sizeof(msgbuf
),
377 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
378 (blockswritten
* 100.0) / tapesize
,
379 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
380 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
382 (void)snprintf(msgbuf
, sizeof(msgbuf
),
383 "%3.2f%% done, finished in %d:%02d\n",
384 (blockswritten
* 100.0) / tapesize
,
385 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
393 * information message upon receipt of SIGINFO
396 statussig(int notused
)
398 int save_errno
= errno
;
401 buf
= mktimeest(time(NULL
));
403 write(STDERR_FILENO
, buf
, strlen(buf
));
413 struct slave_results returned
;
415 int siz
= (char *)nextblock
- (char *)slp
->req
;
417 slp
->req
[trecno
].count
= 0; /* Sentinel */
419 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
420 quit("error writing command pipe: %s\n", strerror(errno
));
421 slp
->sent
= 1; /* we sent a request, read the response later */
423 lastfirstrec
= slp
->firstrec
;
425 if (++slp
>= &slaves
[SLAVES
])
428 /* Read results back from next slave */
430 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
431 != sizeof returned
) {
432 perror(" DUMP: error reading command pipe in master");
435 got
= returned
.unclen
;
436 bytes_written
+= returned
.clen
;
437 if (returned
.unclen
== returned
.clen
)
441 /* Check for errors or end of tape */
443 /* Check for errors */
447 msg("End of tape detected\n");
450 * Drain the results, don't care what the values were.
451 * If we read them here then trewind won't...
453 for (i
= 0; i
< SLAVES
; i
++) {
454 if (slaves
[i
].sent
) {
455 if (dump_atomic_read( slaves
[i
].fd
,
456 (char *)&returned
, sizeof returned
)
457 != sizeof returned
) {
458 perror(" DUMP: error reading command pipe in master");
472 if (spcl
.c_type
!= TS_END
) {
473 for (i
= 0; i
< spcl
.c_count
; i
++)
474 if (spcl
.c_addr
[i
] != 0)
477 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
478 slp
->tapea
= spcl
.c_tapea
;
479 slp
->firstrec
= lastfirstrec
+ ntrec
;
481 nextblock
= slp
->tblock
;
483 asize
+= tenths
+ returned
.clen
/ density
;
484 blockswritten
+= ntrec
;
485 blocksthisvol
+= ntrec
;
486 if (!pipeout
&& !unlimited
) {
488 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfile
) * 1024)
489 : blocksthisvol
>= blocksperfile
) {
494 else if (asize
> tsize
) {
507 struct slave_results returned
;
509 for (f
= 0; f
< SLAVES
; f
++) {
511 * Drain the results, but unlike EOT we DO (or should) care
512 * what the return values were, since if we detect EOT after
513 * we think we've written the last blocks to the tape anyway,
514 * we have to replay those blocks with rollforward.
516 * fixme: punt for now.
518 if (slaves
[f
].sent
) {
519 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
520 != sizeof returned
) {
521 perror(" DUMP: error reading command pipe in master");
524 got
= returned
.unclen
;
525 bytes_written
+= returned
.clen
;
526 if (returned
.unclen
== returned
.clen
)
534 msg("EOT detected in last 2 tape records!\n");
535 msg("Use a longer tape, decrease the size estimate\n");
536 quit("or use no size estimate at all.\n");
539 (void) close(slaves
[f
].fd
);
541 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
546 msg("Closing %s\n", tape
);
551 while (rmtopen(tape
, O_RDONLY
) < 0)
558 (void) close(tapefd
);
560 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
575 if (nexttape
|| Mflag
)
578 msg("Launching %s\n", eot_script
);
579 eot_code
= system_command(eot_script
, tape
, tapeno
);
581 if (eot_code
!= 0 && eot_code
!= 1) {
582 msg("Dump aborted by the end of tape script\n");
588 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
589 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
591 while (!query("Is the new volume mounted and ready to go?"))
592 if (query("Do you want to abort?")) {
601 struct req
*p
, *q
= NULL
, *prev
;
603 int i
, size
, savedtapea
, got
;
604 union u_spcl
*ntb
, *otb
;
605 struct slave_results returned
;
610 tslp
= &slaves
[SLAVES
];
611 ntb
= (union u_spcl
*)tslp
->tblock
[1];
614 * Each of the N slaves should have requests that need to
615 * be replayed on the next tape. Use the extra slave buffers
616 * (slaves[SLAVES]) to construct request lists to be sent to
617 * each slave in turn.
619 for (i
= 0; i
< SLAVES
; i
++) {
621 otb
= (union u_spcl
*)slp
->tblock
;
624 * For each request in the current slave, copy it to tslp.
628 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
631 *ntb
++ = *otb
++; /* copy the datablock also */
636 quit("rollforward: protocol botch");
648 nextblock
= tslp
->tblock
;
649 savedtapea
= spcl
.c_tapea
;
650 spcl
.c_tapea
= slp
->tapea
;
652 spcl
.c_tapea
= savedtapea
;
653 lastspclrec
= savedtapea
- 1;
655 size
= (char *)ntb
- (char *)q
;
656 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
657 perror(" DUMP: error writing command pipe");
662 lastfirstrec
= slp
->firstrec
;
664 if (++slp
>= &slaves
[SLAVES
])
669 if (prev
->dblk
!= 0) {
671 * If the last one was a disk block, make the
672 * first of this one be the last bit of that disk
675 q
->dblk
= prev
->dblk
+
676 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
677 ntb
= (union u_spcl
*)tslp
->tblock
;
680 * It wasn't a disk block. Copy the data to its
681 * new location in the buffer.
684 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
685 ntb
= (union u_spcl
*)tslp
->tblock
[1];
689 nextblock
= slp
->tblock
;
692 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
693 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
700 * Clear the first slaves' response. One hopes that it
701 * worked ok, otherwise the tape is much too short!
704 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
705 != sizeof returned
) {
706 perror(" DUMP: error reading command pipe in master");
709 got
= returned
.unclen
;
710 bytes_written
+= returned
.clen
;
711 if (returned
.clen
== returned
.unclen
)
719 quit("EOT detected at start of the tape!\n");
725 if (spcl
.c_type
!= TS_END
) {
726 for (i
= 0; i
< spcl
.c_count
; i
++)
727 if (spcl
.c_addr
[i
] != 0)
731 slp
->firstrec
= lastfirstrec
+ ntrec
;
732 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
734 asize
+= tenths
+ returned
.clen
/ density
;
735 blockswritten
+= ntrec
;
736 blocksthisvol
+= ntrec
;
741 * We implement taking and restoring checkpoints on the tape level.
742 * When each tape is opened, a new process is created by forking; this
743 * saves all of the necessary context in the parent. The child
744 * continues the dump; the parent waits around, saving the context.
745 * If the child returns X_REWRITE, then it had problems writing that tape;
746 * this causes the parent to fork again, duplicating the context, and
747 * everything continues as if nothing had happened.
750 startnewtape(int top
)
761 sigaddset(&sigs
, SIGINT
);
762 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
763 #else /* __linux__ */
765 void (*interrupt_save
)();
767 sig_t interrupt_save
;
769 interrupt_save
= signal(SIGINT
, SIG_IGN
);
770 #endif /* __linux__ */
772 parentpid
= getpid();
773 tapea_volume
= spcl
.c_tapea
;
774 tapea_bytes
= bytes_written
;
775 tstart_volume
= time(NULL
);
779 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
781 (void)signal(SIGINT
, interrupt_save
);
784 * All signals are inherited...
788 msg("Context save fork fails in parent %d\n", parentpid
);
794 * save the context by waiting
795 * until the child doing all of the work returns.
796 * don't catch the interrupt
799 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
801 signal(SIGINT
, SIG_IGN
);
804 msg("Tape: %d; parent process: %d child process %d\n",
805 tapeno
+1, parentpid
, childpid
);
807 while ((waitpid
= wait(&status
)) != childpid
)
808 if (waitpid
!= rshpid
)
809 msg("Parent %d waiting for child %d has another child %d return\n",
810 parentpid
, childpid
, waitpid
);
812 msg("Child %d returns LOB status %o\n",
813 childpid
, status
&0xFF);
815 status
= (status
>> 8) & 0xFF;
819 msg("Child %d finishes X_FINOK\n", childpid
);
822 msg("Child %d finishes X_ABORT\n", childpid
);
825 msg("Child %d finishes X_REWRITE\n", childpid
);
828 msg("Child %d finishes unknown %d\n",
839 goto restore_check_point
;
841 msg("Bad return code from dump: %d\n", status
);
845 } else { /* we are the child; just continue */
847 sleep(4); /* allow time for parent's message to get out */
848 msg("Child on Tape %d has parent %d, my pid = %d\n",
849 tapeno
+1, parentpid
, getpid());
852 * If we have a name like "/dev/rmt0,/dev/rmt1",
853 * use the name before the comma first, and save
854 * the remaining names for subsequent volumes.
856 tapeno
++; /* current tape sequence */
858 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
859 tape
[MAXPATHLEN
- 1] = '\0';
860 msg("Dumping volume %d on %s\n", tapeno
, tape
);
862 else if (nexttape
|| strchr(tapeprefix
, ',')) {
863 if (nexttape
&& *nexttape
)
864 tapeprefix
= nexttape
;
865 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
870 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
871 tape
[MAXPATHLEN
- 1] = '\0';
872 msg("Dumping volume %d on %s\n", tapeno
, tape
);
875 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
877 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
879 while ((tapefd
= (pipeout
? fileno(stdout
) :
880 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
883 msg("Cannot open output \"%s\": %s\n", tape
,
885 if (!query("Do you want to retry the open?"))
892 struct mtget mt_stat
;
893 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
895 msg("Output is to %s\n",
896 magtapeout ? "tape" : "file/pipe");
900 enslave(); /* Share open tape file descriptor with slaves */
905 newtape
++; /* new tape signal */
906 spcl
.c_count
= slp
->count
;
908 * measure firstrec in TP_BSIZE units since restore doesn't
909 * know the correct ntrec value...
911 spcl
.c_firstrec
= slp
->firstrec
;
913 spcl
.c_type
= TS_TAPE
;
914 spcl
.c_flags
|= DR_NEWHEADER
;
915 spcl
.c_ntrec
= ntrec
;
917 spcl
.c_flags
|= DR_COMPRESSED
;
918 writeheader((dump_ino_t
)slp
->inode
);
919 spcl
.c_flags
&=~ DR_NEWHEADER
;
920 msg("Volume %d started with block %ld at: %s", tapeno
,
921 spcl
.c_tapea
, ctime(&tstart_volume
));
923 msg("Volume %d begins with blocks from inode %d\n",
925 if (tapeno
< (int)TP_NINOS
)
926 volinfo
[tapeno
] = slp
->inode
;
931 dumpabort(UNUSED(int signo
))
934 if (master
!= 0 && master
!= getpid())
935 /* Signals master to call dumpabort */
936 (void) kill(master
, SIGTERM
);
939 msg("The ENTIRE dump is aborted.\n");
952 msg("pid = %d exits with status %d\n", getpid(), status
);
958 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
961 proceed(UNUSED(int signo
))
964 siglongjmp(jmpbuf
, 1);
972 #ifdef LINUX_FORK_BUG
980 { struct sigaction sa
;
981 memset(&sa
, 0, sizeof sa
);
982 sigemptyset(&sa
.sa_mask
);
983 sa
.sa_handler
= dumpabort
;
984 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
985 sa
.sa_handler
= sigpipe
;
986 sigaction(SIGPIPE
, &sa
, NULL
);
987 sa
.sa_handler
= proceed
;
988 sa
.sa_flags
= SA_RESTART
;
989 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
992 for (i
= 0; i
< SLAVES
; i
++) {
993 if (i
== slp
- &slaves
[0]) {
999 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
1000 (slaves
[i
].pid
= fork()) < 0)
1001 quit("too many slaves, %d (recompile smaller): %s\n",
1002 i
, strerror(errno
));
1004 slaves
[i
].fd
= cmd
[1];
1006 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1008 for (j
= 0; j
<= i
; j
++)
1009 (void) close(slaves
[j
].fd
);
1011 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1012 #if defined(SIGINFO)
1013 sigaddset(&sigs
, SIGINFO
);
1015 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1017 #ifdef LINUX_FORK_BUG
1018 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1020 quit("master/slave protocol botched 3\n");
1026 (slaves
[i
].pid
== slp
->pid
));
1033 #ifdef LINUX_FORK_BUG
1035 * Wait for all slaves to _actually_ start to circumvent a bug in
1036 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1037 * returned from fork() causes a SEGV in the child process
1039 for (i
= 0; i
< SLAVES
; i
++)
1040 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1041 quit("master/slave protocol botched 4\n");
1044 for (i
= 0; i
< SLAVES
; i
++)
1045 (void) dump_atomic_write( slaves
[i
].fd
,
1046 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1047 sizeof slaves
[0].pid
);
1057 for (i
= 0; i
< SLAVES
; i
++)
1058 if (slaves
[i
].pid
> 0) {
1059 (void) kill(slaves
[i
].pid
, SIGKILL
);
1065 * Synchronization - each process waits for a SIGUSR2 from the
1066 * previous process before writing to the tape, and sends SIGUSR2
1067 * to the next process when the tape write completes. On tape errors
1068 * a SIGUSR1 is sent to the master which then terminates all of the
1080 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1081 char * volatile buffer
;
1082 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1083 struct tapebuf
* volatile comp_buf
= NULL
;
1085 volatile int do_compress
= !first
;
1086 unsigned long worklen
;
1088 unsigned int worklen2
;
1091 lzo_align_t __LZO_MMODEL
*LZO_WorkMem
;
1093 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1094 struct slave_results returns
;
1099 long long curtapepos
;
1100 union u_spcl
*uspclptr
;
1101 struct s_spcl
*spclptr
;
1102 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1103 long maxntrecs
= 50000; /* every 50MB */
1104 long cntntrecs
= maxntrecs
;
1105 #endif /* USE_QFA */
1109 sigaddset(&set
, SIGUSR2
);
1110 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1114 * Need our own seek pointer.
1116 (void) close(diskfd
);
1117 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1118 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1121 (void)ioctl(diskfd
, BLKFLSBUF
, 0);
1124 retval
= dump_fs_open(disk
, &fs
);
1126 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1127 #endif /* __linux__ */
1130 * Need the pid of the next slave in the loop...
1132 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1133 != sizeof nextslave
) {
1134 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1137 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1138 /* if we're doing a compressed dump, allocate the compress buffer */
1140 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1141 /* Add extra space to deal with compression enlarging the buffer */
1142 if (TP_BSIZE
> writesize
/16 + 67)
1145 bsiz
+= writesize
/16 + 67;
1146 comp_buf
= malloc(bsiz
);
1147 if (comp_buf
== NULL
)
1148 quit("couldn't allocate a compress buffer.\n");
1149 if (zipflag
== COMPRESS_ZLIB
)
1150 comp_buf
->flags
= COMPRESS_ZLIB
;
1151 else if (zipflag
== COMPRESS_BZLIB
)
1152 comp_buf
->flags
= COMPRESS_BZLIB
;
1153 else if (zipflag
== COMPRESS_LZO
) {
1154 comp_buf
->flags
= COMPRESS_LZO
;
1155 if (lzo_init() != LZO_E_OK
) quit("lzo_init failed\n");
1157 quit("internal error - unknown compression method: %d\n", zipflag
);
1160 LZO_WorkMem
= malloc(LZO1X_1_MEM_COMPRESS
);
1162 quit("couldn't allocate a compress buffer.\n");
1164 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1167 * Get list of blocks to dump, read the blocks into tape buffer
1169 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1170 struct req
*p
= slp
->req
;
1172 for (trecno
= 0; trecno
< ntrec
;
1173 trecno
+= p
->count
, p
+= p
->count
) {
1174 if (p
->dblk
) { /* read a disk block */
1175 bread(p
->dblk
, slp
->tblock
[trecno
],
1176 p
->count
* TP_BSIZE
);
1177 } else { /* read record from pipe */
1178 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1179 (char *)slp
->tblock
[trecno
],
1180 TP_BSIZE
) != TP_BSIZE
)
1181 quit("master/slave protocol botched.\n");
1185 /* Try to write the data... */
1189 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1190 bufsize
= writesize
; /* length to write */
1191 returns
.clen
= returns
.unclen
= bufsize
;
1193 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1195 * When writing a compressed dump, each block except
1196 * the first one on each tape is written
1197 * from struct tapebuf with an 4 byte prefix
1198 * followed by the data. This can be less than
1199 * writesize. Restore, on a short read, can compare the
1200 * length read to the compressed length in the header
1201 * to verify that the read was good. Blocks which don't
1202 * compress well are written uncompressed.
1203 * The first block written by each slave is not compressed
1204 * and does not have a prefix.
1207 if (compressed
&& do_compress
) {
1208 comp_buf
->length
= bufsize
;
1209 worklen
= TP_BSIZE
+ writesize
;
1212 if (zipflag
== COMPRESS_ZLIB
) {
1213 compresult
= compress2(comp_buf
->buf
,
1215 (char *)slp
->tblock
[0],
1218 if (compresult
== Z_OK
)
1223 #endif /* HAVE_ZLIB */
1225 if (zipflag
== COMPRESS_BZLIB
) {
1227 compresult
= BZ2_bzBuffToBuffCompress(
1230 (char *)slp
->tblock
[0],
1235 if (compresult
== BZ_OK
)
1241 #endif /* HAVE_BZLIB */
1243 if (zipflag
== COMPRESS_LZO
) {
1244 compresult
= lzo1x_1_compress((char *)slp
->tblock
[0],writesize
,
1246 (lzo_uintp
)&worklen
,
1248 if (compresult
== LZO_E_OK
)
1253 #endif /* HAVE_LZO */
1254 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1255 /* write the compressed buffer */
1256 comp_buf
->length
= worklen
;
1257 comp_buf
->compressed
= 1;
1258 buffer
= (char *) comp_buf
;
1259 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1262 /* write the data uncompressed */
1263 comp_buf
->length
= writesize
;
1264 comp_buf
->compressed
= 0;
1265 buffer
= (char *) comp_buf
;
1266 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1267 returns
.unclen
= returns
.clen
;
1268 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1271 /* compress the remaining blocks if we're compressing */
1272 do_compress
= compressed
;
1273 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1275 if (sigsetjmp(jmpbuf
, 1) == 0) {
1284 if (gTapeposfd
>= 0) {
1288 for (i
= 0; (i
< ntrec
) && !foundone
; ++i
) {
1289 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1290 spclptr
= &uspclptr
->s_spcl
;
1291 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1292 (spclptr
->c_type
== TS_INODE
) &&
1293 (spclptr
->c_date
== gThisDumpDate
) &&
1294 !(spclptr
->c_dinode
.di_mode
& S_IFDIR
)
1297 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1300 gtperr
= GetTapePos(&curtapepos
);
1301 /* if an error occured previously don't
1305 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1307 gtperr
= MkTapeString(spclptr
, curtapepos
);
1313 #endif /* USE_QFA */
1315 while (eot_count
< 10 && size
< bufsize
) {
1318 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1321 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1323 printf("slave %d wrote %d\n", slave_number
, wrote
);
1325 if (wrote
< 0 && errno
!= ENOSPC
)
1327 if (wrote
== 0 || (wrote
< 0 && errno
== ENOSPC
))
1334 if (size
!= bufsize
)
1335 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1336 slave_number
, size
, bufsize
);
1340 * Handle ENOSPC as an EOT condition.
1342 if (wrote
< 0 && errno
== ENOSPC
) {
1348 returns
.clen
= returns
.unclen
= 0;
1351 * pass errno back to master for special handling
1354 returns
.unclen
= -errno
;
1357 * pass size of data and size of write back to master
1358 * (for EOT handling)
1360 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1363 * Signal the next slave to go.
1365 (void) kill(nextslave
, SIGUSR2
);
1367 if (gTapeposfd
>= 0) {
1370 #endif /* USE_QFA */
1373 quit("error reading command pipe: %s\n", strerror(errno
));
1377 * Since a read from a pipe may not return all we asked for,
1378 * or a write may not write all we ask if we get a signal,
1379 * loop until the count is satisfied (or error).
1382 dump_atomic_read(int fd
, void *buf
, size_t count
)
1384 int got
, need
= count
;
1387 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1389 } while (got
== -1 && errno
== EINTR
);
1390 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1394 * Since a read from a pipe may not return all we asked for,
1395 * or a write may not write all we ask if we get a signal,
1396 * loop until the count is satisfied (or error).
1399 dump_atomic_write(int fd
, const void *buf
, size_t count
)
1401 int got
, need
= count
;
1404 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1406 } while (got
== -1 && errno
== EINTR
);
1407 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1418 buf.mt_op = MTSETDRVBUFFER;
1419 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1420 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1422 msg("[%ld] error: %d (setting logical)\n",
1423 (unsigned long)getpid(), err);
1430 #define LSEEK_GET_TAPEPOS 10
1431 #define LSEEK_GO2_TAPEPOS 11
1433 * read the current tape position
1436 GetTapePos(long long *pos
)
1442 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
1451 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1452 *pos
= (long long)mtpos
;
1455 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1461 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1469 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
)
1474 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1477 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1478 (unsigned long)spclptr
->c_inumber
,
1481 gTps
[sizeof(gTps
) - 1] = '\0';
1482 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1484 warn("error writing tapepos file. (error %d)\n", errno
);
1488 #endif /* USE_QFA */