]>
git.wh0rd.org - dump.git/blob - dump/tape.c
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 <pop@cybercable.fr>, 1999-2000
9 * Copyright (c) 1980, 1991, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid
[] =
43 "$Id: tape.c,v 1.21 2000/08/19 22:06:03 stelian Exp $";
47 #include <sys/types.h>
48 #include <linux/types.h>
50 #include <sys/param.h>
51 #include <sys/socket.h>
55 #include <linux/ext2_fs.h>
56 #include <bsdcompat.h>
59 #include <sys/vnode.h>
62 #include <ufs/inode.h>
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
67 #endif /* __linux__ */
69 #include <protocols/dumprestore.h>
76 #include <compaterr.h>
86 #include <ext2fs/ext2fs.h>
91 int writesize
; /* size of malloc()ed buffer for tape */
92 long lastspclrec
= -1; /* tape block number of last written header */
93 int trecno
= 0; /* next record to write in current block */
94 extern long blocksperfile
; /* number of blocks per output file */
95 long blocksthisvol
; /* number of blocks on current output file */
96 extern int ntrec
; /* blocking factor on tape */
103 static ssize_t atomic_read
__P((int, void *, size_t));
104 static ssize_t atomic_write
__P((int, const void *, size_t));
105 static void doslave
__P((int, int));
106 static void enslave
__P((void));
107 static void flushtape
__P((void));
108 static void killall
__P((void));
109 static void rollforward
__P((void));
110 static int system_command
__P((const char *, const char *, int));
113 * Concurrent dump mods (Caltech) - disk block reading and tape writing
114 * are exported to several slave processes. While one slave writes the
115 * tape, the others read disk blocks; they pass control of the tape in
116 * a ring via signals. The parent process traverses the filesystem and
117 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
118 * The following structure defines the instruction packets sent to slaves.
126 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
128 int tapea
; /* header number at start of this chunk */
129 int count
; /* count to next header (used for TS_TAPE */
131 int inode
; /* inode that we are currently dealing with */
132 int fd
; /* FD for this slave */
133 int pid
; /* PID for this slave */
134 int sent
; /* 1 == we've sent this slave requests */
135 int firstrec
; /* record number of this block */
136 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
137 struct req
*req
; /* buffer for requests */
141 char (*nextblock
)[TP_BSIZE
];
143 static time_t tstart_volume
; /* time of volume start */
144 static int tapea_volume
; /* value of spcl.c_tapea at volume start */
146 int master
; /* pid of master, for sending error signals */
147 int tenths
; /* length of tape used per block written */
148 static int caught
; /* have we caught the signal to proceed? */
149 static int ready
; /* have we reached the lock point without having */
150 /* received the SIGUSR2 signal from the prev slave? */
151 static sigjmp_buf jmpbuf
; /* where to jump to if we are ready when the */
152 /* SIGUSR2 arrives from the previous slave */
157 int pgoff
= getpagesize() - 1;
161 writesize
= ntrec
* TP_BSIZE
;
162 reqsiz
= (ntrec
+ 1) * sizeof(struct req
);
164 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
165 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
166 * repositioning after stopping, i.e, streaming mode, where the gap is
167 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
169 if (blocksperfile
== 0 && !unlimited
)
170 tenths
= writesize
/ density
+
171 (cartridge
? 16 : density
== 625 ? 5 : 8);
173 * Allocate tape buffer contiguous with the array of instruction
174 * packets, so flushtape() can write them together with one write().
175 * Align tape buffer on page boundary to speed up tape write().
177 for (i
= 0; i
<= SLAVES
; i
++) {
179 malloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
182 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
184 (((long)&buf
[reqsiz
] + pgoff
) &~ pgoff
);
186 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
188 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
194 nextblock
= slp
->tblock
;
199 writerec(const void *dp
, int isspcl
)
202 slp
->req
[trecno
].dblk
= (daddr_t
)0;
203 slp
->req
[trecno
].count
= 1;
204 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
205 *(union u_spcl
*)(*(nextblock
)) = *(union u_spcl
*)dp
;
208 lastspclrec
= spcl
.c_tapea
;
216 dumpblock(daddr_t blkno
, int size
)
218 int avail
, tpblks
, dblkno
;
220 dblkno
= fsbtodb(sblock
, blkno
);
221 tpblks
= size
>> tp_bshift
;
222 while ((avail
= MIN(tpblks
, ntrec
- trecno
)) > 0) {
223 slp
->req
[trecno
].dblk
= dblkno
;
224 slp
->req
[trecno
].count
= avail
;
226 spcl
.c_tapea
+= avail
;
229 dblkno
+= avail
<< (tp_bshift
- dev_bshift
);
241 msg("write error on %s\n", tape
);
242 quit("Cannot recover\n");
245 msg("write error %d blocks into volume %d\n", blocksthisvol
, tapeno
);
246 broadcast("DUMP WRITE ERROR!\n");
247 if (!query("Do you want to restart?"))
249 msg("Closing this volume. Prepare to restart with new media;\n");
250 msg("this dump volume will be rewritten.\n");
261 quit("Broken pipe\n");
266 * Update xferrate stats
279 ttaken
= tnow
- tstart_volume
;
280 blocks
= spcl
.c_tapea
- tapea_volume
;
281 msg("Volume %d completed at: %s", tapeno
,
288 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
289 ttaken
/ 3600, (ttaken
% 3600) / 60, ttaken
% 60);
290 msg("Volume %d transfer rate: %ld KB/s\n", tapeno
,
292 xferrate
+= blocks
/ ttaken
;
300 * information message upon receipt of SIGINFO
301 * (derived from optr.c::timeest())
304 statussig(int notused
)
308 int save_errno
= errno
;
310 if (blockswritten
< 500)
315 (void) time((time_t *) &tnow
);
317 if (blockswritten
> tapesize
)
318 tapesize
= blockswritten
;
319 deltat
= tstart_writing
- tnow
+ (1.0 * (tnow
- tstart_writing
))
320 / blockswritten
* tapesize
;
321 (void)snprintf(msgbuf
, sizeof(msgbuf
),
322 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
323 (blockswritten
* 100.0) / tapesize
,
324 (spcl
.c_tapea
- tapea_volume
) / (tnow
- tstart_volume
),
325 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
326 write(STDERR_FILENO
, msgbuf
, strlen(msgbuf
));
337 int siz
= (char *)nextblock
- (char *)slp
->req
;
339 slp
->req
[trecno
].count
= 0; /* Sentinel */
341 if (atomic_write( slp
->fd
, (char *)slp
->req
, siz
) != siz
)
342 quit("error writing command pipe: %s\n", strerror(errno
));
343 slp
->sent
= 1; /* we sent a request, read the response later */
345 lastfirstrec
= slp
->firstrec
;
347 if (++slp
>= &slaves
[SLAVES
])
350 /* Read results back from next slave */
352 if (atomic_read( slp
->fd
, (char *)&got
, sizeof got
)
354 perror(" DUMP: error reading command pipe in master");
359 /* Check for end of tape */
360 if (got
< writesize
) {
361 msg("End of tape detected\n");
364 * Drain the results, don't care what the values were.
365 * If we read them here then trewind won't...
367 for (i
= 0; i
< SLAVES
; i
++) {
368 if (slaves
[i
].sent
) {
369 if (atomic_read( slaves
[i
].fd
,
370 (char *)&got
, sizeof got
)
372 perror(" DUMP: error reading command pipe in master");
386 if (spcl
.c_type
!= TS_END
) {
387 for (i
= 0; i
< spcl
.c_count
; i
++)
388 if (spcl
.c_addr
[i
] != 0)
391 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
392 slp
->tapea
= spcl
.c_tapea
;
393 slp
->firstrec
= lastfirstrec
+ ntrec
;
395 nextblock
= slp
->tblock
;
398 blockswritten
+= ntrec
;
399 blocksthisvol
+= ntrec
;
400 if (!pipeout
&& !unlimited
&& (blocksperfile
?
401 (blocksthisvol
>= blocksperfile
) : (asize
> tsize
))) {
409 * Executes the command in a shell.
410 * Returns -1 if an error occured, the exit status of
411 * the command on success.
413 int system_command(const char *command
, const char *device
, int volnum
) {
415 char commandstr
[4096];
419 perror(" DUMP: unable to fork");
425 #if OLD_STYLE_FSCRIPT
426 snprintf(commandstr
, sizeof(commandstr
), "%s", command
);
428 snprintf(commandstr
, sizeof(commandstr
), "%s %s %d", command
, device
, volnum
);
430 commandstr
[sizeof(commandstr
) - 1] = '\0';
431 execl("/bin/sh", "sh", "-c", commandstr
, NULL
);
432 perror(" DUMP: unable to execute shell");
436 if (waitpid(pid
, &status
, 0) == -1) {
437 if (errno
!= EINTR
) {
438 perror(" DUMP: waitpid error");
442 if (WIFEXITED(status
))
443 return WEXITSTATUS(status
);
456 for (f
= 0; f
< SLAVES
; f
++) {
458 * Drain the results, but unlike EOT we DO (or should) care
459 * what the return values were, since if we detect EOT after
460 * we think we've written the last blocks to the tape anyway,
461 * we have to replay those blocks with rollforward.
463 * fixme: punt for now.
465 if (slaves
[f
].sent
) {
466 if (atomic_read( slaves
[f
].fd
, (char *)&got
, sizeof got
)
468 perror(" DUMP: error reading command pipe in master");
472 if (got
!= writesize
) {
473 msg("EOT detected in last 2 tape records!\n");
474 msg("Use a longer tape, decrease the size estimate\n");
475 quit("or use no size estimate at all.\n");
478 (void) close(slaves
[f
].fd
);
480 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
485 msg("Closing %s\n", tape
);
490 while (rmtopen(tape
, 0) < 0)
497 (void) close(tapefd
);
498 while ((f
= open(tape
, 0)) < 0)
503 if (eot_script
&& spcl
.c_type
!= TS_END
) {
504 msg("Launching %s\n", eot_script
);
505 eot_code
= system_command(eot_script
, tape
, tapeno
);
507 if (eot_code
!= 0 && eot_code
!= 1) {
508 msg("Dump aborted by the end of tape script\n");
520 if (nexttape
|| Mflag
|| (eot_code
== 0) )
523 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
524 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
526 while (!query("Is the new volume mounted and ready to go?"))
527 if (query("Do you want to abort?")) {
536 register struct req
*p
, *q
, *prev
;
537 register struct slave
*tslp
;
538 int i
, size
, savedtapea
, got
;
539 union u_spcl
*ntb
, *otb
;
544 tslp
= &slaves
[SLAVES
];
545 ntb
= (union u_spcl
*)tslp
->tblock
[1];
548 * Each of the N slaves should have requests that need to
549 * be replayed on the next tape. Use the extra slave buffers
550 * (slaves[SLAVES]) to construct request lists to be sent to
551 * each slave in turn.
553 for (i
= 0; i
< SLAVES
; i
++) {
555 otb
= (union u_spcl
*)slp
->tblock
;
558 * For each request in the current slave, copy it to tslp.
562 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
565 *ntb
++ = *otb
++; /* copy the datablock also */
570 quit("rollforward: protocol botch");
582 nextblock
= tslp
->tblock
;
583 savedtapea
= spcl
.c_tapea
;
584 spcl
.c_tapea
= slp
->tapea
;
586 spcl
.c_tapea
= savedtapea
;
587 lastspclrec
= savedtapea
- 1;
589 size
= (char *)ntb
- (char *)q
;
590 if (atomic_write( slp
->fd
, (char *)q
, size
) != size
) {
591 perror(" DUMP: error writing command pipe");
596 lastfirstrec
= slp
->firstrec
;
598 if (++slp
>= &slaves
[SLAVES
])
603 if (prev
->dblk
!= 0) {
605 * If the last one was a disk block, make the
606 * first of this one be the last bit of that disk
609 q
->dblk
= prev
->dblk
+
610 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
611 ntb
= (union u_spcl
*)tslp
->tblock
;
614 * It wasn't a disk block. Copy the data to its
615 * new location in the buffer.
618 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
619 ntb
= (union u_spcl
*)tslp
->tblock
[1];
623 nextblock
= slp
->tblock
;
626 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
627 *(union u_spcl
*)(*nextblock
) = *(union u_spcl
*)tslp
->tblock
;
634 * Clear the first slaves' response. One hopes that it
635 * worked ok, otherwise the tape is much too short!
638 if (atomic_read( slp
->fd
, (char *)&got
, sizeof got
)
640 perror(" DUMP: error reading command pipe in master");
645 if (got
!= writesize
) {
646 quit("EOT detected at start of the tape!\n");
652 if (spcl
.c_type
!= TS_END
) {
653 for (i
= 0; i
< spcl
.c_count
; i
++)
654 if (spcl
.c_addr
[i
] != 0)
658 slp
->firstrec
= lastfirstrec
+ ntrec
;
659 slp
->count
= lastspclrec
+ blks
+ 1 - spcl
.c_tapea
;
662 blockswritten
+= ntrec
;
663 blocksthisvol
+= ntrec
;
668 * We implement taking and restoring checkpoints on the tape level.
669 * When each tape is opened, a new process is created by forking; this
670 * saves all of the necessary context in the parent. The child
671 * continues the dump; the parent waits around, saving the context.
672 * If the child returns X_REWRITE, then it had problems writing that tape;
673 * this causes the parent to fork again, duplicating the context, and
674 * everything continues as if nothing had happened.
677 startnewtape(int top
)
688 sigaddset(&sigs
, SIGINT
);
689 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
690 #else /* __linux__ */
692 void (*interrupt_save
)();
694 sig_t interrupt_save
;
696 interrupt_save
= signal(SIGINT
, SIG_IGN
);
697 #endif /* __linux__ */
699 parentpid
= getpid();
700 tapea_volume
= spcl
.c_tapea
;
702 (void)time4(&tstart_volume
);
704 (void)time((&tstart_volume
);
709 sigprocmask(SIG_UNBLOCK
, &sigs
, NULL
);
711 (void)signal(SIGINT
, interrupt_save
);
714 * All signals are inherited...
718 msg("Context save fork fails in parent %d\n", parentpid
);
724 * save the context by waiting
725 * until the child doing all of the work returns.
726 * don't catch the interrupt
729 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
731 signal(SIGINT
, SIG_IGN
);
734 msg("Tape: %d; parent process: %d child process %d\n",
735 tapeno
+1, parentpid
, childpid
);
737 while ((waitpid
= wait(&status
)) != childpid
)
738 if (waitpid
!= rshpid
)
739 msg("Parent %d waiting for child %d has another child %d return\n",
740 parentpid
, childpid
, waitpid
);
742 msg("Child %d returns LOB status %o\n",
743 childpid
, status
&0xFF);
745 status
= (status
>> 8) & 0xFF;
749 msg("Child %d finishes X_FINOK\n", childpid
);
752 msg("Child %d finishes X_ABORT\n", childpid
);
755 msg("Child %d finishes X_REWRITE\n", childpid
);
758 msg("Child %d finishes unknown %d\n",
769 goto restore_check_point
;
771 msg("Bad return code from dump: %d\n", status
);
775 } else { /* we are the child; just continue */
777 sleep(4); /* allow time for parent's message to get out */
778 msg("Child on Tape %d has parent %d, my pid = %d\n",
779 tapeno
+1, parentpid
, getpid());
782 * If we have a name like "/dev/rmt0,/dev/rmt1",
783 * use the name before the comma first, and save
784 * the remaining names for subsequent volumes.
786 tapeno
++; /* current tape sequence */
788 snprintf(tape
, MAXPATHLEN
, "%s%03d", tapeprefix
, tapeno
);
789 tape
[MAXPATHLEN
- 1] = '\0';
790 msg("Dumping volume %d on %s\n", tapeno
, tape
);
792 else if (nexttape
|| strchr(tapeprefix
, ',')) {
793 if (nexttape
&& *nexttape
)
794 tapeprefix
= nexttape
;
795 if ((p
= strchr(tapeprefix
, ',')) != NULL
) {
800 strncpy(tape
, tapeprefix
, MAXPATHLEN
);
801 tape
[MAXPATHLEN
- 1] = '\0';
802 msg("Dumping volume %d on %s\n", tapeno
, tape
);
805 while ((tapefd
= (host
? rmtopen(tape
, 2) : pipeout
?
807 open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
809 while ((tapefd
= (pipeout
? fileno(stdout
) :
810 open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
813 msg("Cannot open output \"%s\".\n", tape
);
814 if (!query("Do you want to retry the open?"))
818 enslave(); /* Share open tape file descriptor with slaves */
823 newtape
++; /* new tape signal */
824 spcl
.c_count
= slp
->count
;
826 * measure firstrec in TP_BSIZE units since restore doesn't
827 * know the correct ntrec value...
829 spcl
.c_firstrec
= slp
->firstrec
;
831 spcl
.c_type
= TS_TAPE
;
832 spcl
.c_flags
|= DR_NEWHEADER
;
833 writeheader((ino_t
)slp
->inode
);
834 spcl
.c_flags
&=~ DR_NEWHEADER
;
835 msg("Volume %d started at: %s", tapeno
,
837 ctime4(&tstart_volume
));
839 ctime(&tstart_volume
));
842 msg("Volume %d begins with blocks from inode %d\n",
851 if (master
!= 0 && master
!= getpid())
852 /* Signals master to call dumpabort */
853 (void) kill(master
, SIGTERM
);
856 msg("The ENTIRE dump is aborted.\n");
869 msg("pid = %d exits with status %d\n", getpid(), status
);
875 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
881 siglongjmp(jmpbuf
, 1);
889 #ifdef LINUX_FORK_BUG
897 { struct sigaction sa
;
898 sa
.sa_sigaction
= NULL
;
899 sigemptyset(&sa
.sa_mask
);
901 sa
.sa_handler
= dumpabort
;
902 sigaction(SIGTERM
, &sa
, NULL
); /* Slave sends SIGTERM on dumpabort() */
903 sa
.sa_handler
= sigpipe
;
904 sigaction(SIGPIPE
, &sa
, NULL
);
905 sa
.sa_handler
= tperror
;
906 sigaction(SIGUSR1
, &sa
, NULL
); /* Slave sends SIGUSR1 on tape errors */
907 sa
.sa_handler
= proceed
;
908 sa
.sa_flags
= SA_RESTART
;
909 sigaction(SIGUSR2
, &sa
, NULL
); /* Slave sends SIGUSR2 to next slave */
912 for (i
= 0; i
< SLAVES
; i
++) {
913 if (i
== slp
- &slaves
[0]) {
919 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, cmd
) < 0 ||
920 (slaves
[i
].pid
= fork()) < 0)
921 quit("too many slaves, %d (recompile smaller): %s\n",
924 slaves
[i
].fd
= cmd
[1];
926 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
928 for (j
= 0; j
<= i
; j
++)
929 (void) close(slaves
[j
].fd
);
931 sigaddset(&sigs
, SIGINT
); /* Master handles this */
933 sigaddset(&sigs
, SIGINFO
);
935 sigprocmask(SIG_BLOCK
, &sigs
, NULL
);
937 #ifdef LINUX_FORK_BUG
938 if (atomic_write( cmd
[0], (char *) &i
, sizeof i
)
940 quit("master/slave protocol botched 3\n");
947 #ifdef LINUX_FORK_BUG
949 * Wait for all slaves to _actually_ start to circumvent a bug in
950 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
951 * returned from fork() causes a SEGV in the child process
953 for (i
= 0; i
< SLAVES
; i
++)
954 if (atomic_read( slaves
[i
].fd
, (char *) &j
, sizeof j
) != sizeof j
)
955 quit("master/slave protocol botched 4\n");
958 for (i
= 0; i
< SLAVES
; i
++)
959 (void) atomic_write( slaves
[i
].fd
,
960 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
961 sizeof slaves
[0].pid
);
971 for (i
= 0; i
< SLAVES
; i
++)
972 if (slaves
[i
].pid
> 0) {
973 (void) kill(slaves
[i
].pid
, SIGKILL
);
979 * Synchronization - each process has a lockfile, and shares file
980 * descriptors to the following process's lockfile. When our write
981 * completes, we release our lock on the following process's lock-
982 * file, allowing the following process to lock it and proceed. We
983 * get the lock back for the next cycle by swapping descriptors.
986 doslave(int cmd
, int slave_number
)
989 int nextslave
, size
, eot_count
;
990 volatile int wrote
= 0;
997 * Need our own seek pointer.
999 (void) close(diskfd
);
1000 if ((diskfd
= open(disk
, O_RDONLY
)) < 0)
1001 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
1004 retval
= dump_fs_open(disk
, &fs
);
1006 quit("slave couldn't reopen disk: %s\n", error_message(retval
));
1007 #endif /* __linux__ */
1010 * Need the pid of the next slave in the loop...
1012 if ((nread
= atomic_read( cmd
, (char *)&nextslave
, sizeof nextslave
))
1013 != sizeof nextslave
) {
1014 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1018 * Get list of blocks to dump, read the blocks into tape buffer
1020 while ((nread
= atomic_read( cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
1021 register struct req
*p
= slp
->req
;
1023 for (trecno
= 0; trecno
< ntrec
;
1024 trecno
+= p
->count
, p
+= p
->count
) {
1026 bread(p
->dblk
, slp
->tblock
[trecno
],
1027 p
->count
* TP_BSIZE
);
1029 if (p
->count
!= 1 || atomic_read( cmd
,
1030 (char *)slp
->tblock
[trecno
],
1031 TP_BSIZE
) != TP_BSIZE
)
1032 quit("master/slave protocol botched.\n");
1035 if (sigsetjmp(jmpbuf
, 1) == 0) {
1043 /* Try to write the data... */
1048 while (eot_count
< 10 && size
< writesize
) {
1051 wrote
= rmtwrite(slp
->tblock
[0]+size
,
1055 wrote
= write(tapefd
, slp
->tblock
[0]+size
,
1058 printf("slave %d wrote %d\n", slave_number
, wrote
);
1068 if (size
!= writesize
)
1069 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1070 slave_number
, size
, writesize
);
1074 * Handle ENOSPC as an EOT condition.
1076 if (wrote
< 0 && errno
== ENOSPC
) {
1085 (void) kill(master
, SIGUSR1
);
1086 sigemptyset(&sigset
);
1087 sigaddset(&sigset
, SIGINT
);
1089 sigsuspend(&sigset
);
1092 * pass size of write back to master
1093 * (for EOT handling)
1095 (void) atomic_write( cmd
, (char *)&size
, sizeof size
);
1099 * If partial write, don't want next slave to go.
1100 * Also jolts him awake.
1102 (void) kill(nextslave
, SIGUSR2
);
1105 quit("error reading command pipe: %s\n", strerror(errno
));
1109 * Since a read from a pipe may not return all we asked for,
1110 * or a write may not write all we ask if we get a signal,
1111 * loop until the count is satisfied (or error).
1114 atomic_read(int fd
, void *buf
, size_t count
)
1116 int got
, need
= count
;
1119 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1121 } while (got
== -1 && errno
== EINTR
);
1122 return (got
< 0 ? got
: count
- need
);
1126 * Since a read from a pipe may not return all we asked for,
1127 * or a write may not write all we ask if we get a signal,
1128 * loop until the count is satisfied (or error).
1131 atomic_write(int fd
, const void *buf
, size_t count
)
1133 int got
, need
= count
;
1136 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
1138 } while (got
== -1 && errno
== EINTR
);
1139 return (got
< 0 ? got
: count
- need
);