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.91 2009/06/18 09:50:54 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 #endif /* HAVE_ZLIB */
101 #endif /* HAVE_BZLIB */
105 #endif /* HAVE_LZO */
109 int writesize
; /* size of malloc()ed buffer for tape */
110 long lastspclrec
= -1; /* tape block number of last written header */
111 int trecno
= 0; /* next record to write in current block */
112 extern long *blocksperfiles
; /* number of blocks per output file(s) */
113 long blocksperfiles_current
; /* current position in blocksperfiles */
114 long blocksthisvol
; /* number of blocks on current output file */
115 extern int ntrec
; /* blocking factor on tape */
116 extern int cartridge
;
120 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
121 static int magtapeout
; /* output is really a tape */
123 static ssize_t dump_atomic_read
__P((int, char *, size_t));
124 static ssize_t dump_atomic_write
__P((int, const char *, size_t));
126 static void doslave
__P((int, int, int));
128 static void doslave
__P((int, int));
130 static void enslave
__P((void));
131 static void flushtape
__P((void));
132 static void killall
__P((void));
133 static void rollforward
__P((void));
135 static int GetTapePos
__P((long long *));
136 static int MkTapeString
__P((struct s_spcl
*, long long));
137 #define FILESQFAPOS 20
141 * Concurrent dump mods (Caltech) - disk block reading and tape writing
142 * are exported to several slave processes. While one slave writes the
143 * tape, the others read disk blocks; they pass control of the tape in
144 * a ring via signals. The parent process traverses the filesystem and
145 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
146 * The following structure defines the instruction packets sent to slaves.
154 struct slave_results
{
155 ssize_t unclen
; /* uncompressed length */
156 ssize_t clen
; /* compressed length */
159 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
161 int tapea
; /* header number at start of this chunk */
162 int count
; /* count to next header (used for TS_TAPE */
164 int inode
; /* inode that we are currently dealing with */
165 int fd
; /* FD for this slave */
166 int pid
; /* PID for this slave */
167 int sent
; /* 1 == we've sent this slave requests */
168 int firstrec
; /* record number of this block */
169 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
170 struct req
*req
; /* buffer for requests */
174 char (*nextblock
)[TP_BSIZE
];
176 static time_t tstart_volume
; /* time of volume start */
177 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
179 int master
; /* pid of master, for sending error signals */
180 int tenths
; /* length of tape overhead per block written */
181 static int caught
; /* have we caught the signal to proceed? */
182 static int ready
; /* have we reached the lock point without having */
183 /* received the SIGUSR2 signal from the prev slave? */
184 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
185 /* SIGUSR2 arrives from the previous slave */
187 static int gtperr
= 0;
191 * Determine if we can use Linux' clone system call. If so, call it
192 * with the CLONE_IO flag so that all processes will share the same I/O
193 * context, allowing the I/O schedulers to make better scheduling decisions.
196 /* first, pull in the header files that define sys_clone and CLONE_IO */
203 /* If either is not present, fall back on the fork behaviour */
204 #if ! defined(SYS_clone) || ! defined (CLONE_IO)
205 #define fork_clone_io fork
206 #else /* SYS_clone */
207 /* CLONE_IO is available, determine which version of sys_clone to use */
208 #include <linux/version.h>
210 * Kernel 2.5.49 introduced two extra parameters to the clone system call.
211 * Neither is useful in our case, so this is easy to handle.
213 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,49)
214 /* clone_flags, child_stack, parent_tidptr, child_tidptr */
215 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0, NULL, NULL
217 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0
218 #endif /* LINUX_VERSION_CODE */
219 pid_t
fork_clone_io(void);
220 #endif /* SYS_clone */
221 #else /* __linux__ not defined */
222 #define fork_clone_io fork
223 #endif /* __linux__ */
228 int pgoff
= getpagesize() - 1;
232 writesize
= ntrec
* TP_BSIZE
;
233 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
235 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
236 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
237 * repositioning after stopping, i.e, streaming mode, where the gap is
238 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
240 if (!blocksperfiles
&& !unlimited
)
241 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
247 * Allocate tape buffer contiguous with the array of instruction
248 * packets, so flushtape() can write them together with one write().
249 * Align tape buffer on page boundary to speed up tape write().
251 for (i
= 0; i
<= SLAVES
; i
++) {
253 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
256 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
258 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
260 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
262 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
268 nextblock
= slp
->tblock
;
273 writerec(const void *dp
, int isspcl
)
276 slp
->req
[trecno
].dblk
= (ext2_loff_t
)0;
277 slp
->req
[trecno
].count
= 1;
278 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
279 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
281 /* Need to write it to the archive file */
282 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
284 if (AfileActive
&& Afile
>= 0 && !(spcl
.c_flags
& DR_EXTATTRIBUTES
)) {
285 /* When we dump an inode which is not a directory,
286 * it means we ended the archive contents */
287 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
288 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
292 tmp
= *(union u_spcl
*)dp
;
293 /* Write the record, _uncompressed_ */
295 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
298 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
299 msg("error writing archive file: %s\n",
306 lastspclrec
= spcl
.c_tapea
;
314 dumpblock(blk_t blkno
, int size
)
319 dblkno
= fsbtodb(sblock
, blkno
);
320 tpblks
= size
>> tp_bshift
;
321 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
322 slp
->req
[trecno
].dblk
= dblkno
;
323 slp
->req
[trecno
].count
= avail
;
325 spcl
.c_tapea
+= avail
;
328 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
340 msg("write error on %s: %s\n", tape
, strerror(errnum
));
341 quit("Cannot recover\n");
344 msg("write error %d blocks into volume %d: %s\n",
345 blocksthisvol
, tapeno
, strerror(errnum
));
346 broadcast("DUMP WRITE ERROR!\n");
347 if (query("Do you want to rewrite this volume?")) {
348 msg("Closing this volume. Prepare to restart with new media;\n");
349 msg("this dump volume will be rewritten.\n");
355 if (query("Do you want to start the next tape?"))
361 sigpipe(UNUSED(int signo
))
364 quit("Broken pipe\n");
369 * Update xferrate stats
378 ttaken
= tnow
- tstart_volume
;
379 blocks
= spcl
.c_tapea
- tapea_volume
;
380 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
382 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
383 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
385 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
386 long txfrate
= volkb
/ ttaken
;
387 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
388 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
389 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
393 double rate
= .0005 + (double) blocks
/ (double) volkb
;
394 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
396 tapeno
, blocks
, volkb
, rate
);
403 mktimeest(time_t tnow
)
405 static char msgbuf
[128];
410 if (blockswritten
< 500)
412 if (blockswritten
> tapesize
)
413 tapesize
= blockswritten
;
414 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
415 / blockswritten
* tapesize
;
416 if (tnow
> tstart_volume
)
417 (void)snprintf(msgbuf
, sizeof(msgbuf
),
418 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
419 (blockswritten
* 100.0) / tapesize
,
420 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
421 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
423 (void)snprintf(msgbuf
, sizeof(msgbuf
),
424 "%3.2f%% done, finished in %d:%02d\n",
425 (blockswritten
* 100.0) / tapesize
,
426 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
434 * information message upon receipt of SIGINFO
437 statussig(int notused
)
439 int save_errno
= errno
;
442 buf
= mktimeest(time(NULL
));
444 write(STDERR_FILENO
, buf
, strlen(buf
));
454 struct slave_results returned
;
456 int siz
= (char *)nextblock
- (char *)slp
->req
;
458 /* make sure returned has sane values in case we don't read
459 * them from the slave in this pass */
460 returned
.unclen
= returned
.clen
= writesize
;
462 slp
->req
[trecno
].count
= 0; /* Sentinel */
464 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
465 quit("error writing command pipe: %s\n", strerror(errno
));
466 slp
->sent
= 1; /* we sent a request, read the response later */
468 lastfirstrec
= slp
->firstrec
;
470 if (++slp
>= &slaves
[SLAVES
])
473 /* Read results back from next slave */
475 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
476 != sizeof returned
) {
477 perror(" DUMP: error reading command pipe in master");
480 got
= returned
.unclen
;
481 bytes_written
+= returned
.clen
;
482 if (returned
.unclen
== returned
.clen
)
486 /* Check for errors or end of tape */
488 /* Check for errors */
492 msg("End of tape detected\n");
495 * Drain the results, don't care what the values were.
496 * If we read them here then trewind won't...
498 for (i
= 0; i
< SLAVES
; i
++) {
499 if (slaves
[i
].sent
) {
500 if (dump_atomic_read( slaves
[i
].fd
,
501 (char *)&returned
, sizeof returned
)
502 != sizeof returned
) {
503 perror(" DUMP: error reading command pipe in master");
517 if (spcl
.c_type
== TS_CLRI
|| spcl
.c_type
== TS_BITS
)
520 if (spcl
.c_type
!= TS_END
) {
521 for (i
= 0; i
< spcl
.c_count
; i
++)
522 if (spcl
.c_addr
[i
] != 0)
526 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
527 slp
->tapea
= spcl
.c_tapea
;
528 slp
->firstrec
= lastfirstrec
+ ntrec
;
530 nextblock
= slp
->tblock
;
532 asize
+= tenths
+ returned
.clen
/ density
;
533 blockswritten
+= ntrec
;
534 blocksthisvol
+= ntrec
;
535 if (!pipeout
&& !unlimited
) {
536 if (blocksperfiles
&& blocksperfiles
[blocksperfiles_current
]) {
537 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfiles
[blocksperfiles_current
]) * 1024)
538 : blocksthisvol
>= blocksperfiles
[blocksperfiles_current
] ) {
543 else if (asize
> tsize
) {
556 struct slave_results returned
;
558 for (f
= 0; f
< SLAVES
; f
++) {
560 * Drain the results, but unlike EOT we DO (or should) care
561 * what the return values were, since if we detect EOT after
562 * we think we've written the last blocks to the tape anyway,
563 * we have to replay those blocks with rollforward.
565 * fixme: punt for now.
567 if (slaves
[f
].sent
) {
568 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
569 != sizeof returned
) {
570 perror(" DUMP: error reading command pipe in master");
573 got
= returned
.unclen
;
574 bytes_written
+= returned
.clen
;
575 if (returned
.unclen
== returned
.clen
)
583 msg("EOT detected in last 2 tape records!\n");
584 msg("Use a longer tape, decrease the size estimate\n");
585 quit("or use no size estimate at all.\n");
588 (void) close(slaves
[f
].fd
);
590 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
595 msg("Closing %s\n", tape
);
600 while (rmtopen(tape
, O_RDONLY
) < 0)
607 (void) close(tapefd
);
609 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
625 msg("Launching %s\n", eot_script
);
626 eot_code
= system_command(eot_script
, tape
, tapeno
);
628 if (eot_code
!= 0 && eot_code
!= 1) {
629 msg("Dump aborted by the end of tape script\n");
634 if (nexttape
|| Mflag
)
637 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
638 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
640 while (!query("Is the new volume mounted and ready to go?"))
641 if (query("Do you want to abort?")) {
650 struct req
*p
, *q
= NULL
, *prev
;
652 int i
, size
, savedtapea
, got
;
653 union u_spcl
*ntb
, *otb
;
654 struct slave_results returned
;
659 tslp
= &slaves
[SLAVES
];
660 ntb
= (union u_spcl
*)tslp
->tblock
[1];
662 /* make sure returned has sane values in case we don't read
663 * them from the slave in this pass */
664 returned
.unclen
= returned
.clen
= writesize
;
667 * Each of the N slaves should have requests that need to
668 * be replayed on the next tape. Use the extra slave buffers
669 * (slaves[SLAVES]) to construct request lists to be sent to
670 * each slave in turn.
672 for (i
= 0; i
< SLAVES
; i
++) {
674 otb
= (union u_spcl
*)slp
->tblock
;
677 * For each request in the current slave, copy it to tslp.
681 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
684 *ntb
++ = *otb
++; /* copy the datablock also */
689 quit("rollforward: protocol botch");
701 nextblock
= tslp
->tblock
;
702 savedtapea
= spcl
.c_tapea
;
703 spcl
.c_tapea
= slp
->tapea
;
705 spcl
.c_tapea
= savedtapea
;
706 lastspclrec
= savedtapea
- 1;
708 size
= (char *)ntb
- (char *)q
;
709 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
710 perror(" DUMP: error writing command pipe");
715 lastfirstrec
= slp
->firstrec
;
717 if (++slp
>= &slaves
[SLAVES
])
722 if (prev
->dblk
!= 0) {
724 * If the last one was a disk block, make the
725 * first of this one be the last bit of that disk
728 q
->dblk
= prev
->dblk
+
729 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
730 ntb
= (union u_spcl
*)tslp
->tblock
;
733 * It wasn't a disk block. Copy the data to its
734 * new location in the buffer.
737 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
738 ntb
= (union u_spcl
*)tslp
->tblock
[1];
742 nextblock
= slp
->tblock
;
745 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
746 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
753 * Clear the first slaves' response. One hopes that it
754 * worked ok, otherwise the tape is much too short!
757 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
758 != sizeof returned
) {
759 perror(" DUMP: error reading command pipe in master");
762 got
= returned
.unclen
;
763 bytes_written
+= returned
.clen
;
764 if (returned
.clen
== returned
.unclen
)
772 quit("EOT detected at start of the tape!\n");
778 if (spcl
.c_type
!= TS_END
) {
779 for (i
= 0; i
< spcl
.c_count
; i
++)
780 if (spcl
.c_addr
[i
] != 0)
784 slp
->firstrec
= lastfirstrec
+ ntrec
;
785 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
787 asize
+= tenths
+ returned
.clen
/ density
;
788 blockswritten
+= ntrec
;
789 blocksthisvol
+= ntrec
;
794 #if defined(SYS_clone) && defined(CLONE_IO)
798 return syscall(SYS_clone
, CLONE_ARGS
);
804 * We implement taking and restoring checkpoints on the tape level.
805 * When each tape is opened, a new process is created by forking; this
806 * saves all of the necessary context in the parent. The child
807 * continues the dump; the parent waits around, saving the context.
808 * If the child returns X_REWRITE, then it had problems writing that tape;
809 * this causes the parent to fork again, duplicating the context, and
810 * everything continues as if nothing had happened.
813 startnewtape(int top
)
824 sigaddset(&sigs
, SIGINT
);
825 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
826 #else /* __linux__ */
828 void (*interrupt_save
)();
830 sig_t interrupt_save
;
832 interrupt_save
= signal(SIGINT
, SIG_IGN
);
833 #endif /* __linux__ */
835 parentpid
= getpid();
836 tapea_volume
= spcl
.c_tapea
;
837 tapea_bytes
= bytes_written
;
838 tstart_volume
= time(NULL
);
842 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
844 (void)signal(SIGINT
, interrupt_save
);
847 * All signals are inherited...
849 childpid
= fork_clone_io();
851 msg("Context save fork fails in parent %d\n", parentpid
);
857 * save the context by waiting
858 * until the child doing all of the work returns.
859 * don't catch the interrupt
862 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
864 signal(SIGINT
, SIG_IGN
);
867 msg("Tape: %d; parent process: %d child process %d\n",
868 tapeno
+1, parentpid
, childpid
);
870 while ((waitpid
= wait(&status
)) != childpid
)
871 if (waitpid
!= rshpid
)
872 msg("Parent %d waiting for child %d has another child %d return\n",
873 parentpid
, childpid
, waitpid
);
875 msg("Child %d returns LOB status %o\n",
876 childpid
, status
&0xFF);
878 status
= (status
>> 8) & 0xFF;
882 msg("Child %d finishes X_FINOK\n", childpid
);
885 msg("Child %d finishes X_ABORT\n", childpid
);
888 msg("Child %d finishes X_REWRITE\n", childpid
);
891 msg("Child %d finishes unknown %d\n",
902 goto restore_check_point
;
904 msg("Bad return code from dump: %d\n", status
);
908 } else { /* we are the child; just continue */
910 sleep(4); /* allow time for parent's message to get out */
911 msg("Child on Tape %d has parent %d, my pid = %d\n",
912 tapeno
+1, parentpid
, getpid());
915 * If we have a name like "/dev/rmt0,/dev/rmt1",
916 * use the name before the comma first, and save
917 * the remaining names for subsequent volumes.
919 tapeno
++; /* current tape sequence */
921 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
922 tape
[MAXPATHLEN
- 1] = '\0';
923 msg("Dumping volume %d on %s\n", tapeno
, tape
);
925 else if (nexttape
|| strchr(tapeprefix
, ',')) {
926 if (nexttape
&& *nexttape
)
927 tapeprefix
= nexttape
;
928 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
933 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
934 tape
[MAXPATHLEN
- 1] = '\0';
935 msg("Dumping volume %d on %s\n", tapeno
, tape
);
937 if (blocksperfiles
&& blocksperfiles_current
< *blocksperfiles
)
938 blocksperfiles_current
++;
940 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
942 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
944 while ((tapefd
= (pipeout
? fileno(stdout
) :
945 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
948 msg("Cannot open output \"%s\": %s\n", tape
,
950 if (!query("Do you want to retry the open?"))
957 struct mtget mt_stat
;
958 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
960 msg("Output is to %s\n",
961 magtapeout ? "tape" : "file/pipe");
965 enslave(); /* Share open tape file descriptor with slaves */
970 newtape
++; /* new tape signal */
971 spcl
.c_count
= slp
->count
;
973 * measure firstrec in TP_BSIZE units since restore doesn't
974 * know the correct ntrec value...
976 spcl
.c_firstrec
= slp
->firstrec
;
978 spcl
.c_type
= TS_TAPE
;
979 spcl
.c_flags
|= DR_NEWHEADER
;
980 spcl
.c_ntrec
= ntrec
;
982 spcl
.c_flags
|= DR_COMPRESSED
;
983 writeheader((dump_ino_t
)slp
->inode
);
984 spcl
.c_flags
&=~ DR_NEWHEADER
;
985 msg("Volume %d started with block %ld at: %s", tapeno
,
986 spcl
.c_tapea
, ctime(&tstart_volume
));
988 msg("Volume %d begins with blocks from inode %d\n",
990 if (tapeno
< (int)TP_NINOS
)
991 volinfo
[tapeno
] = slp
->inode
;
996 dumpabort(UNUSED(int signo
))
999 if (master
!= 0 && master
!= getpid())
1000 /* Signals master to call dumpabort */
1001 (void) kill(master
, SIGTERM
);
1004 msg("The ENTIRE dump is aborted.\n");
1017 msg("pid = %d exits with status %d\n", getpid(), status
);
1023 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
1026 proceed(UNUSED(int signo
))
1029 siglongjmp(jmpbuf
, 1);
1037 #ifdef LINUX_FORK_BUG
1045 { struct sigaction sa
;
1046 memset(&sa
, 0, sizeof sa
);
1047 sigemptyset(&sa
.sa_mask
);
1048 sa
.sa_handler
= dumpabort
;
1049 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
1050 sa
.sa_handler
= sigpipe
;
1051 sigaction(SIGPIPE
, &sa
, NULL
);
1052 sa
.sa_handler
= proceed
;
1053 sa
.sa_flags
= SA_RESTART
;
1054 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
1057 for (i
= 0; i
< SLAVES
; i
++) {
1058 if (i
== slp
- &slaves
[0]) {
1064 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
1065 (slaves
[i
].pid
= fork_clone_io()) < 0)
1066 quit("too many slaves, %d (recompile smaller): %s\n",
1067 i
, strerror(errno
));
1069 slaves
[i
].fd
= cmd
[1];
1071 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1073 for (j
= 0; j
<= i
; j
++)
1074 (void) close(slaves
[j
].fd
);
1076 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1077 #if defined(SIGINFO)
1078 sigaddset(&sigs
, SIGINFO
);
1080 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1082 #ifdef LINUX_FORK_BUG
1083 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1085 quit("master/slave protocol botched 3\n");
1091 (slaves
[i
].pid
== slp
->pid
));
1098 #ifdef LINUX_FORK_BUG
1100 * Wait for all slaves to _actually_ start to circumvent a bug in
1101 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1102 * returned from fork() causes a SEGV in the child process
1104 for (i
= 0; i
< SLAVES
; i
++)
1105 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1106 quit("master/slave protocol botched 4\n");
1109 for (i
= 0; i
< SLAVES
; i
++)
1110 (void) dump_atomic_write( slaves
[i
].fd
,
1111 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1112 sizeof slaves
[0].pid
);
1122 for (i
= 0; i
< SLAVES
; i
++)
1123 if (slaves
[i
].pid
> 0) {
1124 (void) kill(slaves
[i
].pid
, SIGKILL
);
1130 * Synchronization - each process waits for a SIGUSR2 from the
1131 * previous process before writing to the tape, and sends SIGUSR2
1132 * to the next process when the tape write completes. On tape errors
1133 * a SIGUSR1 is sent to the master which then terminates all of the
1145 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1146 char * volatile buffer
;
1147 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1148 struct tapebuf
* volatile comp_buf
= NULL
;
1150 volatile int do_compress
= !first
;
1151 unsigned long worklen
;
1153 lzo_align_t __LZO_MMODEL
*LZO_WorkMem
;
1155 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1156 struct slave_results returns
;
1161 long long curtapepos
;
1162 union u_spcl
*uspclptr
;
1163 struct s_spcl
*spclptr
;
1164 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1165 long maxntrecs
= 50000; /* every 50MB */
1166 long cntntrecs
= maxntrecs
;
1167 #endif /* USE_QFA */
1171 sigaddset(&set
, SIGUSR2
);
1172 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1176 * Need our own seek pointer.
1178 (void) close(diskfd
);
1179 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1180 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1183 (void)ioctl(diskfd
, BLKFLSBUF
, 0);
1186 retval
= dump_fs_open(disk
, &fs
);
1188 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1189 #endif /* __linux__ */
1192 * Need the pid of the next slave in the loop...
1194 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1195 != sizeof nextslave
) {
1196 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1199 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1200 /* if we're doing a compressed dump, allocate the compress buffer */
1202 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1203 /* Add extra space to deal with compression enlarging the buffer */
1204 if (TP_BSIZE
> writesize
/16 + 67)
1207 bsiz
+= writesize
/16 + 67;
1208 comp_buf
= malloc(bsiz
);
1209 if (comp_buf
== NULL
)
1210 quit("couldn't allocate a compress buffer.\n");
1211 if (zipflag
== COMPRESS_ZLIB
)
1212 comp_buf
->flags
= COMPRESS_ZLIB
;
1213 else if (zipflag
== COMPRESS_BZLIB
)
1214 comp_buf
->flags
= COMPRESS_BZLIB
;
1215 else if (zipflag
== COMPRESS_LZO
) {
1216 comp_buf
->flags
= COMPRESS_LZO
;
1217 if (lzo_init() != LZO_E_OK
) quit("lzo_init failed\n");
1219 quit("internal error - unknown compression method: %d\n", zipflag
);
1222 LZO_WorkMem
= malloc(LZO1X_1_MEM_COMPRESS
);
1224 quit("couldn't allocate a compress buffer.\n");
1226 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1229 * Get list of blocks to dump, read the blocks into tape buffer
1231 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1232 struct req
*p
= slp
->req
;
1234 for (trecno
= 0; trecno
< ntrec
;
1235 trecno
+= p
->count
, p
+= p
->count
) {
1236 if (p
->dblk
) { /* read a disk block */
1237 bread(p
->dblk
, slp
->tblock
[trecno
],
1238 p
->count
* TP_BSIZE
);
1239 } else { /* read record from pipe */
1240 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1241 (char *)slp
->tblock
[trecno
],
1242 TP_BSIZE
) != TP_BSIZE
)
1243 quit("master/slave protocol botched.\n");
1247 /* Try to write the data... */
1251 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1252 bufsize
= writesize
; /* length to write */
1253 returns
.clen
= returns
.unclen
= bufsize
;
1255 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1257 * When writing a compressed dump, each block except
1258 * the first one on each tape is written
1259 * from struct tapebuf with an 4 byte prefix
1260 * followed by the data. This can be less than
1261 * writesize. Restore, on a short read, can compare the
1262 * length read to the compressed length in the header
1263 * to verify that the read was good. Blocks which don't
1264 * compress well are written uncompressed.
1265 * The first block written by each slave is not compressed
1266 * and does not have a prefix.
1269 if (compressed
&& do_compress
) {
1270 comp_buf
->length
= bufsize
;
1271 worklen
= TP_BSIZE
+ writesize
;
1274 if (zipflag
== COMPRESS_ZLIB
) {
1275 compresult
= compress2(comp_buf
->buf
,
1277 (char *)slp
->tblock
[0],
1280 if (compresult
== Z_OK
)
1285 #endif /* HAVE_ZLIB */
1287 if (zipflag
== COMPRESS_BZLIB
) {
1288 unsigned int worklen2
= worklen
;
1289 compresult
= BZ2_bzBuffToBuffCompress(
1292 (char *)slp
->tblock
[0],
1297 if (compresult
== BZ_OK
)
1303 #endif /* HAVE_BZLIB */
1305 if (zipflag
== COMPRESS_LZO
) {
1306 lzo_uint worklen2
= worklen
;
1307 compresult
= lzo1x_1_compress((char *)slp
->tblock
[0],writesize
,
1312 if (compresult
== LZO_E_OK
)
1317 #endif /* HAVE_LZO */
1318 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1319 /* write the compressed buffer */
1320 comp_buf
->length
= worklen
;
1321 comp_buf
->compressed
= 1;
1322 buffer
= (char *) comp_buf
;
1323 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1326 /* write the data uncompressed */
1327 comp_buf
->length
= writesize
;
1328 comp_buf
->compressed
= 0;
1329 buffer
= (char *) comp_buf
;
1330 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1331 returns
.unclen
= returns
.clen
;
1332 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1335 /* compress the remaining blocks if we're compressing */
1336 do_compress
= compressed
;
1337 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1339 if (sigsetjmp(jmpbuf
, 1) == 0) {
1348 if (gTapeposfd
>= 0) {
1352 for (i
= 0; (i
< ntrec
) && !foundone
; ++i
) {
1353 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1354 spclptr
= &uspclptr
->s_spcl
;
1355 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1356 (spclptr
->c_type
== TS_INODE
) &&
1357 (spclptr
->c_date
== gThisDumpDate
) &&
1358 !(spclptr
->c_dinode
.di_mode
& S_IFDIR
) &&
1359 !(spclptr
->c_flags
& DR_EXTATTRIBUTES
)
1362 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1365 gtperr
= GetTapePos(&curtapepos
);
1366 /* if an error occured previously don't
1370 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1372 gtperr
= MkTapeString(spclptr
, curtapepos
);
1378 #endif /* USE_QFA */
1380 while (eot_count
< 10 && size
< bufsize
) {
1383 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1386 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1388 printf("slave %d wrote %d\n", slave_number
, wrote
);
1390 if (wrote
< 0 && errno
!= ENOSPC
)
1392 if (wrote
== 0 || (wrote
< 0 && errno
== ENOSPC
))
1399 if (size
!= bufsize
)
1400 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1401 slave_number
, size
, bufsize
);
1405 * Handle ENOSPC as an EOT condition.
1407 if (wrote
< 0 && errno
== ENOSPC
) {
1413 returns
.clen
= returns
.unclen
= 0;
1416 * pass errno back to master for special handling
1419 returns
.unclen
= -errno
;
1422 * pass size of data and size of write back to master
1423 * (for EOT handling)
1425 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1428 * Signal the next slave to go.
1430 (void) kill(nextslave
, SIGUSR2
);
1432 if (gTapeposfd
>= 0) {
1435 #endif /* USE_QFA */
1438 quit("error reading command pipe: %s\n", strerror(errno
));
1442 * Since a read from a pipe may not return all we asked for,
1443 * or a write may not write all we ask if we get a signal,
1444 * loop until the count is satisfied (or error).
1447 dump_atomic_read(int fd
, char *buf
, size_t count
)
1449 int got
, need
= count
;
1452 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1454 } while (got
== -1 && errno
== EINTR
);
1455 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1459 * Since a read from a pipe may not return all we asked for,
1460 * or a write may not write all we ask if we get a signal,
1461 * loop until the count is satisfied (or error).
1464 dump_atomic_write(int fd
, const char *buf
, size_t count
)
1466 int got
, need
= count
;
1469 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1471 } while (got
== -1 && errno
== EINTR
);
1472 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1483 buf.mt_op = MTSETDRVBUFFER;
1484 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1485 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1487 msg("[%ld] error: %d (setting logical)\n",
1488 (unsigned long)getpid(), err);
1495 #define LSEEK_GET_TAPEPOS 10
1496 #define LSEEK_GO2_TAPEPOS 11
1498 * read the current tape position
1501 GetTapePos(long long *pos
)
1507 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
1516 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1517 *pos
= (long long)mtpos
;
1520 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1526 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1534 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
)
1539 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1542 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1543 (unsigned long)spclptr
->c_inumber
,
1546 gTps
[sizeof(gTps
) - 1] = '\0';
1547 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1549 warn("error writing tapepos file. (error %d)\n", errno
);
1553 #endif /* USE_QFA */