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.96 2011/06/10 13:41:41 stelian Exp $";
44 #include <compatlfs.h>
50 #include <compaterr.h>
61 #include <sys/types.h>
63 #include <sys/ioctl.h>
64 #include <sys/mount.h> /* for definition of BLKFLSBUF */
65 #ifndef BLKFLSBUF /* last resort... */
66 #define BLKFLSBUF _IO(0x12, 97) /* Flush buffer cache. */
70 #include <sys/param.h>
71 #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>
82 #include <bsdcompat.h>
84 #include <sys/vnode.h>
87 #include <ufs/inode.h>
89 #include <ufs/ufs/dinode.h>
90 #include <ufs/ffs/fs.h>
91 #endif /* __linux__ */
93 #include <protocols/dumprestore.h>
99 int writesize
; /* size of malloc()ed buffer for tape */
100 long lastspclrec
= -1; /* tape block number of last written header */
101 int trecno
= 0; /* next record to write in current block */
102 extern long *blocksperfiles
; /* number of blocks per output file(s) */
103 long blocksperfiles_current
; /* current position in blocksperfiles */
104 long blocksthisvol
; /* number of blocks on current output file */
105 extern int ntrec
; /* blocking factor on tape */
106 extern int cartridge
;
110 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
111 static int magtapeout
; /* output is really a tape */
113 static ssize_t dump_atomic_read
__P((int, char *, size_t));
114 static ssize_t dump_atomic_write
__P((int, const char *, size_t));
116 static void doslave
__P((int, int, int));
118 static void doslave
__P((int, int));
120 static void enslave
__P((void));
121 static void flushtape
__P((void));
122 static void killall
__P((void));
123 static void rollforward
__P((void));
127 struct slave_results
{
128 ssize_t unclen
; /* uncompressed length */
129 ssize_t clen
; /* compressed length */
132 struct slave slaves
[SLAVES
+1];
135 char (*nextblock
)[TP_BSIZE
];
137 static time_t tstart_volume
; /* time of volume start */
138 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
140 int master
; /* pid of master, for sending error signals */
141 int tenths
; /* length of tape overhead per block written */
142 static int caught1
; /* have we caught the signal to proceed? */
143 static int ready1
; /* have we reached the lock point without having */
144 /* received the SIGUSR2 signal from the prev slave? */
145 static sigjmp_buf jmpbuf1
; /* where to jump to if we are ready when the */
146 /* SIGUSR1 arrives from the previous slave */
147 static int caught2
; /* have we caught the signal to proceed? */
148 static int ready2
; /* have we reached the lock point without having */
149 /* received the SIGUSR2 signal from the prev slave? */
150 static sigjmp_buf jmpbuf2
; /* where to jump to if we are ready when the */
151 /* SIGUSR2 arrives from the previous slave */
154 * Determine if we can use Linux' clone system call. If so, call it
155 * with the CLONE_IO flag so that all processes will share the same I/O
156 * context, allowing the I/O schedulers to make better scheduling decisions.
159 /* first, pull in the header files that define sys_clone and CLONE_IO */
166 /* If either is not present, fall back on the fork behaviour */
167 #if ! defined(SYS_clone) || ! defined (CLONE_IO)
168 #define fork_clone_io fork
169 #else /* SYS_clone */
170 /* CLONE_IO is available, determine which version of sys_clone to use */
171 #include <linux/version.h>
173 * Kernel 2.5.49 introduced two extra parameters to the clone system call.
174 * Neither is useful in our case, so this is easy to handle.
176 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,49)
178 * Parameters of the sys_clone syscall are
179 * clone_flags, child_stack, parent_tidptr, child_tidptr
180 * on all architectures except s390 and s390x
181 * s390* have child_stack, clone_flags, parent_tidptr, child_tidptr
183 #if defined(__s390__) || defined(__s390x__)
184 #define CLONE_ARGS 0, SIGCHLD|CLONE_IO, NULL, NULL
186 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0, NULL, NULL
189 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0
190 #endif /* LINUX_VERSION_CODE */
191 pid_t
fork_clone_io(void);
192 #endif /* SYS_clone */
193 #else /* __linux__ not defined */
194 #define fork_clone_io fork
195 #endif /* __linux__ */
200 int pgoff
= getpagesize() - 1;
204 writesize
= ntrec
* TP_BSIZE
;
205 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
207 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
208 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
209 * repositioning after stopping, i.e, streaming mode, where the gap is
210 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
212 if (!blocksperfiles
&& !unlimited
)
213 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
219 * Allocate tape buffer contiguous with the array of instruction
220 * packets, so flushtape() can write them together with one write().
221 * Align tape buffer on page boundary to speed up tape write().
223 for (i
= 0; i
<= SLAVES
; i
++) {
225 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
228 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
230 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
232 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
234 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
240 nextblock
= slp
->tblock
;
245 writerec(const void *dp
, int isspcl
)
247 slp
->req
[trecno
].dblk
= (ext2_loff_t
)0;
248 slp
->req
[trecno
].count
= 1;
249 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
250 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
252 /* Need to write it to the archive file */
253 indexer
->writerec(dp
, isspcl
);
257 lastspclrec
= spcl
.c_tapea
;
265 dumpblock(blk_t blkno
, int size
)
270 dblkno
= fsbtodb(sblock
, blkno
);
271 tpblks
= size
>> tp_bshift
;
272 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
273 slp
->req
[trecno
].dblk
= dblkno
;
274 slp
->req
[trecno
].count
= avail
;
276 spcl
.c_tapea
+= avail
;
279 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
291 msg("write error on %s: %s\n", tape
, strerror(errnum
));
292 quit("Cannot recover\n");
295 msg("write error %d blocks into volume %d: %s\n",
296 blocksthisvol
, tapeno
, strerror(errnum
));
297 broadcast("DUMP WRITE ERROR!\n");
298 if (query("Do you want to rewrite this volume?")) {
299 msg("Closing this volume. Prepare to restart with new media;\n");
300 msg("this dump volume will be rewritten.\n");
306 if (query("Do you want to start the next tape?"))
312 sigpipe(UNUSED(int signo
))
315 quit("Broken pipe\n");
320 * Update xferrate stats
329 ttaken
= tnow
- tstart_volume
;
330 blocks
= spcl
.c_tapea
- tapea_volume
;
331 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
333 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
334 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
336 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
337 long txfrate
= volkb
/ ttaken
;
338 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
339 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
340 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
344 double rate
= .0005 + (double) blocks
/ (double) volkb
;
345 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
347 tapeno
, blocks
, volkb
, rate
);
354 mktimeest(time_t tnow
)
356 static char msgbuf
[128];
361 if (blockswritten
< 500)
363 if (blockswritten
> tapesize
)
364 tapesize
= blockswritten
;
365 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
366 / blockswritten
* tapesize
;
367 if (tnow
> tstart_volume
)
368 (void)snprintf(msgbuf
, sizeof(msgbuf
),
369 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
370 (blockswritten
* 100.0) / tapesize
,
371 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
372 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
374 (void)snprintf(msgbuf
, sizeof(msgbuf
),
375 "%3.2f%% done, finished in %d:%02d\n",
376 (blockswritten
* 100.0) / tapesize
,
377 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
385 * information message upon receipt of SIGINFO
388 statussig(int notused
)
390 int save_errno
= errno
;
393 buf
= mktimeest(time(NULL
));
395 write(STDERR_FILENO
, buf
, strlen(buf
));
405 struct slave_results returned
;
407 int siz
= (char *)nextblock
- (char *)slp
->req
;
409 /* make sure returned has sane values in case we don't read
410 * them from the slave in this pass */
411 returned
.unclen
= returned
.clen
= writesize
;
413 slp
->req
[trecno
].count
= 0; /* Sentinel */
415 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
416 quit("error writing command pipe: %s\n", strerror(errno
));
417 slp
->sent
= 1; /* we sent a request, read the response later */
419 lastfirstrec
= slp
->firstrec
;
421 if (++slp
>= &slaves
[SLAVES
])
424 /* Read results back from next slave */
426 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
427 != sizeof returned
) {
428 perror(" DUMP: error reading command pipe in master");
431 got
= returned
.unclen
;
432 bytes_written
+= returned
.clen
;
433 if (returned
.unclen
== returned
.clen
)
437 /* Check for errors or end of tape */
439 /* Check for errors */
443 msg("End of tape detected\n");
446 * Drain the results, don't care what the values were.
447 * If we read them here then trewind won't...
449 for (i
= 0; i
< SLAVES
; i
++) {
450 if (slaves
[i
].sent
) {
451 if (dump_atomic_read( slaves
[i
].fd
,
452 (char *)&returned
, sizeof returned
)
453 != sizeof returned
) {
454 perror(" DUMP: error reading command pipe in master");
468 if (spcl
.c_type
== TS_CLRI
|| spcl
.c_type
== TS_BITS
)
471 if (spcl
.c_type
!= TS_END
) {
472 for (i
= 0; i
< spcl
.c_count
; i
++)
473 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
) {
487 if (blocksperfiles
&& blocksperfiles
[blocksperfiles_current
]) {
488 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfiles
[blocksperfiles_current
]) * 1024)
489 : blocksthisvol
>= blocksperfiles
[blocksperfiles_current
] ) {
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)
576 msg("Launching %s\n", eot_script
);
577 eot_code
= system_command(eot_script
, tape
, tapeno
);
579 if (eot_code
!= 0 && eot_code
!= 1) {
580 msg("Dump aborted by the end of tape script\n");
585 if (nexttape
|| Mflag
)
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];
613 /* make sure returned has sane values in case we don't read
614 * them from the slave in this pass */
615 returned
.unclen
= returned
.clen
= writesize
;
618 * Each of the N slaves should have requests that need to
619 * be replayed on the next tape. Use the extra slave buffers
620 * (slaves[SLAVES]) to construct request lists to be sent to
621 * each slave in turn.
623 for (i
= 0; i
< SLAVES
; i
++) {
625 otb
= (union u_spcl
*)slp
->tblock
;
628 * For each request in the current slave, copy it to tslp.
632 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
635 *ntb
++ = *otb
++; /* copy the datablock also */
640 quit("rollforward: protocol botch");
652 nextblock
= tslp
->tblock
;
653 savedtapea
= spcl
.c_tapea
;
654 spcl
.c_tapea
= slp
->tapea
;
656 spcl
.c_tapea
= savedtapea
;
657 lastspclrec
= savedtapea
- 1;
659 size
= (char *)ntb
- (char *)q
;
660 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
661 perror(" DUMP: error writing command pipe");
666 lastfirstrec
= slp
->firstrec
;
668 if (++slp
>= &slaves
[SLAVES
])
673 if (prev
->dblk
!= 0) {
675 * If the last one was a disk block, make the
676 * first of this one be the last bit of that disk
679 q
->dblk
= prev
->dblk
+
680 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
681 ntb
= (union u_spcl
*)tslp
->tblock
;
684 * It wasn't a disk block. Copy the data to its
685 * new location in the buffer.
688 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
689 ntb
= (union u_spcl
*)tslp
->tblock
[1];
693 nextblock
= slp
->tblock
;
696 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
697 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
704 * Clear the first slaves' response. One hopes that it
705 * worked ok, otherwise the tape is much too short!
708 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
709 != sizeof returned
) {
710 perror(" DUMP: error reading command pipe in master");
713 got
= returned
.unclen
;
714 bytes_written
+= returned
.clen
;
715 if (returned
.clen
== returned
.unclen
)
723 quit("EOT detected at start of the tape!\n");
729 if (spcl
.c_type
!= TS_END
) {
730 for (i
= 0; i
< spcl
.c_count
; i
++)
731 if (spcl
.c_addr
[i
] != 0)
735 slp
->firstrec
= lastfirstrec
+ ntrec
;
736 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
738 asize
+= tenths
+ returned
.clen
/ density
;
739 blockswritten
+= ntrec
;
740 blocksthisvol
+= ntrec
;
745 #if defined(SYS_clone) && defined(CLONE_IO)
749 return syscall(SYS_clone
, CLONE_ARGS
);
755 * We implement taking and restoring checkpoints on the tape level.
756 * When each tape is opened, a new process is created by forking; this
757 * saves all of the necessary context in the parent. The child
758 * continues the dump; the parent waits around, saving the context.
759 * If the child returns X_REWRITE, then it had problems writing that tape;
760 * this causes the parent to fork again, duplicating the context, and
761 * everything continues as if nothing had happened.
764 startnewtape(int top
)
775 sigaddset(&sigs
, SIGINT
);
776 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
777 #else /* __linux__ */
779 void (*interrupt_save
)();
781 sig_t interrupt_save
;
783 interrupt_save
= signal(SIGINT
, SIG_IGN
);
784 #endif /* __linux__ */
786 parentpid
= getpid();
787 tapea_volume
= spcl
.c_tapea
;
788 tapea_bytes
= bytes_written
;
789 tstart_volume
= time(NULL
);
793 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
795 (void)signal(SIGINT
, interrupt_save
);
798 * All signals are inherited...
800 childpid
= fork_clone_io();
802 msg("Context save fork fails in parent %d\n", parentpid
);
808 * save the context by waiting
809 * until the child doing all of the work returns.
810 * don't catch the interrupt
813 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
815 signal(SIGINT
, SIG_IGN
);
818 msg("Tape: %d; parent process: %d child process %d\n",
819 tapeno
+1, parentpid
, childpid
);
821 while ((waitpid
= wait(&status
)) != childpid
)
822 if (waitpid
!= rshpid
)
823 msg("Parent %d waiting for child %d has another child %d return\n",
824 parentpid
, childpid
, waitpid
);
826 msg("Child %d returns LOB status %o\n",
827 childpid
, status
&0xFF);
829 status
= (status
>> 8) & 0xFF;
833 msg("Child %d finishes X_FINOK\n", childpid
);
836 msg("Child %d finishes X_ABORT\n", childpid
);
839 msg("Child %d finishes X_REWRITE\n", childpid
);
842 msg("Child %d finishes unknown %d\n",
853 goto restore_check_point
;
855 msg("Bad return code from dump: %d\n", status
);
859 } else { /* we are the child; just continue */
861 sleep(4); /* allow time for parent's message to get out */
862 msg("Child on Tape %d has parent %d, my pid = %d\n",
863 tapeno
+1, parentpid
, getpid());
866 * If we have a name like "/dev/rmt0,/dev/rmt1",
867 * use the name before the comma first, and save
868 * the remaining names for subsequent volumes.
870 tapeno
++; /* current tape sequence */
872 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
873 tape
[MAXPATHLEN
- 1] = '\0';
874 msg("Dumping volume %d on %s\n", tapeno
, tape
);
876 else if (nexttape
|| strchr(tapeprefix
, ',')) {
877 if (nexttape
&& *nexttape
)
878 tapeprefix
= nexttape
;
879 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
884 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
885 tape
[MAXPATHLEN
- 1] = '\0';
886 msg("Dumping volume %d on %s\n", tapeno
, tape
);
888 if (blocksperfiles
&& blocksperfiles_current
< *blocksperfiles
)
889 blocksperfiles_current
++;
891 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
893 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
895 while ((tapefd
= (pipeout
? fileno(stdout
) :
896 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
899 msg("Cannot open output \"%s\": %s\n", tape
,
901 if (!query("Do you want to retry the open?"))
908 struct mtget mt_stat
;
909 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
911 msg("Output is to %s\n",
912 magtapeout ? "tape" : "file/pipe");
916 enslave(); /* Share open tape file descriptor with slaves */
921 newtape
++; /* new tape signal */
922 spcl
.c_count
= slp
->count
;
924 * measure firstrec in TP_BSIZE units since restore doesn't
925 * know the correct ntrec value...
927 spcl
.c_firstrec
= slp
->firstrec
;
929 spcl
.c_type
= TS_TAPE
;
930 spcl
.c_flags
|= DR_NEWHEADER
;
931 spcl
.c_ntrec
= ntrec
;
933 spcl
.c_flags
|= DR_COMPRESSED
;
934 writeheader((dump_ino_t
)slp
->inode
);
935 spcl
.c_flags
&=~ DR_NEWHEADER
;
936 msg("Volume %d started with block %ld at: %s", tapeno
,
937 spcl
.c_tapea
, ctime(&tstart_volume
));
939 msg("Volume %d begins with blocks from inode %d\n",
941 if (tapeno
< (int)TP_NINOS
)
942 volinfo
[tapeno
] = slp
->inode
;
943 transformation
->startNewTape(transformation
, NULL
, 0);
948 dumpabort(UNUSED(int signo
))
951 if (master
!= 0 && master
!= getpid())
952 /* Signals master to call dumpabort */
953 (void) kill(master
, SIGTERM
);
956 msg("The ENTIRE dump is aborted.\n");
969 msg("pid = %d exits with status %d\n", getpid(), status
);
975 * proceed - handler for SIGUSR1, used to synchronize IO between the slaves.
978 proceed1(UNUSED(int signo
))
981 siglongjmp(jmpbuf1
, 1);
986 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
989 proceed2(UNUSED(int signo
))
992 siglongjmp(jmpbuf2
, 1);
1000 #ifdef LINUX_FORK_BUG
1008 { struct sigaction sa
;
1009 memset(&sa
, 0, sizeof sa
);
1010 sigemptyset(&sa
.sa_mask
);
1011 sa
.sa_handler
= dumpabort
;
1012 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
1013 sa
.sa_handler
= sigpipe
;
1014 sigaction(SIGPIPE
, &sa
, NULL
);
1015 sa
.sa_handler
= proceed1
;
1016 sa
.sa_flags
= SA_RESTART
;
1017 sigaction(SIGUSR1
, &sa
, NULL
); /* Slave sends SIGUSR1 to next slave */
1018 sa
.sa_handler
= proceed2
;
1019 sa
.sa_flags
= SA_RESTART
;
1020 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
1023 for (i
= 0; i
< SLAVES
; i
++) {
1024 if (i
== slp
- &slaves
[0]) {
1032 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
1033 (slaves
[i
].pid
= fork_clone_io()) < 0)
1034 quit("too many slaves, %d (recompile smaller): %s\n",
1035 i
, strerror(errno
));
1037 slaves
[i
].fd
= cmd
[1];
1039 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1041 for (j
= 0; j
<= i
; j
++)
1042 (void) close(slaves
[j
].fd
);
1044 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1045 #if defined(SIGINFO)
1046 sigaddset(&sigs
, SIGINFO
);
1048 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1050 #ifdef LINUX_FORK_BUG
1051 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1053 quit("master/slave protocol botched 3\n");
1059 (slaves
[i
].pid
== slp
->pid
));
1066 #ifdef LINUX_FORK_BUG
1068 * Wait for all slaves to _actually_ start to circumvent a bug in
1069 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1070 * returned from fork() causes a SEGV in the child process
1072 for (i
= 0; i
< SLAVES
; i
++)
1073 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1074 quit("master/slave protocol botched 4\n");
1077 for (i
= 0; i
< SLAVES
; i
++)
1078 (void) dump_atomic_write( slaves
[i
].fd
,
1079 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1080 sizeof slaves
[0].pid
);
1090 for (i
= 0; i
< SLAVES
; i
++)
1091 if (slaves
[i
].pid
> 0) {
1092 (void) kill(slaves
[i
].pid
, SIGKILL
);
1098 * Synchronization - each process waits for a SIGUSR2 from the
1099 * previous process before writing to the tape, and sends SIGUSR2
1100 * to the next process when the tape write completes. On tape errors
1101 * a SIGUSR1 is sent to the master which then terminates all of the
1102 * slaves. Each process sends SIGUSR1 to the next to signal that it
1103 * is time to start reading from the disk, after it finishes reading
1104 * and moves to the compression phase.
1115 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1116 char * volatile buffer
;
1117 #if defined(HAVE_BLOCK_TRANSFORMATION)
1118 struct tapebuf
* volatile comp_buf
= NULL
;
1120 volatile int do_compress
= !first
;
1121 unsigned long worklen
;
1122 #endif /* HAVE_BLOCK_TRANSFORMATION */
1124 struct slave_results returns
;
1129 QFA_State qfa_state
;
1130 #endif /* USE_QFA */
1134 sigaddset(&set
, SIGUSR1
);
1135 sigaddset(&set
, SIGUSR2
);
1136 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1139 #ifdef HAVE_BLOCK_TRANSFORMATION
1140 transformation
->startDiskIOProcess(transformation
);
1141 #endif /* HAVE_BLOCK_TRANSFORMATION */
1143 indexer
->openQfaState(&qfa_state
);
1146 * Need our own seek pointer.
1148 (void) close(diskfd
);
1149 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1150 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1153 (void)ioctl(diskfd
, BLKFLSBUF
, 0);
1156 retval
= dump_fs_open(disk
, &fs
);
1158 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1159 #endif /* __linux__ */
1162 * Need the pid of the next slave in the loop...
1164 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1165 != sizeof nextslave
) {
1166 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1169 #if defined(HAVE_BLOCK_TRANSFORMATION)
1170 /* if we're doing a compressed dump, allocate the compress buffer */
1172 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1173 /* Add extra space to deal with compression or encryption enlarging the buffer */
1174 if (TP_BSIZE
> writesize
/16 + 200)
1177 bsiz
+= writesize
/16 + 200;
1178 comp_buf
= malloc(bsiz
);
1179 if (comp_buf
== NULL
)
1180 quit("couldn't allocate a compress buffer.\n");
1181 transformation
->initialize(transformation
, 1);
1182 if (zipflag
== COMPRESS_ZLIB
)
1183 comp_buf
->flags
= COMPRESS_ZLIB
;
1184 else if (zipflag
== COMPRESS_BZLIB
)
1185 comp_buf
->flags
= COMPRESS_BZLIB
;
1186 else if (zipflag
== COMPRESS_LZO
)
1187 comp_buf
->flags
= COMPRESS_LZO
;
1189 quit("internal error - unknown compression method: %d\n", zipflag
);
1191 #endif /* HAVE_BLOCK_TRANSFORMATION */
1194 * Get list of blocks to dump, read the blocks into tape buffer
1196 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1197 struct req
*p
= slp
->req
;
1199 /* wait for previous slave to finish reading */
1200 if (sigsetjmp(jmpbuf1
, 1) == 0) {
1208 for (trecno
= 0; trecno
< ntrec
;
1209 trecno
+= p
->count
, p
+= p
->count
) {
1211 if (p
->dblk
) { /* read a disk block */
1212 bread(p
->dblk
, slp
->tblock
[trecno
],
1213 p
->count
* TP_BSIZE
);
1214 } else { /* read record from pipe */
1215 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1216 (char *)slp
->tblock
[trecno
],
1217 TP_BSIZE
) != TP_BSIZE
)
1218 quit("master/slave protocol botched.\n");
1221 /* signal next slave to start reading */
1222 (void) kill(nextslave
, SIGUSR1
);
1224 /* Try to write the data... */
1228 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1229 bufsize
= writesize
; /* length to write */
1230 returns
.clen
= returns
.unclen
= bufsize
;
1232 #if defined(HAVE_BLOCK_TRANSFORMATION)
1234 * When writing a compressed dump, each block except
1235 * the first one on each tape is written
1236 * from struct tapebuf with an 4 byte prefix
1237 * followed by the data. This can be less than
1238 * writesize. Restore, on a short read, can compare the
1239 * length read to the compressed length in the header
1240 * to verify that the read was good. Blocks which don't
1241 * compress well are written uncompressed.
1242 * The first block written by each slave is not compressed
1243 * and does not have a prefix.
1245 if (compressed
&& do_compress
) {
1246 comp_buf
->length
= bufsize
;
1247 worklen
= TP_BSIZE
+ writesize
;
1250 // tapebuf: compressed, flags, length
1251 compresult
= transformation
->compress(transformation
, comp_buf
,
1252 &worklen
, slp
->tblock
[0], writesize
);
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_BLOCK_TRANSFORMATION */
1275 if (sigsetjmp(jmpbuf2
, 1) == 0) {
1283 indexer
->updateQfa(&qfa_state
);
1285 while (eot_count
< 10 && size
< bufsize
) {
1288 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1291 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1293 printf("slave %d wrote %d\n", slave_number
, wrote
);
1295 if (wrote
< 0 && errno
!= ENOSPC
)
1297 if (wrote
== 0 || (wrote
< 0 && errno
== ENOSPC
))
1304 if (size
!= bufsize
)
1305 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1306 slave_number
, size
, bufsize
);
1310 * Handle ENOSPC as an EOT condition.
1312 if (wrote
< 0 && errno
== ENOSPC
) {
1318 returns
.clen
= returns
.unclen
= 0;
1321 * pass errno back to master for special handling
1324 returns
.unclen
= -errno
;
1327 * pass size of data and size of write back to master
1328 * (for EOT handling)
1330 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1333 * Signal the next slave to go.
1335 (void) kill(nextslave
, SIGUSR2
);
1337 indexer
->updateQfaState(&qfa_state
);
1340 #ifdef HAVE_BLOCK_TRANSFORMATION
1341 transformation
->endDiskIOProcess(transformation
);
1342 #endif /* HAVE_BLOCK_TRANSFORMATION */
1345 quit("error reading command pipe: %s\n", strerror(errno
));
1349 * Since a read from a pipe may not return all we asked for,
1350 * or a write may not write all we ask if we get a signal,
1351 * loop until the count is satisfied (or error).
1354 dump_atomic_read(int fd
, char *buf
, size_t count
)
1356 int got
, need
= count
;
1359 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1361 } while (got
== -1 && errno
== EINTR
);
1362 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1366 * Since a read from a pipe may not return all we asked for,
1367 * or a write may not write all we ask if we get a signal,
1368 * loop until the count is satisfied (or error).
1371 dump_atomic_write(int fd
, const char *buf
, size_t count
)
1373 int got
, need
= count
;
1376 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1378 } while (got
== -1 && errno
== EINTR
);
1379 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1390 buf.mt_op = MTSETDRVBUFFER;
1391 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1392 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1394 msg("[%ld] error: %d (setting logical)\n",
1395 (unsigned long)getpid(), err);