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.85 2004/07/01 09:14:49 stelian Exp $";
44 #include <compatlfs.h>
50 #include <compaterr.h>
61 #include <sys/types.h>
63 #include <linux/fs.h> /* for definition of BLKFLSBUF */
66 #include <sys/param.h>
67 #include <sys/socket.h>
71 #ifdef HAVE_EXT2FS_EXT2_FS_H
72 #include <ext2fs/ext2_fs.h>
74 #include <linux/ext2_fs.h>
76 #include <ext2fs/ext2fs.h>
78 #include <bsdcompat.h>
80 #include <sys/vnode.h>
83 #include <ufs/inode.h>
85 #include <ufs/ufs/dinode.h>
86 #include <ufs/ffs/fs.h>
87 #endif /* __linux__ */
89 #include <protocols/dumprestore.h>
93 #endif /* HAVE_ZLIB */
97 #endif /* HAVE_BZLIB */
101 #endif /* HAVE_LZO */
105 int writesize
; /* size of malloc()ed buffer for tape */
106 long lastspclrec
= -1; /* tape block number of last written header */
107 int trecno
= 0; /* next record to write in current block */
108 extern long *blocksperfiles
; /* number of blocks per output file(s) */
109 long blocksperfiles_current
; /* current position in blocksperfiles */
110 long blocksthisvol
; /* number of blocks on current output file */
111 extern int ntrec
; /* blocking factor on tape */
112 extern int cartridge
;
116 long long tapea_bytes
= 0; /* bytes_written at start of current volume */
117 static int magtapeout
; /* output is really a tape */
119 static ssize_t dump_atomic_read
__P((int, char *, size_t));
120 static ssize_t dump_atomic_write
__P((int, const char *, size_t));
122 static void doslave
__P((int, int, int));
124 static void doslave
__P((int, int));
126 static void enslave
__P((void));
127 static void flushtape
__P((void));
128 static void killall
__P((void));
129 static void rollforward
__P((void));
131 static int GetTapePos
__P((long long *));
132 static int MkTapeString
__P((struct s_spcl
*, long long));
133 #define FILESQFAPOS 20
137 * Concurrent dump mods (Caltech) - disk block reading and tape writing
138 * are exported to several slave processes. While one slave writes the
139 * tape, the others read disk blocks; they pass control of the tape in
140 * a ring via signals. The parent process traverses the filesystem and
141 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
142 * The following structure defines the instruction packets sent to slaves.
150 struct slave_results
{
151 ssize_t unclen
; /* uncompressed length */
152 ssize_t clen
; /* compressed length */
155 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
157 int tapea
; /* header number at start of this chunk */
158 int count
; /* count to next header (used for TS_TAPE */
160 int inode
; /* inode that we are currently dealing with */
161 int fd
; /* FD for this slave */
162 int pid
; /* PID for this slave */
163 int sent
; /* 1 == we've sent this slave requests */
164 int firstrec
; /* record number of this block */
165 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
166 struct req
*req
; /* buffer for requests */
170 char (*nextblock
)[TP_BSIZE
];
172 static time_t tstart_volume
; /* time of volume start */
173 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
175 int master
; /* pid of master, for sending error signals */
176 int tenths
; /* length of tape overhead per block written */
177 static int caught
; /* have we caught the signal to proceed? */
178 static int ready
; /* have we reached the lock point without having */
179 /* received the SIGUSR2 signal from the prev slave? */
180 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
181 /* SIGUSR2 arrives from the previous slave */
183 static int gtperr
= 0;
189 int pgoff
= getpagesize() - 1;
193 writesize
= ntrec
* TP_BSIZE
;
194 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
196 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
197 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
198 * repositioning after stopping, i.e, streaming mode, where the gap is
199 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
201 if (!blocksperfiles
&& !unlimited
)
202 tenths
= (cartridge
? 16 : density
== 625 ? 5 : 8);
208 * Allocate tape buffer contiguous with the array of instruction
209 * packets, so flushtape() can write them together with one write().
210 * Align tape buffer on page boundary to speed up tape write().
212 for (i
= 0; i
<= SLAVES
; i
++) {
214 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
217 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
219 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
221 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
223 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
229 nextblock
= slp
->tblock
;
234 writerec(const void *dp
, int isspcl
)
237 slp
->req
[trecno
].dblk
= (ext2_loff_t
)0;
238 slp
->req
[trecno
].count
= 1;
239 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
240 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
242 /* Need to write it to the archive file */
243 if (! AfileActive
&& isspcl
&& (spcl
.c_type
== TS_END
))
245 if (AfileActive
&& Afile
>= 0) {
246 /* When we dump an inode which is not a directory,
247 * it means we ended the archive contents */
248 if (isspcl
&& (spcl
.c_type
== TS_INODE
) &&
249 ((spcl
.c_dinode
.di_mode
& S_IFMT
) != IFDIR
))
253 tmp
= *(union u_spcl
*)dp
;
254 /* Write the record, _uncompressed_ */
256 tmp
.s_spcl
.c_flags
&= ~DR_COMPRESSED
;
259 if (write(Afile
, &tmp
, TP_BSIZE
) != TP_BSIZE
)
260 msg("error writing archive file: %s\n",
267 lastspclrec
= spcl
.c_tapea
;
275 dumpblock(blk_t blkno
, int size
)
280 dblkno
= fsbtodb(sblock
, blkno
);
281 tpblks
= size
>> tp_bshift
;
282 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
283 slp
->req
[trecno
].dblk
= dblkno
;
284 slp
->req
[trecno
].count
= avail
;
286 spcl
.c_tapea
+= avail
;
289 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
301 msg("write error on %s: %s\n", tape
, strerror(errnum
));
302 quit("Cannot recover\n");
305 msg("write error %d blocks into volume %d: %s\n",
306 blocksthisvol
, tapeno
, strerror(errnum
));
307 broadcast("DUMP WRITE ERROR!\n");
308 if (query("Do you want to rewrite this volume?")) {
309 msg("Closing this volume. Prepare to restart with new media;\n");
310 msg("this dump volume will be rewritten.\n");
316 if (query("Do you want to start the next tape?"))
322 sigpipe(UNUSED(int signo
))
325 quit("Broken pipe\n");
330 * Update xferrate stats
339 ttaken
= tnow
- tstart_volume
;
340 blocks
= spcl
.c_tapea
- tapea_volume
;
341 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
343 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno
,
344 blocks
, ((double)blocks
* TP_BSIZE
/ 1048576));
346 long volkb
= (bytes_written
- tapea_bytes
) / 1024;
347 long txfrate
= volkb
/ ttaken
;
348 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
349 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
350 msg("Volume %d transfer rate: %ld kB/s\n", tapeno
,
354 double rate
= .0005 + (double) blocks
/ (double) volkb
;
355 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
357 tapeno
, blocks
, volkb
, rate
);
364 mktimeest(time_t tnow
)
366 static char msgbuf
[128];
371 if (blockswritten
< 500)
373 if (blockswritten
> tapesize
)
374 tapesize
= blockswritten
;
375 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
376 / blockswritten
* tapesize
;
377 if (tnow
> tstart_volume
)
378 (void)snprintf(msgbuf
, sizeof(msgbuf
),
379 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
380 (blockswritten
* 100.0) / tapesize
,
381 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
382 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
384 (void)snprintf(msgbuf
, sizeof(msgbuf
),
385 "%3.2f%% done, finished in %d:%02d\n",
386 (blockswritten
* 100.0) / tapesize
,
387 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
395 * information message upon receipt of SIGINFO
398 statussig(int notused
)
400 int save_errno
= errno
;
403 buf
= mktimeest(time(NULL
));
405 write(STDERR_FILENO
, buf
, strlen(buf
));
415 struct slave_results returned
;
417 int siz
= (char *)nextblock
- (char *)slp
->req
;
419 slp
->req
[trecno
].count
= 0; /* Sentinel */
421 if (dump_atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
422 quit("error writing command pipe: %s\n", strerror(errno
));
423 slp
->sent
= 1; /* we sent a request, read the response later */
425 lastfirstrec
= slp
->firstrec
;
427 if (++slp
>= &slaves
[SLAVES
])
430 /* Read results back from next slave */
432 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
433 != sizeof returned
) {
434 perror(" DUMP: error reading command pipe in master");
437 got
= returned
.unclen
;
438 bytes_written
+= returned
.clen
;
439 if (returned
.unclen
== returned
.clen
)
443 /* Check for errors or end of tape */
445 /* Check for errors */
449 msg("End of tape detected\n");
452 * Drain the results, don't care what the values were.
453 * If we read them here then trewind won't...
455 for (i
= 0; i
< SLAVES
; i
++) {
456 if (slaves
[i
].sent
) {
457 if (dump_atomic_read( slaves
[i
].fd
,
458 (char *)&returned
, sizeof returned
)
459 != sizeof returned
) {
460 perror(" DUMP: error reading command pipe in master");
474 if (spcl
.c_type
!= TS_END
) {
475 for (i
= 0; i
< spcl
.c_count
; i
++)
476 if (spcl
.c_addr
[i
] != 0)
479 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
480 slp
->tapea
= spcl
.c_tapea
;
481 slp
->firstrec
= lastfirstrec
+ ntrec
;
483 nextblock
= slp
->tblock
;
485 asize
+= tenths
+ returned
.clen
/ density
;
486 blockswritten
+= ntrec
;
487 blocksthisvol
+= ntrec
;
488 if (!pipeout
&& !unlimited
) {
489 if (blocksperfiles
&& blocksperfiles
[blocksperfiles_current
]) {
490 if ( compressed
? (bytes_written
- tapea_bytes
+ SLAVES
* (writesize
+ sizeof(struct tapebuf
))) >= (((long long)blocksperfiles
[blocksperfiles_current
]) * 1024)
491 : blocksthisvol
>= blocksperfiles
[blocksperfiles_current
] ) {
496 else if (asize
> tsize
) {
509 struct slave_results returned
;
511 for (f
= 0; f
< SLAVES
; f
++) {
513 * Drain the results, but unlike EOT we DO (or should) care
514 * what the return values were, since if we detect EOT after
515 * we think we've written the last blocks to the tape anyway,
516 * we have to replay those blocks with rollforward.
518 * fixme: punt for now.
520 if (slaves
[f
].sent
) {
521 if (dump_atomic_read( slaves
[f
].fd
, (char *)&returned
, sizeof returned
)
522 != sizeof returned
) {
523 perror(" DUMP: error reading command pipe in master");
526 got
= returned
.unclen
;
527 bytes_written
+= returned
.clen
;
528 if (returned
.unclen
== returned
.clen
)
536 msg("EOT detected in last 2 tape records!\n");
537 msg("Use a longer tape, decrease the size estimate\n");
538 quit("or use no size estimate at all.\n");
541 (void) close(slaves
[f
].fd
);
543 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
548 msg("Closing %s\n", tape
);
553 while (rmtopen(tape
, O_RDONLY
) < 0)
560 (void) close(tapefd
);
562 while ((f
= OPEN(tape
, O_RDONLY
)) < 0)
578 msg("Launching %s\n", eot_script
);
579 eot_code
= system_command(eot_script
, tape
, tapeno
);
581 if (eot_code
!= 0 && eot_code
!= 1) {
582 msg("Dump aborted by the end of tape script\n");
587 if (nexttape
|| Mflag
)
590 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
591 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
593 while (!query("Is the new volume mounted and ready to go?"))
594 if (query("Do you want to abort?")) {
603 struct req
*p
, *q
= NULL
, *prev
;
605 int i
, size
, savedtapea
, got
;
606 union u_spcl
*ntb
, *otb
;
607 struct slave_results returned
;
612 tslp
= &slaves
[SLAVES
];
613 ntb
= (union u_spcl
*)tslp
->tblock
[1];
616 * Each of the N slaves should have requests that need to
617 * be replayed on the next tape. Use the extra slave buffers
618 * (slaves[SLAVES]) to construct request lists to be sent to
619 * each slave in turn.
621 for (i
= 0; i
< SLAVES
; i
++) {
623 otb
= (union u_spcl
*)slp
->tblock
;
626 * For each request in the current slave, copy it to tslp.
630 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
633 *ntb
++ = *otb
++; /* copy the datablock also */
638 quit("rollforward: protocol botch");
650 nextblock
= tslp
->tblock
;
651 savedtapea
= spcl
.c_tapea
;
652 spcl
.c_tapea
= slp
->tapea
;
654 spcl
.c_tapea
= savedtapea
;
655 lastspclrec
= savedtapea
- 1;
657 size
= (char *)ntb
- (char *)q
;
658 if (dump_atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
659 perror(" DUMP: error writing command pipe");
664 lastfirstrec
= slp
->firstrec
;
666 if (++slp
>= &slaves
[SLAVES
])
671 if (prev
->dblk
!= 0) {
673 * If the last one was a disk block, make the
674 * first of this one be the last bit of that disk
677 q
->dblk
= prev
->dblk
+
678 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
679 ntb
= (union u_spcl
*)tslp
->tblock
;
682 * It wasn't a disk block. Copy the data to its
683 * new location in the buffer.
686 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
687 ntb
= (union u_spcl
*)tslp
->tblock
[1];
691 nextblock
= slp
->tblock
;
694 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
695 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
702 * Clear the first slaves' response. One hopes that it
703 * worked ok, otherwise the tape is much too short!
706 if (dump_atomic_read( slp
->fd
, (char *)&returned
, sizeof returned
)
707 != sizeof returned
) {
708 perror(" DUMP: error reading command pipe in master");
711 got
= returned
.unclen
;
712 bytes_written
+= returned
.clen
;
713 if (returned
.clen
== returned
.unclen
)
721 quit("EOT detected at start of the tape!\n");
727 if (spcl
.c_type
!= TS_END
) {
728 for (i
= 0; i
< spcl
.c_count
; i
++)
729 if (spcl
.c_addr
[i
] != 0)
733 slp
->firstrec
= lastfirstrec
+ ntrec
;
734 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
736 asize
+= tenths
+ returned
.clen
/ density
;
737 blockswritten
+= ntrec
;
738 blocksthisvol
+= ntrec
;
743 * We implement taking and restoring checkpoints on the tape level.
744 * When each tape is opened, a new process is created by forking; this
745 * saves all of the necessary context in the parent. The child
746 * continues the dump; the parent waits around, saving the context.
747 * If the child returns X_REWRITE, then it had problems writing that tape;
748 * this causes the parent to fork again, duplicating the context, and
749 * everything continues as if nothing had happened.
752 startnewtape(int top
)
763 sigaddset(&sigs
, SIGINT
);
764 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
765 #else /* __linux__ */
767 void (*interrupt_save
)();
769 sig_t interrupt_save
;
771 interrupt_save
= signal(SIGINT
, SIG_IGN
);
772 #endif /* __linux__ */
774 parentpid
= getpid();
775 tapea_volume
= spcl
.c_tapea
;
776 tapea_bytes
= bytes_written
;
777 tstart_volume
= time(NULL
);
781 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
783 (void)signal(SIGINT
, interrupt_save
);
786 * All signals are inherited...
790 msg("Context save fork fails in parent %d\n", parentpid
);
796 * save the context by waiting
797 * until the child doing all of the work returns.
798 * don't catch the interrupt
801 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
803 signal(SIGINT
, SIG_IGN
);
806 msg("Tape: %d; parent process: %d child process %d\n",
807 tapeno
+1, parentpid
, childpid
);
809 while ((waitpid
= wait(&status
)) != childpid
)
810 if (waitpid
!= rshpid
)
811 msg("Parent %d waiting for child %d has another child %d return\n",
812 parentpid
, childpid
, waitpid
);
814 msg("Child %d returns LOB status %o\n",
815 childpid
, status
&0xFF);
817 status
= (status
>> 8) & 0xFF;
821 msg("Child %d finishes X_FINOK\n", childpid
);
824 msg("Child %d finishes X_ABORT\n", childpid
);
827 msg("Child %d finishes X_REWRITE\n", childpid
);
830 msg("Child %d finishes unknown %d\n",
841 goto restore_check_point
;
843 msg("Bad return code from dump: %d\n", status
);
847 } else { /* we are the child; just continue */
849 sleep(4); /* allow time for parent's message to get out */
850 msg("Child on Tape %d has parent %d, my pid = %d\n",
851 tapeno
+1, parentpid
, getpid());
854 * If we have a name like "/dev/rmt0,/dev/rmt1",
855 * use the name before the comma first, and save
856 * the remaining names for subsequent volumes.
858 tapeno
++; /* current tape sequence */
860 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
861 tape
[MAXPATHLEN
- 1] = '\0';
862 msg("Dumping volume %d on %s\n", tapeno
, tape
);
864 else if (nexttape
|| strchr(tapeprefix
, ',')) {
865 if (nexttape
&& *nexttape
)
866 tapeprefix
= nexttape
;
867 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
872 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
873 tape
[MAXPATHLEN
- 1] = '\0';
874 msg("Dumping volume %d on %s\n", tapeno
, tape
);
876 if (blocksperfiles
&& blocksperfiles_current
< *blocksperfiles
)
877 blocksperfiles_current
++;
879 while ((tapefd
= (host
? rmtopen(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
) : pipeout
?
881 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
883 while ((tapefd
= (pipeout
? fileno(stdout
) :
884 OPEN(tape
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666))) < 0)
887 msg("Cannot open output \"%s\": %s\n", tape
,
889 if (!query("Do you want to retry the open?"))
896 struct mtget mt_stat
;
897 magtapeout
= ioctl(tapefd
, MTIOCGET
, (char *)&mt_stat
) == 0;
899 msg("Output is to %s\n",
900 magtapeout ? "tape" : "file/pipe");
904 enslave(); /* Share open tape file descriptor with slaves */
909 newtape
++; /* new tape signal */
910 spcl
.c_count
= slp
->count
;
912 * measure firstrec in TP_BSIZE units since restore doesn't
913 * know the correct ntrec value...
915 spcl
.c_firstrec
= slp
->firstrec
;
917 spcl
.c_type
= TS_TAPE
;
918 spcl
.c_flags
|= DR_NEWHEADER
;
919 spcl
.c_ntrec
= ntrec
;
921 spcl
.c_flags
|= DR_COMPRESSED
;
922 writeheader((dump_ino_t
)slp
->inode
);
923 spcl
.c_flags
&=~ DR_NEWHEADER
;
924 msg("Volume %d started with block %ld at: %s", tapeno
,
925 spcl
.c_tapea
, ctime(&tstart_volume
));
927 msg("Volume %d begins with blocks from inode %d\n",
929 if (tapeno
< (int)TP_NINOS
)
930 volinfo
[tapeno
] = slp
->inode
;
935 dumpabort(UNUSED(int signo
))
938 if (master
!= 0 && master
!= getpid())
939 /* Signals master to call dumpabort */
940 (void) kill(master
, SIGTERM
);
943 msg("The ENTIRE dump is aborted.\n");
956 msg("pid = %d exits with status %d\n", getpid(), status
);
962 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
965 proceed(UNUSED(int signo
))
968 siglongjmp(jmpbuf
, 1);
976 #ifdef LINUX_FORK_BUG
984 { struct sigaction sa
;
985 memset(&sa
, 0, sizeof sa
);
986 sigemptyset(&sa
.sa_mask
);
987 sa
.sa_handler
= dumpabort
;
988 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
989 sa
.sa_handler
= sigpipe
;
990 sigaction(SIGPIPE
, &sa
, NULL
);
991 sa
.sa_handler
= proceed
;
992 sa
.sa_flags
= SA_RESTART
;
993 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
996 for (i
= 0; i
< SLAVES
; i
++) {
997 if (i
== slp
- &slaves
[0]) {
1003 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
1004 (slaves
[i
].pid
= fork()) < 0)
1005 quit("too many slaves, %d (recompile smaller): %s\n",
1006 i
, strerror(errno
));
1008 slaves
[i
].fd
= cmd
[1];
1010 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
1012 for (j
= 0; j
<= i
; j
++)
1013 (void) close(slaves
[j
].fd
);
1015 sigaddset(&sigs
, SIGINT
); /* Master handles this */
1016 #if defined(SIGINFO)
1017 sigaddset(&sigs
, SIGINFO
);
1019 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
1021 #ifdef LINUX_FORK_BUG
1022 if (dump_atomic_write( cmd
[0], (char *) &i
, sizeof i
)
1024 quit("master/slave protocol botched 3\n");
1030 (slaves
[i
].pid
== slp
->pid
));
1037 #ifdef LINUX_FORK_BUG
1039 * Wait for all slaves to _actually_ start to circumvent a bug in
1040 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1041 * returned from fork() causes a SEGV in the child process
1043 for (i
= 0; i
< SLAVES
; i
++)
1044 if (dump_atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
1045 quit("master/slave protocol botched 4\n");
1048 for (i
= 0; i
< SLAVES
; i
++)
1049 (void) dump_atomic_write( slaves
[i
].fd
,
1050 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
1051 sizeof slaves
[0].pid
);
1061 for (i
= 0; i
< SLAVES
; i
++)
1062 if (slaves
[i
].pid
> 0) {
1063 (void) kill(slaves
[i
].pid
, SIGKILL
);
1069 * Synchronization - each process waits for a SIGUSR2 from the
1070 * previous process before writing to the tape, and sends SIGUSR2
1071 * to the next process when the tape write completes. On tape errors
1072 * a SIGUSR1 is sent to the master which then terminates all of the
1084 volatile int wrote
= 0, size
, eot_count
, bufsize
;
1085 char * volatile buffer
;
1086 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1087 struct tapebuf
* volatile comp_buf
= NULL
;
1089 volatile int do_compress
= !first
;
1090 unsigned long worklen
;
1092 lzo_align_t __LZO_MMODEL
*LZO_WorkMem
;
1094 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1095 struct slave_results returns
;
1100 long long curtapepos
;
1101 union u_spcl
*uspclptr
;
1102 struct s_spcl
*spclptr
;
1103 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1104 long maxntrecs
= 50000; /* every 50MB */
1105 long cntntrecs
= maxntrecs
;
1106 #endif /* USE_QFA */
1110 sigaddset(&set
, SIGUSR2
);
1111 sigprocmask(SIG_BLOCK
, &set
, NULL
);
1115 * Need our own seek pointer.
1117 (void) close(diskfd
);
1118 if ((diskfd
= OPEN(disk
, O_RDONLY
)) < 0)
1119 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1122 (void)ioctl(diskfd
, BLKFLSBUF
, 0);
1125 retval
= dump_fs_open(disk
, &fs
);
1127 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1128 #endif /* __linux__ */
1131 * Need the pid of the next slave in the loop...
1133 if ((nread
= dump_atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1134 != sizeof nextslave
) {
1135 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1138 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1139 /* if we're doing a compressed dump, allocate the compress buffer */
1141 int bsiz
= sizeof(struct tapebuf
) + writesize
;
1142 /* Add extra space to deal with compression enlarging the buffer */
1143 if (TP_BSIZE
> writesize
/16 + 67)
1146 bsiz
+= writesize
/16 + 67;
1147 comp_buf
= malloc(bsiz
);
1148 if (comp_buf
== NULL
)
1149 quit("couldn't allocate a compress buffer.\n");
1150 if (zipflag
== COMPRESS_ZLIB
)
1151 comp_buf
->flags
= COMPRESS_ZLIB
;
1152 else if (zipflag
== COMPRESS_BZLIB
)
1153 comp_buf
->flags
= COMPRESS_BZLIB
;
1154 else if (zipflag
== COMPRESS_LZO
) {
1155 comp_buf
->flags
= COMPRESS_LZO
;
1156 if (lzo_init() != LZO_E_OK
) quit("lzo_init failed\n");
1158 quit("internal error - unknown compression method: %d\n", zipflag
);
1161 LZO_WorkMem
= malloc(LZO1X_1_MEM_COMPRESS
);
1163 quit("couldn't allocate a compress buffer.\n");
1165 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1168 * Get list of blocks to dump, read the blocks into tape buffer
1170 while ((nread
= dump_atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1171 struct req
*p
= slp
->req
;
1173 for (trecno
= 0; trecno
< ntrec
;
1174 trecno
+= p
->count
, p
+= p
->count
) {
1175 if (p
->dblk
) { /* read a disk block */
1176 bread(p
->dblk
, slp
->tblock
[trecno
],
1177 p
->count
* TP_BSIZE
);
1178 } else { /* read record from pipe */
1179 if (p
->count
!= 1 || dump_atomic_read( cmd
,
1180 (char *)slp
->tblock
[trecno
],
1181 TP_BSIZE
) != TP_BSIZE
)
1182 quit("master/slave protocol botched.\n");
1186 /* Try to write the data... */
1190 buffer
= (char *) slp
->tblock
[0]; /* set write pointer */
1191 bufsize
= writesize
; /* length to write */
1192 returns
.clen
= returns
.unclen
= bufsize
;
1194 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1196 * When writing a compressed dump, each block except
1197 * the first one on each tape is written
1198 * from struct tapebuf with an 4 byte prefix
1199 * followed by the data. This can be less than
1200 * writesize. Restore, on a short read, can compare the
1201 * length read to the compressed length in the header
1202 * to verify that the read was good. Blocks which don't
1203 * compress well are written uncompressed.
1204 * The first block written by each slave is not compressed
1205 * and does not have a prefix.
1208 if (compressed
&& do_compress
) {
1209 comp_buf
->length
= bufsize
;
1210 worklen
= TP_BSIZE
+ writesize
;
1213 if (zipflag
== COMPRESS_ZLIB
) {
1214 compresult
= compress2(comp_buf
->buf
,
1216 (char *)slp
->tblock
[0],
1219 if (compresult
== Z_OK
)
1224 #endif /* HAVE_ZLIB */
1226 if (zipflag
== COMPRESS_BZLIB
) {
1227 unsigned int worklen2
= worklen
;
1228 compresult
= BZ2_bzBuffToBuffCompress(
1231 (char *)slp
->tblock
[0],
1236 if (compresult
== BZ_OK
)
1242 #endif /* HAVE_BZLIB */
1244 if (zipflag
== COMPRESS_LZO
) {
1245 lzo_uint worklen2
= worklen
;
1246 compresult
= lzo1x_1_compress((char *)slp
->tblock
[0],writesize
,
1251 if (compresult
== LZO_E_OK
)
1256 #endif /* HAVE_LZO */
1257 if (compresult
&& worklen
<= ((unsigned long)writesize
- 16)) {
1258 /* write the compressed buffer */
1259 comp_buf
->length
= worklen
;
1260 comp_buf
->compressed
= 1;
1261 buffer
= (char *) comp_buf
;
1262 returns
.clen
= bufsize
= worklen
+ sizeof(struct tapebuf
);
1265 /* write the data uncompressed */
1266 comp_buf
->length
= writesize
;
1267 comp_buf
->compressed
= 0;
1268 buffer
= (char *) comp_buf
;
1269 returns
.clen
= bufsize
= writesize
+ sizeof(struct tapebuf
);
1270 returns
.unclen
= returns
.clen
;
1271 memcpy(comp_buf
->buf
, (char *)slp
->tblock
[0], writesize
);
1274 /* compress the remaining blocks if we're compressing */
1275 do_compress
= compressed
;
1276 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1278 if (sigsetjmp(jmpbuf
, 1) == 0) {
1287 if (gTapeposfd
>= 0) {
1291 for (i
= 0; (i
< ntrec
) && !foundone
; ++i
) {
1292 uspclptr
= (union u_spcl
*)&slp
->tblock
[i
];
1293 spclptr
= &uspclptr
->s_spcl
;
1294 if ((spclptr
->c_magic
== NFS_MAGIC
) &&
1295 (spclptr
->c_type
== TS_INODE
) &&
1296 (spclptr
->c_date
== gThisDumpDate
) &&
1297 !(spclptr
->c_dinode
.di_mode
& S_IFDIR
)
1300 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1303 gtperr
= GetTapePos(&curtapepos
);
1304 /* if an error occured previously don't
1308 msg("inode %ld at tapepos %ld\n", spclptr
->c_inumber
, curtapepos
);
1310 gtperr
= MkTapeString(spclptr
, curtapepos
);
1316 #endif /* USE_QFA */
1318 while (eot_count
< 10 && size
< bufsize
) {
1321 wrote
= rmtwrite(buffer
+ size
, bufsize
- size
);
1324 wrote
= write(tapefd
, buffer
+ size
, bufsize
- size
);
1326 printf("slave %d wrote %d\n", slave_number
, wrote
);
1328 if (wrote
< 0 && errno
!= ENOSPC
)
1330 if (wrote
== 0 || (wrote
< 0 && errno
== ENOSPC
))
1337 if (size
!= bufsize
)
1338 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1339 slave_number
, size
, bufsize
);
1343 * Handle ENOSPC as an EOT condition.
1345 if (wrote
< 0 && errno
== ENOSPC
) {
1351 returns
.clen
= returns
.unclen
= 0;
1354 * pass errno back to master for special handling
1357 returns
.unclen
= -errno
;
1360 * pass size of data and size of write back to master
1361 * (for EOT handling)
1363 (void) dump_atomic_write( cmd
, (char *)&returns
, sizeof returns
);
1366 * Signal the next slave to go.
1368 (void) kill(nextslave
, SIGUSR2
);
1370 if (gTapeposfd
>= 0) {
1373 #endif /* USE_QFA */
1376 quit("error reading command pipe: %s\n", strerror(errno
));
1380 * Since a read from a pipe may not return all we asked for,
1381 * or a write may not write all we ask if we get a signal,
1382 * loop until the count is satisfied (or error).
1385 dump_atomic_read(int fd
, char *buf
, size_t count
)
1387 int got
, need
= count
;
1390 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1392 } while (got
== -1 && errno
== EINTR
);
1393 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1397 * Since a read from a pipe may not return all we asked for,
1398 * or a write may not write all we ask if we get a signal,
1399 * loop until the count is satisfied (or error).
1402 dump_atomic_write(int fd
, const char *buf
, size_t count
)
1404 int got
, need
= count
;
1407 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1409 } while (got
== -1 && errno
== EINTR
);
1410 return (got
< 0 ? got
: (ssize_t
)count
- need
);
1421 buf.mt_op = MTSETDRVBUFFER;
1422 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1423 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1425 msg("[%ld] error: %d (setting logical)\n",
1426 (unsigned long)getpid(), err);
1433 #define LSEEK_GET_TAPEPOS 10
1434 #define LSEEK_GO2_TAPEPOS 11
1436 * read the current tape position
1439 GetTapePos(long long *pos
)
1445 *pos
= (long long) rmtseek((OFF_T
)0, (int)LSEEK_GET_TAPEPOS
);
1454 err
= (ioctl(tapefd
, MTIOCPOS
, &mtpos
) < 0);
1455 *pos
= (long long)mtpos
;
1458 *pos
= LSEEK(tapefd
, 0, SEEK_CUR
);
1464 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1472 MkTapeString(struct s_spcl
*spclptr
, long long curtapepos
)
1477 msg("inode %ld at tapepos %lld\n", spclptr
->c_inumber
, curtapepos
);
1480 snprintf(gTps
, sizeof(gTps
), "%ld\t%d\t%lld\n",
1481 (unsigned long)spclptr
->c_inumber
,
1484 gTps
[sizeof(gTps
) - 1] = '\0';
1485 if (write(gTapeposfd
, gTps
, strlen(gTps
)) != (ssize_t
)strlen(gTps
)) {
1487 warn("error writing tapepos file. (error %d)\n", errno
);
1491 #endif /* USE_QFA */