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.95 2011/06/10 13:07:29 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>
97 int writesize
; /* size of malloc()ed buffer for tape */
98 long lastspclrec
= -1; /* tape block number of last written header */
99 int trecno
= 0; /* next record to write in current block */
100 extern long *blocksperfiles
; /* number of blocks per output file(s) */
101 long blocksperfiles_current
; /* current position in blocksperfiles */
102 long blocksthisvol
; /* number of blocks on current output file */
103 extern int ntrec
; /* blocking factor on tape */
104 extern int cartridge
;
108 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
109 static int magtapeout
; /* output is really a tape */
111 static ssize_t dump_atomic_read
__P((int, char *, size_t));
112 static ssize_t dump_atomic_write
__P((int, const char *, size_t));
114 static void doslave
__P((int, int, int));
116 static void doslave
__P((int, int));
118 static void enslave
__P((void));
119 static void flushtape
__P((void));
120 static void killall
__P((void));
121 static void rollforward
__P((void));
123 static int GetTapePos
__P((long long *));
124 static int MkTapeString
__P((struct s_spcl
*, long long));
125 #define FILESQFAPOS 20
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 caught1
; /* have we caught the signal to proceed? */
170 static int ready1
; /* have we reached the lock point without having */
171 /* received the SIGUSR2 signal from the prev slave? */
172 static sigjmp_buf jmpbuf1
; /* where to jump to if we are ready when the */
173 /* SIGUSR1 arrives from the previous slave */
174 static int caught2
; /* have we caught the signal to proceed? */
175 static int ready2
; /* have we reached the lock point without having */
176 /* received the SIGUSR2 signal from the prev slave? */
177 static sigjmp_buf jmpbuf2
; /* where to jump to if we are ready when the */
178 /* SIGUSR2 arrives from the previous slave */
180 static int gtperr
= 0;
184 * Determine if we can use Linux' clone system call. If so, call it
185 * with the CLONE_IO flag so that all processes will share the same I/O
186 * context, allowing the I/O schedulers to make better scheduling decisions.
189 /* first, pull in the header files that define sys_clone and CLONE_IO */
196 /* If either is not present, fall back on the fork behaviour */
197 #if ! defined(SYS_clone) || ! defined (CLONE_IO)
198 #define fork_clone_io fork
199 #else /* SYS_clone */
200 /* CLONE_IO is available, determine which version of sys_clone to use */
201 #include <linux/version.h>
203 * Kernel 2.5.49 introduced two extra parameters to the clone system call.
204 * Neither is useful in our case, so this is easy to handle.
206 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,49)
208 * Parameters of the sys_clone syscall are
209 * clone_flags, child_stack, parent_tidptr, child_tidptr
210 * on all architectures except s390 and s390x
211 * s390* have child_stack, clone_flags, parent_tidptr, child_tidptr
213 #if defined(__s390__) || defined(__s390x__)
214 #define CLONE_ARGS 0, SIGCHLD|CLONE_IO, NULL, NULL
216 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0, NULL, NULL
219 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0
220 #endif /* LINUX_VERSION_CODE */
221 pid_t
fork_clone_io(void);
222 #endif /* SYS_clone */
223 #else /* __linux__ not defined */
224 #define fork_clone_io fork
225 #endif /* __linux__ */
230 int pgoff
= getpagesize() - 1;
234 writesize
= ntrec
* TP_BSIZE
;
235 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
237 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
238 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
239 * repositioning after stopping, i.e, streaming mode, where the gap is
240 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
242 if (!blocksperfiles
&& !unlimited
)
243 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
249 * Allocate tape buffer contiguous with the array of instruction
250 * packets, so flushtape() can write them together with one write().
251 * Align tape buffer on page boundary to speed up tape write().
253 for (i
= 0; i
<= SLAVES
; i
++) {
255 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
258 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
260 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
262 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
264 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
270 nextblock
= slp
->tblock
;
275 writerec(const void *dp
, int isspcl
)
277 slp
->req
[trecno
].dblk
= (ext2_loff_t
)0;
278 slp
->req
[trecno
].count
= 1;
279 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
280 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
282 /* Need to write it to the archive file */
283 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
285 if (AfileActive
&& Afile
>= 0 && !(spcl
.c_flags
& DR_EXTATTRIBUTES
)) {
286 /* When we dump an inode which is not a directory,
287 * it means we ended the archive contents */
288 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
289 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
293 tmp
= *(union u_spcl
*)dp
;
294 /* Write the record, _uncompressed_ */
296 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
299 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
300 msg("error writing archive file: %s\n",
307 lastspclrec
= spcl
.c_tapea
;
315 dumpblock(blk_t blkno
, int size
)
320 dblkno
= fsbtodb(sblock
, blkno
);
321 tpblks
= size
>> tp_bshift
;
322 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
323 slp
->req
[trecno
].dblk
= dblkno
;
324 slp
->req
[trecno
].count
= avail
;
326 spcl
.c_tapea
+= avail
;
329 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
341 msg("write error on %s: %s\n", tape
, strerror(errnum
));
342 quit("Cannot recover\n");
345 msg("write error %d blocks into volume %d: %s\n",
346 blocksthisvol
, tapeno
, strerror(errnum
));
347 broadcast("DUMP WRITE ERROR!\n");
348 if (query("Do you want to rewrite this volume?")) {
349 msg("Closing this volume. Prepare to restart with new media;\n");
350 msg("this dump volume will be rewritten.\n");
356 if (query("Do you want to start the next tape?"))
362 sigpipe(UNUSED(int signo
))
365 quit("Broken pipe\n");
370 * Update xferrate stats
379 ttaken
= tnow
- tstart_volume
;
380 blocks
= spcl
.c_tapea
- tapea_volume
;
381 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
383 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
384 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
386 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
387 long txfrate
= volkb
/ ttaken
;
388 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
389 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
390 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
394 double rate
= .0005 + (double) blocks
/ (double) volkb
;
395 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
397 tapeno
, blocks
, volkb
, rate
);
404 mktimeest(time_t tnow
)
406 static char msgbuf
[128];
411 if (blockswritten
< 500)
413 if (blockswritten
> tapesize
)
414 tapesize
= blockswritten
;
415 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
416 / blockswritten
* tapesize
;
417 if (tnow
> tstart_volume
)
418 (void)snprintf(msgbuf
, sizeof(msgbuf
),
419 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
420 (blockswritten
* 100.0) / tapesize
,
421 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
422 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
424 (void)snprintf(msgbuf
, sizeof(msgbuf
),
425 "%3.2f%% done, finished in %d:%02d\n",
426 (blockswritten
* 100.0) / tapesize
,
427 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
435 * information message upon receipt of SIGINFO
438 statussig(int notused
)
440 int save_errno
= errno
;
443 buf
= mktimeest(time(NULL
));
445 write(STDERR_FILENO
, buf
, strlen(buf
));
455 struct slave_results returned
;
457 int siz
= (char *)nextblock
- (char *)slp
->req
;
459 /* make sure returned has sane values in case we don't read
460 * them from the slave in this pass */
461 returned
.unclen
= returned
.clen
= writesize
;
463 slp
->req
[trecno
].count
= 0; /* Sentinel */
465 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
466 quit("error writing command pipe: %s\n", strerror(errno
));
467 slp
->sent
= 1; /* we sent a request, read the response later */
469 lastfirstrec
= slp
->firstrec
;
471 if (++slp
>= &slaves
[SLAVES
])
474 /* Read results back from next slave */
476 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
477 != sizeof returned
) {
478 perror(" DUMP: error reading command pipe in master");
481 got
= returned
.unclen
;
482 bytes_written
+= returned
.clen
;
483 if (returned
.unclen
== returned
.clen
)
487 /* Check for errors or end of tape */
489 /* Check for errors */
493 msg("End of tape detected\n");
496 * Drain the results, don't care what the values were.
497 * If we read them here then trewind won't...
499 for (i
= 0; i
< SLAVES
; i
++) {
500 if (slaves
[i
].sent
) {
501 if (dump_atomic_read( slaves
[i
].fd
,
502 (char *)&returned
, sizeof returned
)
503 != sizeof returned
) {
504 perror(" DUMP: error reading command pipe in master");
518 if (spcl
.c_type
== TS_CLRI
|| spcl
.c_type
== TS_BITS
)
521 if (spcl
.c_type
!= TS_END
) {
522 for (i
= 0; i
< spcl
.c_count
; i
++)
523 if (spcl
.c_addr
[i
] != 0)
527 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
528 slp
->tapea
= spcl
.c_tapea
;
529 slp
->firstrec
= lastfirstrec
+ ntrec
;
531 nextblock
= slp
->tblock
;
533 asize
+= tenths
+ returned
.clen
/ density
;
534 blockswritten
+= ntrec
;
535 blocksthisvol
+= ntrec
;
536 if (!pipeout
&& !unlimited
) {
537 if (blocksperfiles
&& blocksperfiles
[blocksperfiles_current
]) {
538 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfiles
[blocksperfiles_current
]) * 1024)
539 : blocksthisvol
>= blocksperfiles
[blocksperfiles_current
] ) {
544 else if (asize
> tsize
) {
557 struct slave_results returned
;
559 for (f
= 0; f
< SLAVES
; f
++) {
561 * Drain the results, but unlike EOT we DO (or should) care
562 * what the return values were, since if we detect EOT after
563 * we think we've written the last blocks to the tape anyway,
564 * we have to replay those blocks with rollforward.
566 * fixme: punt for now.
568 if (slaves
[f
].sent
) {
569 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
570 != sizeof returned
) {
571 perror(" DUMP: error reading command pipe in master");
574 got
= returned
.unclen
;
575 bytes_written
+= returned
.clen
;
576 if (returned
.unclen
== returned
.clen
)
584 msg("EOT detected in last 2 tape records!\n");
585 msg("Use a longer tape, decrease the size estimate\n");
586 quit("or use no size estimate at all.\n");
589 (void) close(slaves
[f
].fd
);
591 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
596 msg("Closing %s\n", tape
);
601 while (rmtopen(tape
, O_RDONLY
) < 0)
608 (void) close(tapefd
);
610 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
626 msg("Launching %s\n", eot_script
);
627 eot_code
= system_command(eot_script
, tape
, tapeno
);
629 if (eot_code
!= 0 && eot_code
!= 1) {
630 msg("Dump aborted by the end of tape script\n");
635 if (nexttape
|| Mflag
)
638 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
639 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
641 while (!query("Is the new volume mounted and ready to go?"))
642 if (query("Do you want to abort?")) {
651 struct req
*p
, *q
= NULL
, *prev
;
653 int i
, size
, savedtapea
, got
;
654 union u_spcl
*ntb
, *otb
;
655 struct slave_results returned
;
660 tslp
= &slaves
[SLAVES
];
661 ntb
= (union u_spcl
*)tslp
->tblock
[1];
663 /* make sure returned has sane values in case we don't read
664 * them from the slave in this pass */
665 returned
.unclen
= returned
.clen
= writesize
;
668 * Each of the N slaves should have requests that need to
669 * be replayed on the next tape. Use the extra slave buffers
670 * (slaves[SLAVES]) to construct request lists to be sent to
671 * each slave in turn.
673 for (i
= 0; i
< SLAVES
; i
++) {
675 otb
= (union u_spcl
*)slp
->tblock
;
678 * For each request in the current slave, copy it to tslp.
682 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
685 *ntb
++ = *otb
++; /* copy the datablock also */
690 quit("rollforward: protocol botch");
702 nextblock
= tslp
->tblock
;
703 savedtapea
= spcl
.c_tapea
;
704 spcl
.c_tapea
= slp
->tapea
;
706 spcl
.c_tapea
= savedtapea
;
707 lastspclrec
= savedtapea
- 1;
709 size
= (char *)ntb
- (char *)q
;
710 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
711 perror(" DUMP: error writing command pipe");
716 lastfirstrec
= slp
->firstrec
;
718 if (++slp
>= &slaves
[SLAVES
])
723 if (prev
->dblk
!= 0) {
725 * If the last one was a disk block, make the
726 * first of this one be the last bit of that disk
729 q
->dblk
= prev
->dblk
+
730 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
731 ntb
= (union u_spcl
*)tslp
->tblock
;
734 * It wasn't a disk block. Copy the data to its
735 * new location in the buffer.
738 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
739 ntb
= (union u_spcl
*)tslp
->tblock
[1];
743 nextblock
= slp
->tblock
;
746 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
747 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
754 * Clear the first slaves' response. One hopes that it
755 * worked ok, otherwise the tape is much too short!
758 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
759 != sizeof returned
) {
760 perror(" DUMP: error reading command pipe in master");
763 got
= returned
.unclen
;
764 bytes_written
+= returned
.clen
;
765 if (returned
.clen
== returned
.unclen
)
773 quit("EOT detected at start of the tape!\n");
779 if (spcl
.c_type
!= TS_END
) {
780 for (i
= 0; i
< spcl
.c_count
; i
++)
781 if (spcl
.c_addr
[i
] != 0)
785 slp
->firstrec
= lastfirstrec
+ ntrec
;
786 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
788 asize
+= tenths
+ returned
.clen
/ density
;
789 blockswritten
+= ntrec
;
790 blocksthisvol
+= ntrec
;
795 #if defined(SYS_clone) && defined(CLONE_IO)
799 return syscall(SYS_clone
, CLONE_ARGS
);
805 * We implement taking and restoring checkpoints on the tape level.
806 * When each tape is opened, a new process is created by forking; this
807 * saves all of the necessary context in the parent. The child
808 * continues the dump; the parent waits around, saving the context.
809 * If the child returns X_REWRITE, then it had problems writing that tape;
810 * this causes the parent to fork again, duplicating the context, and
811 * everything continues as if nothing had happened.
814 startnewtape(int top
)
825 sigaddset(&sigs
, SIGINT
);
826 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
827 #else /* __linux__ */
829 void (*interrupt_save
)();
831 sig_t interrupt_save
;
833 interrupt_save
= signal(SIGINT
, SIG_IGN
);
834 #endif /* __linux__ */
836 parentpid
= getpid();
837 tapea_volume
= spcl
.c_tapea
;
838 tapea_bytes
= bytes_written
;
839 tstart_volume
= time(NULL
);
843 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
845 (void)signal(SIGINT
, interrupt_save
);
848 * All signals are inherited...
850 childpid
= fork_clone_io();
852 msg("Context save fork fails in parent %d\n", parentpid
);
858 * save the context by waiting
859 * until the child doing all of the work returns.
860 * don't catch the interrupt
863 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
865 signal(SIGINT
, SIG_IGN
);
868 msg("Tape: %d; parent process: %d child process %d\n",
869 tapeno
+1, parentpid
, childpid
);
871 while ((waitpid
= wait(&status
)) != childpid
)
872 if (waitpid
!= rshpid
)
873 msg("Parent %d waiting for child %d has another child %d return\n",
874 parentpid
, childpid
, waitpid
);
876 msg("Child %d returns LOB status %o\n",
877 childpid
, status
&0xFF);
879 status
= (status
>> 8) & 0xFF;
883 msg("Child %d finishes X_FINOK\n", childpid
);
886 msg("Child %d finishes X_ABORT\n", childpid
);
889 msg("Child %d finishes X_REWRITE\n", childpid
);
892 msg("Child %d finishes unknown %d\n",
903 goto restore_check_point
;
905 msg("Bad return code from dump: %d\n", status
);
909 } else { /* we are the child; just continue */
911 sleep(4); /* allow time for parent's message to get out */
912 msg("Child on Tape %d has parent %d, my pid = %d\n",
913 tapeno
+1, parentpid
, getpid());
916 * If we have a name like "/dev/rmt0,/dev/rmt1",
917 * use the name before the comma first, and save
918 * the remaining names for subsequent volumes.
920 tapeno
++; /* current tape sequence */
922 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
923 tape
[MAXPATHLEN
- 1] = '\0';
924 msg("Dumping volume %d on %s\n", tapeno
, tape
);
926 else if (nexttape
|| strchr(tapeprefix
, ',')) {
927 if (nexttape
&& *nexttape
)
928 tapeprefix
= nexttape
;
929 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
934 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
935 tape
[MAXPATHLEN
- 1] = '\0';
936 msg("Dumping volume %d on %s\n", tapeno
, tape
);
938 if (blocksperfiles
&& blocksperfiles_current
< *blocksperfiles
)
939 blocksperfiles_current
++;
941 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
943 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
945 while ((tapefd
= (pipeout
? fileno(stdout
) :
946 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
949 msg("Cannot open output \"%s\": %s\n", tape
,
951 if (!query("Do you want to retry the open?"))
958 struct mtget mt_stat
;
959 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
961 msg("Output is to %s\n",
962 magtapeout ? "tape" : "file/pipe");
966 enslave(); /* Share open tape file descriptor with slaves */
971 newtape
++; /* new tape signal */
972 spcl
.c_count
= slp
->count
;
974 * measure firstrec in TP_BSIZE units since restore doesn't
975 * know the correct ntrec value...
977 spcl
.c_firstrec
= slp
->firstrec
;
979 spcl
.c_type
= TS_TAPE
;
980 spcl
.c_flags
|= DR_NEWHEADER
;
981 spcl
.c_ntrec
= ntrec
;
983 spcl
.c_flags
|= DR_COMPRESSED
;
984 writeheader((dump_ino_t
)slp
->inode
);
985 spcl
.c_flags
&=~ DR_NEWHEADER
;
986 msg("Volume %d started with block %ld at: %s", tapeno
,
987 spcl
.c_tapea
, ctime(&tstart_volume
));
989 msg("Volume %d begins with blocks from inode %d\n",
991 if (tapeno
< (int)TP_NINOS
)
992 volinfo
[tapeno
] = slp
->inode
;
993 transformation
->startNewTape(transformation
, NULL
, 0);
998 dumpabort(UNUSED(int signo
))
1001 if (master
!= 0 && master
!= getpid())
1002 /* Signals master to call dumpabort */
1003 (void) kill(master
, SIGTERM
);
1006 msg("The ENTIRE dump is aborted.\n");
1019 msg("pid = %d exits with status %d\n", getpid(), status
);
1025 * proceed - handler for SIGUSR1, used to synchronize IO between the slaves.
1028 proceed1(UNUSED(int signo
))
1031 siglongjmp(jmpbuf1
, 1);
1036 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
1039 proceed2(UNUSED(int signo
))
1042 siglongjmp(jmpbuf2
, 1);
1050 #ifdef LINUX_FORK_BUG
1058 { struct sigaction sa
;
1059 memset(&sa
, 0, sizeof sa
);
1060 sigemptyset(&sa
.sa_mask
);
1061 sa
.sa_handler
= dumpabort
;
1062 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
1063 sa
.sa_handler
= sigpipe
;
1064 sigaction(SIGPIPE
, &sa
, NULL
);
1065 sa
.sa_handler
= proceed1
;
1066 sa
.sa_flags
= SA_RESTART
;
1067 sigaction(SIGUSR1
, &sa
, NULL
); /* Slave sends SIGUSR1 to next slave */
1068 sa
.sa_handler
= proceed2
;
1069 sa
.sa_flags
= SA_RESTART
;
1070 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
1073 for (i
= 0; i
< SLAVES
; i
++) {
1074 if (i
== slp
- &slaves
[0]) {
1082 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
1083 (slaves
[i
].pid
= fork_clone_io()) < 0)
1084 quit("too many slaves, %d (recompile smaller): %s\n",
1085 i
, strerror(errno
));
1087 slaves
[i
].fd
= cmd
[1];
1089 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1091 for (j
= 0; j
<= i
; j
++)
1092 (void) close(slaves
[j
].fd
);
1094 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1095 #if defined(SIGINFO)
1096 sigaddset(&sigs
, SIGINFO
);
1098 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1100 #ifdef LINUX_FORK_BUG
1101 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1103 quit("master/slave protocol botched 3\n");
1109 (slaves
[i
].pid
== slp
->pid
));
1116 #ifdef LINUX_FORK_BUG
1118 * Wait for all slaves to _actually_ start to circumvent a bug in
1119 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1120 * returned from fork() causes a SEGV in the child process
1122 for (i
= 0; i
< SLAVES
; i
++)
1123 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1124 quit("master/slave protocol botched 4\n");
1127 for (i
= 0; i
< SLAVES
; i
++)
1128 (void) dump_atomic_write( slaves
[i
].fd
,
1129 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1130 sizeof slaves
[0].pid
);
1140 for (i
= 0; i
< SLAVES
; i
++)
1141 if (slaves
[i
].pid
> 0) {
1142 (void) kill(slaves
[i
].pid
, SIGKILL
);
1148 * Synchronization - each process waits for a SIGUSR2 from the
1149 * previous process before writing to the tape, and sends SIGUSR2
1150 * to the next process when the tape write completes. On tape errors
1151 * a SIGUSR1 is sent to the master which then terminates all of the
1152 * slaves. Each process sends SIGUSR1 to the next to signal that it
1153 * is time to start reading from the disk, after it finishes reading
1154 * and moves to the compression phase.
1165 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1166 char * volatile buffer
;
1167 #if defined(HAVE_BLOCK_TRANSFORMATION)
1168 struct tapebuf
* volatile comp_buf
= NULL
;
1170 volatile int do_compress
= !first
;
1171 unsigned long worklen
;
1172 #endif /* HAVE_BLOCK_TRANSFORMATION */
1174 struct slave_results returns
;
1179 long long curtapepos
;
1180 union u_spcl
*uspclptr
;
1181 struct s_spcl
*spclptr
;
1182 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1183 long maxntrecs
= 50000; /* every 50MB */
1184 long cntntrecs
= maxntrecs
;
1185 #endif /* USE_QFA */
1189 sigaddset(&set
, SIGUSR1
);
1190 sigaddset(&set
, SIGUSR2
);
1191 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1194 #ifdef HAVE_BLOCK_TRANSFORMATION
1195 transformation
->startDiskIOProcess(transformation
);
1196 #endif /* HAVE_BLOCK_TRANSFORMATION */
1199 * Need our own seek pointer.
1201 (void) close(diskfd
);
1202 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1203 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1206 (void)ioctl(diskfd
, BLKFLSBUF
, 0);
1209 retval
= dump_fs_open(disk
, &fs
);
1211 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1212 #endif /* __linux__ */
1215 * Need the pid of the next slave in the loop...
1217 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1218 != sizeof nextslave
) {
1219 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1222 #if defined(HAVE_BLOCK_TRANSFORMATION)
1223 /* if we're doing a compressed dump, allocate the compress buffer */
1225 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1226 /* Add extra space to deal with compression or encryption enlarging the buffer */
1227 if (TP_BSIZE
> writesize
/16 + 200)
1230 bsiz
+= writesize
/16 + 200;
1231 comp_buf
= malloc(bsiz
);
1232 if (comp_buf
== NULL
)
1233 quit("couldn't allocate a compress buffer.\n");
1234 transformation
->initialize(transformation
, 1);
1235 if (zipflag
== COMPRESS_ZLIB
)
1236 comp_buf
->flags
= COMPRESS_ZLIB
;
1237 else if (zipflag
== COMPRESS_BZLIB
)
1238 comp_buf
->flags
= COMPRESS_BZLIB
;
1239 else if (zipflag
== COMPRESS_LZO
)
1240 comp_buf
->flags
= COMPRESS_LZO
;
1242 quit("internal error - unknown compression method: %d\n", zipflag
);
1244 #endif /* HAVE_BLOCK_TRANSFORMATION */
1247 * Get list of blocks to dump, read the blocks into tape buffer
1249 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1250 struct req
*p
= slp
->req
;
1252 /* wait for previous slave to finish reading */
1253 if (sigsetjmp(jmpbuf1
, 1) == 0) {
1261 for (trecno
= 0; trecno
< ntrec
;
1262 trecno
+= p
->count
, p
+= p
->count
) {
1264 if (p
->dblk
) { /* read a disk block */
1265 bread(p
->dblk
, slp
->tblock
[trecno
],
1266 p
->count
* TP_BSIZE
);
1267 } else { /* read record from pipe */
1268 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1269 (char *)slp
->tblock
[trecno
],
1270 TP_BSIZE
) != TP_BSIZE
)
1271 quit("master/slave protocol botched.\n");
1274 /* signal next slave to start reading */
1275 (void) kill(nextslave
, SIGUSR1
);
1277 /* Try to write the data... */
1281 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1282 bufsize
= writesize
; /* length to write */
1283 returns
.clen
= returns
.unclen
= bufsize
;
1285 #if defined(HAVE_BLOCK_TRANSFORMATION)
1287 * When writing a compressed dump, each block except
1288 * the first one on each tape is written
1289 * from struct tapebuf with an 4 byte prefix
1290 * followed by the data. This can be less than
1291 * writesize. Restore, on a short read, can compare the
1292 * length read to the compressed length in the header
1293 * to verify that the read was good. Blocks which don't
1294 * compress well are written uncompressed.
1295 * The first block written by each slave is not compressed
1296 * and does not have a prefix.
1298 if (compressed
&& do_compress
) {
1299 comp_buf
->length
= bufsize
;
1300 worklen
= TP_BSIZE
+ writesize
;
1303 // tapebuf: compressed, flags, length
1304 compresult
= transformation
->compress(transformation
, comp_buf
,
1305 &worklen
, slp
->tblock
[0], writesize
);
1307 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1308 /* write the compressed buffer */
1309 comp_buf
->length
= worklen
;
1310 comp_buf
->compressed
= 1;
1311 buffer
= (char *) comp_buf
;
1312 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1315 /* write the data uncompressed */
1316 comp_buf
->length
= writesize
;
1317 comp_buf
->compressed
= 0;
1318 buffer
= (char *) comp_buf
;
1319 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1320 returns
.unclen
= returns
.clen
;
1321 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1324 /* compress the remaining blocks if we're compressing */
1325 do_compress
= compressed
;
1326 #endif /* HAVE_BLOCK_TRANSFORMATION */
1328 if (sigsetjmp(jmpbuf2
, 1) == 0) {
1337 if (gTapeposfd
>= 0) {
1341 for (i
= 0; (i
< ntrec
) && !foundone
; ++i
) {
1342 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1343 spclptr
= &uspclptr
->s_spcl
;
1344 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1345 (spclptr
->c_type
== TS_INODE
) &&
1346 (spclptr
->c_date
== gThisDumpDate
) &&
1347 !(spclptr
->c_dinode
.di_mode
& S_IFDIR
) &&
1348 !(spclptr
->c_flags
& DR_EXTATTRIBUTES
)
1351 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1354 gtperr
= GetTapePos(&curtapepos
);
1355 /* if an error occured previously don't
1359 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1361 gtperr
= MkTapeString(spclptr
, curtapepos
);
1367 #endif /* USE_QFA */
1369 while (eot_count
< 10 && size
< bufsize
) {
1372 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1375 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1377 printf("slave %d wrote %d\n", slave_number
, wrote
);
1379 if (wrote
< 0 && errno
!= ENOSPC
)
1381 if (wrote
== 0 || (wrote
< 0 && errno
== ENOSPC
))
1388 if (size
!= bufsize
)
1389 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1390 slave_number
, size
, bufsize
);
1394 * Handle ENOSPC as an EOT condition.
1396 if (wrote
< 0 && errno
== ENOSPC
) {
1402 returns
.clen
= returns
.unclen
= 0;
1405 * pass errno back to master for special handling
1408 returns
.unclen
= -errno
;
1411 * pass size of data and size of write back to master
1412 * (for EOT handling)
1414 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1417 * Signal the next slave to go.
1419 (void) kill(nextslave
, SIGUSR2
);
1421 if (gTapeposfd
>= 0) {
1424 #endif /* USE_QFA */
1427 #ifdef HAVE_BLOCK_TRANSFORMATION
1428 transformation
->endDiskIOProcess(transformation
);
1429 #endif /* HAVE_BLOCK_TRANSFORMATION */
1432 quit("error reading command pipe: %s\n", strerror(errno
));
1436 * Since a read from a pipe may not return all we asked for,
1437 * or a write may not write all we ask if we get a signal,
1438 * loop until the count is satisfied (or error).
1441 dump_atomic_read(int fd
, char *buf
, size_t count
)
1443 int got
, need
= count
;
1446 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1448 } while (got
== -1 && errno
== EINTR
);
1449 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1453 * Since a read from a pipe may not return all we asked for,
1454 * or a write may not write all we ask if we get a signal,
1455 * loop until the count is satisfied (or error).
1458 dump_atomic_write(int fd
, const char *buf
, size_t count
)
1460 int got
, need
= count
;
1463 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1465 } while (got
== -1 && errno
== EINTR
);
1466 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1477 buf.mt_op = MTSETDRVBUFFER;
1478 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1479 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1481 msg("[%ld] error: %d (setting logical)\n",
1482 (unsigned long)getpid(), err);
1489 #define LSEEK_GET_TAPEPOS 10
1490 #define LSEEK_GO2_TAPEPOS 11
1492 * read the current tape position
1495 GetTapePos(long long *pos
)
1501 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
1510 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1511 *pos
= (long long)mtpos
;
1514 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1520 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1528 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
)
1533 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1536 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1537 (unsigned long)spclptr
->c_inumber
,
1540 gTps
[sizeof(gTps
) - 1] = '\0';
1541 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1543 warn("error writing tapepos file. (error %d)\n", errno
);
1547 #endif /* USE_QFA */