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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char rcsid[] =
44 "$Id: tape.c,v 1.42 2001/04/12 13:14:15 stelian Exp $";
53 #include <compaterr.h>
63 #include <sys/types.h>
65 #include <linux/types.h>
67 #include <sys/param.h>
68 #include <sys/socket.h>
73 #include <linux/ext2_fs.h>
74 #include <ext2fs/ext2fs.h>
75 #include <bsdcompat.h>
77 #include <sys/vnode.h>
80 #include <ufs/inode.h>
82 #include <ufs/ufs/dinode.h>
83 #include <ufs/ffs/fs.h>
84 #endif /* __linux__ */
86 #include <protocols/dumprestore.h>
90 #endif /* HAVE_ZLIB */
94 int writesize; /* size of malloc()ed buffer for tape */
95 long lastspclrec = -1; /* tape block number of last written header */
96 int trecno = 0; /* next record to write in current block */
97 extern long blocksperfile; /* number of blocks per output file */
98 long blocksthisvol; /* number of blocks on current output file */
99 extern int ntrec; /* blocking factor on tape */
100 extern int cartridge;
105 long long tapea_bytes = 0; /* bytes_written at start of current volume */
107 static ssize_t atomic_read __P((int, void *, size_t));
108 static ssize_t atomic_write __P((int, const void *, size_t));
109 static void doslave __P((int, int));
110 static void enslave __P((void));
111 static void flushtape __P((void));
112 static void killall __P((void));
113 static void rollforward __P((void));
114 static int system_command __P((const char *, const char *, int));
117 * Concurrent dump mods (Caltech) - disk block reading and tape writing
118 * are exported to several slave processes. While one slave writes the
119 * tape, the others read disk blocks; they pass control of the tape in
120 * a ring via signals. The parent process traverses the filesystem and
121 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
122 * The following structure defines the instruction packets sent to slaves.
130 struct slave_results {
131 ssize_t unclen; /* uncompressed length */
132 ssize_t clen; /* compressed length */
135 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
137 int tapea; /* header number at start of this chunk */
138 int count; /* count to next header (used for TS_TAPE */
140 int inode; /* inode that we are currently dealing with */
141 int fd; /* FD for this slave */
142 int pid; /* PID for this slave */
143 int sent; /* 1 == we've sent this slave requests */
144 int firstrec; /* record number of this block */
145 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
146 struct req *req; /* buffer for requests */
150 char (*nextblock)[TP_BSIZE];
152 static time_t tstart_volume; /* time of volume start */
153 static int tapea_volume; /* value of spcl.c_tapea at volume start */
155 int master; /* pid of master, for sending error signals */
156 int tenths; /* length of tape overhead per block written */
157 static int caught; /* have we caught the signal to proceed? */
158 static int ready; /* have we reached the lock point without having */
159 /* received the SIGUSR2 signal from the prev slave? */
160 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
161 /* SIGUSR2 arrives from the previous slave */
163 static int gtperr = 0;
169 int pgoff = getpagesize() - 1;
173 writesize = ntrec * TP_BSIZE;
174 reqsiz = (ntrec + 1) * sizeof(struct req);
176 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
177 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
178 * repositioning after stopping, i.e, streaming mode, where the gap is
179 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
181 if (blocksperfile == 0 && !unlimited)
182 tenths = (cartridge ? 16 : density == 625 ? 5 : 8);
188 * Allocate tape buffer contiguous with the array of instruction
189 * packets, so flushtape() can write them together with one write().
190 * Align tape buffer on page boundary to speed up tape write().
192 for (i = 0; i <= SLAVES; i++) {
194 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
197 slaves[i].tblock = (char (*)[TP_BSIZE])
199 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
201 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
203 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
209 nextblock = slp->tblock;
214 writerec(const void *dp, int isspcl)
217 slp->req[trecno].dblk = (daddr_t)0;
218 slp->req[trecno].count = 1;
219 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
220 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
223 lastspclrec = spcl.c_tapea;
231 dumpblock(daddr_t blkno, int size)
233 int avail, tpblks, dblkno;
235 dblkno = fsbtodb(sblock, blkno);
236 tpblks = size >> tp_bshift;
237 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
238 slp->req[trecno].dblk = dblkno;
239 slp->req[trecno].count = avail;
241 spcl.c_tapea += avail;
244 dblkno += avail << (tp_bshift - dev_bshift);
256 msg("write error on %s: %s\n", tape, strerror(errnum));
257 quit("Cannot recover\n");
260 msg("write error %d blocks into volume %d: %s\n",
261 blocksthisvol, tapeno, strerror(errnum));
262 broadcast("DUMP WRITE ERROR!\n");
263 if (query("Do you want to rewrite this volume?")) {
264 msg("Closing this volume. Prepare to restart with new media;\n");
265 msg("this dump volume will be rewritten.\n");
271 if (query("Do you want to start the next tape?"))
280 quit("Broken pipe\n");
285 * Update xferrate stats
294 ttaken = tnow - tstart_volume;
295 blocks = spcl.c_tapea - tapea_volume;
296 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
298 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno,
299 blocks, ((double)blocks * TP_BSIZE / 1048576));
301 long volkb = (bytes_written - tapea_bytes) / 1024;
302 long txfrate = volkb / ttaken;
303 msg("Volume %d took %d:%02d:%02d\n", tapeno,
304 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
305 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
309 double rate = .0005 + (double) blocks / (double) volkb;
310 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
312 tapeno, blocks, volkb, rate);
319 mktimeest(time_t tnow)
321 static char msgbuf[128];
326 if (blockswritten < 500)
328 if (blockswritten > tapesize)
329 tapesize = blockswritten;
330 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
331 / blockswritten * tapesize;
332 if (tnow > tstart_volume)
333 (void)snprintf(msgbuf, sizeof(msgbuf),
334 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
335 (blockswritten * 100.0) / tapesize,
336 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
337 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
339 (void)snprintf(msgbuf, sizeof(msgbuf),
340 "%3.2f%% done, finished in %d:%02d\n",
341 (blockswritten * 100.0) / tapesize,
342 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
350 * information message upon receipt of SIGINFO
353 statussig(int notused)
355 int save_errno = errno;
358 buf = mktimeest(time(NULL));
360 write(STDERR_FILENO, buf, strlen(buf));
370 struct slave_results returned;
372 int siz = (char *)nextblock - (char *)slp->req;
374 slp->req[trecno].count = 0; /* Sentinel */
376 if (atomic_write( slp->fd, (char *)slp->req, siz) != siz)
377 quit("error writing command pipe: %s\n", strerror(errno));
378 slp->sent = 1; /* we sent a request, read the response later */
380 lastfirstrec = slp->firstrec;
382 if (++slp >= &slaves[SLAVES])
385 /* Read results back from next slave */
387 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
388 != sizeof returned) {
389 perror(" DUMP: error reading command pipe in master");
392 got = returned.unclen;
393 bytes_written += returned.clen;
394 if (returned.unclen == returned.clen)
398 /* Check for errors or end of tape */
400 /* Check for errors */
404 msg("End of tape detected\n");
407 * Drain the results, don't care what the values were.
408 * If we read them here then trewind won't...
410 for (i = 0; i < SLAVES; i++) {
411 if (slaves[i].sent) {
412 if (atomic_read( slaves[i].fd,
413 (char *)&returned, sizeof returned)
414 != sizeof returned) {
415 perror(" DUMP: error reading command pipe in master");
429 if (spcl.c_type != TS_END) {
430 for (i = 0; i < spcl.c_count; i++)
431 if (spcl.c_addr[i] != 0)
434 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
435 slp->tapea = spcl.c_tapea;
436 slp->firstrec = lastfirstrec + ntrec;
438 nextblock = slp->tblock;
440 asize += tenths + returned.clen / density;
441 blockswritten += ntrec;
442 blocksthisvol += ntrec;
443 if (!pipeout && !unlimited && (blocksperfile ?
444 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
452 * Executes the command in a shell.
453 * Returns -1 if an error occured, the exit status of
454 * the command on success.
456 int system_command(const char *command, const char *device, int volnum) {
458 char commandstr[4096];
462 perror(" DUMP: unable to fork");
468 #if OLD_STYLE_FSCRIPT
469 snprintf(commandstr, sizeof(commandstr), "%s", command);
471 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
473 commandstr[sizeof(commandstr) - 1] = '\0';
474 execl("/bin/sh", "sh", "-c", commandstr, NULL);
475 perror(" DUMP: unable to execute shell");
479 if (waitpid(pid, &status, 0) == -1) {
480 if (errno != EINTR) {
481 perror(" DUMP: waitpid error");
485 if (WIFEXITED(status))
486 return WEXITSTATUS(status);
498 struct slave_results returned;
500 for (f = 0; f < SLAVES; f++) {
502 * Drain the results, but unlike EOT we DO (or should) care
503 * what the return values were, since if we detect EOT after
504 * we think we've written the last blocks to the tape anyway,
505 * we have to replay those blocks with rollforward.
507 * fixme: punt for now.
509 if (slaves[f].sent) {
510 if (atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
511 != sizeof returned) {
512 perror(" DUMP: error reading command pipe in master");
515 got = returned.unclen;
516 bytes_written += returned.clen;
517 if (returned.unclen == returned.clen)
525 msg("EOT detected in last 2 tape records!\n");
526 msg("Use a longer tape, decrease the size estimate\n");
527 quit("or use no size estimate at all.\n");
530 (void) close(slaves[f].fd);
532 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
537 msg("Closing %s\n", tape);
542 while (rmtopen(tape, 0) < 0)
549 (void) close(tapefd);
550 while ((f = open(tape, 0)) < 0)
555 if (eot_script && spcl.c_type != TS_END) {
556 msg("Launching %s\n", eot_script);
557 eot_code = system_command(eot_script, tape, tapeno);
559 if (eot_code != 0 && eot_code != 1) {
560 msg("Dump aborted by the end of tape script\n");
572 if (nexttape || Mflag || (eot_code == 0) )
575 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
576 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
578 while (!query("Is the new volume mounted and ready to go?"))
579 if (query("Do you want to abort?")) {
588 register struct req *p, *q, *prev;
589 register struct slave *tslp;
590 int i, size, savedtapea, got;
591 union u_spcl *ntb, *otb;
592 struct slave_results returned;
597 tslp = &slaves[SLAVES];
598 ntb = (union u_spcl *)tslp->tblock[1];
601 * Each of the N slaves should have requests that need to
602 * be replayed on the next tape. Use the extra slave buffers
603 * (slaves[SLAVES]) to construct request lists to be sent to
604 * each slave in turn.
606 for (i = 0; i < SLAVES; i++) {
608 otb = (union u_spcl *)slp->tblock;
611 * For each request in the current slave, copy it to tslp.
615 for (p = slp->req; p->count > 0; p += p->count) {
618 *ntb++ = *otb++; /* copy the datablock also */
623 quit("rollforward: protocol botch");
635 nextblock = tslp->tblock;
636 savedtapea = spcl.c_tapea;
637 spcl.c_tapea = slp->tapea;
639 spcl.c_tapea = savedtapea;
640 lastspclrec = savedtapea - 1;
642 size = (char *)ntb - (char *)q;
643 if (atomic_write( slp->fd, (char *)q, size) != size) {
644 perror(" DUMP: error writing command pipe");
649 lastfirstrec = slp->firstrec;
651 if (++slp >= &slaves[SLAVES])
656 if (prev->dblk != 0) {
658 * If the last one was a disk block, make the
659 * first of this one be the last bit of that disk
662 q->dblk = prev->dblk +
663 prev->count * (TP_BSIZE / DEV_BSIZE);
664 ntb = (union u_spcl *)tslp->tblock;
667 * It wasn't a disk block. Copy the data to its
668 * new location in the buffer.
671 *((union u_spcl *)tslp->tblock) = *ntb;
672 ntb = (union u_spcl *)tslp->tblock[1];
676 nextblock = slp->tblock;
679 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
680 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
687 * Clear the first slaves' response. One hopes that it
688 * worked ok, otherwise the tape is much too short!
691 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
692 != sizeof returned) {
693 perror(" DUMP: error reading command pipe in master");
696 got = returned.unclen;
697 bytes_written += returned.clen;
698 if (returned.clen == returned.unclen)
706 quit("EOT detected at start of the tape!\n");
712 if (spcl.c_type != TS_END) {
713 for (i = 0; i < spcl.c_count; i++)
714 if (spcl.c_addr[i] != 0)
718 slp->firstrec = lastfirstrec + ntrec;
719 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
721 asize += tenths + returned.clen / density;
722 blockswritten += ntrec;
723 blocksthisvol += ntrec;
728 * We implement taking and restoring checkpoints on the tape level.
729 * When each tape is opened, a new process is created by forking; this
730 * saves all of the necessary context in the parent. The child
731 * continues the dump; the parent waits around, saving the context.
732 * If the child returns X_REWRITE, then it had problems writing that tape;
733 * this causes the parent to fork again, duplicating the context, and
734 * everything continues as if nothing had happened.
737 startnewtape(int top)
748 sigaddset(&sigs, SIGINT);
749 sigprocmask(SIG_BLOCK, &sigs, NULL);
750 #else /* __linux__ */
752 void (*interrupt_save)();
754 sig_t interrupt_save;
756 interrupt_save = signal(SIGINT, SIG_IGN);
757 #endif /* __linux__ */
759 parentpid = getpid();
760 tapea_volume = spcl.c_tapea;
761 tapea_bytes = bytes_written;
762 tstart_volume = time(NULL);
766 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
768 (void)signal(SIGINT, interrupt_save);
771 * All signals are inherited...
775 msg("Context save fork fails in parent %d\n", parentpid);
781 * save the context by waiting
782 * until the child doing all of the work returns.
783 * don't catch the interrupt
786 sigprocmask(SIG_BLOCK, &sigs, NULL);
788 signal(SIGINT, SIG_IGN);
791 msg("Tape: %d; parent process: %d child process %d\n",
792 tapeno+1, parentpid, childpid);
794 while ((waitpid = wait(&status)) != childpid)
795 if (waitpid != rshpid)
796 msg("Parent %d waiting for child %d has another child %d return\n",
797 parentpid, childpid, waitpid);
799 msg("Child %d returns LOB status %o\n",
800 childpid, status&0xFF);
802 status = (status >> 8) & 0xFF;
806 msg("Child %d finishes X_FINOK\n", childpid);
809 msg("Child %d finishes X_ABORT\n", childpid);
812 msg("Child %d finishes X_REWRITE\n", childpid);
815 msg("Child %d finishes unknown %d\n",
826 goto restore_check_point;
828 msg("Bad return code from dump: %d\n", status);
832 } else { /* we are the child; just continue */
834 sleep(4); /* allow time for parent's message to get out */
835 msg("Child on Tape %d has parent %d, my pid = %d\n",
836 tapeno+1, parentpid, getpid());
839 * If we have a name like "/dev/rmt0,/dev/rmt1",
840 * use the name before the comma first, and save
841 * the remaining names for subsequent volumes.
843 tapeno++; /* current tape sequence */
845 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
846 tape[MAXPATHLEN - 1] = '\0';
847 msg("Dumping volume %d on %s\n", tapeno, tape);
849 else if (nexttape || strchr(tapeprefix, ',')) {
850 if (nexttape && *nexttape)
851 tapeprefix = nexttape;
852 if ((p = strchr(tapeprefix, ',')) != NULL) {
857 strncpy(tape, tapeprefix, MAXPATHLEN);
858 tape[MAXPATHLEN - 1] = '\0';
859 msg("Dumping volume %d on %s\n", tapeno, tape);
862 while ((tapefd = (host ? rmtopen(tape, 2) : pipeout ?
864 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
866 while ((tapefd = (pipeout ? fileno(stdout) :
867 open(tape, O_RDWR|O_CREAT, 0666))) < 0)
870 msg("Cannot open output \"%s\".\n", tape);
871 if (!query("Do you want to retry the open?"))
875 enslave(); /* Share open tape file descriptor with slaves */
880 newtape++; /* new tape signal */
881 spcl.c_count = slp->count;
883 * measure firstrec in TP_BSIZE units since restore doesn't
884 * know the correct ntrec value...
886 spcl.c_firstrec = slp->firstrec;
888 spcl.c_type = TS_TAPE;
889 spcl.c_flags |= DR_NEWHEADER;
891 spcl.c_flags |= DR_COMPRESSED;
892 writeheader((dump_ino_t)slp->inode);
893 spcl.c_flags &=~ DR_NEWHEADER;
894 msg("Volume %d started with block %ld at: %s", tapeno,
895 spcl.c_tapea, ctime(&tstart_volume));
897 msg("Volume %d begins with blocks from inode %d\n",
906 if (master != 0 && master != getpid())
907 /* Signals master to call dumpabort */
908 (void) kill(master, SIGTERM);
911 msg("The ENTIRE dump is aborted.\n");
924 msg("pid = %d exits with status %d\n", getpid(), status);
930 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
936 siglongjmp(jmpbuf, 1);
944 #ifdef LINUX_FORK_BUG
952 { struct sigaction sa;
953 memset(&sa, 0, sizeof sa);
954 sigemptyset(&sa.sa_mask);
955 sa.sa_handler = dumpabort;
956 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
957 sa.sa_handler = sigpipe;
958 sigaction(SIGPIPE, &sa, NULL);
959 sa.sa_handler = proceed;
960 sa.sa_flags = SA_RESTART;
961 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
964 for (i = 0; i < SLAVES; i++) {
965 if (i == slp - &slaves[0]) {
971 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
972 (slaves[i].pid = fork()) < 0)
973 quit("too many slaves, %d (recompile smaller): %s\n",
976 slaves[i].fd = cmd[1];
978 if (slaves[i].pid == 0) { /* Slave starts up here */
980 for (j = 0; j <= i; j++)
981 (void) close(slaves[j].fd);
983 sigaddset(&sigs, SIGINT); /* Master handles this */
985 sigaddset(&sigs, SIGINFO);
987 sigprocmask(SIG_BLOCK, &sigs, NULL);
989 #ifdef LINUX_FORK_BUG
990 if (atomic_write( cmd[0], (char *) &i, sizeof i)
992 quit("master/slave protocol botched 3\n");
1001 #ifdef LINUX_FORK_BUG
1003 * Wait for all slaves to _actually_ start to circumvent a bug in
1004 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1005 * returned from fork() causes a SEGV in the child process
1007 for (i = 0; i < SLAVES; i++)
1008 if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
1009 quit("master/slave protocol botched 4\n");
1012 for (i = 0; i < SLAVES; i++)
1013 (void) atomic_write( slaves[i].fd,
1014 (char *) &slaves[(i + 1) % SLAVES].pid,
1015 sizeof slaves[0].pid);
1025 for (i = 0; i < SLAVES; i++)
1026 if (slaves[i].pid > 0) {
1027 (void) kill(slaves[i].pid, SIGKILL);
1033 * Synchronization - each process waits for a SIGUSR2 from the
1034 * previous process before writing to the tape, and sends SIGUSR2
1035 * to the next process when the tape write completes. On tape errors
1036 * a SIGUSR1 is sent to the master which then terminates all of the
1040 doslave(int cmd, int slave_number)
1043 int nextslave, size, eot_count, bufsize;
1044 volatile int wrote = 0;
1047 struct tapebuf *comp_buf = NULL;
1048 int compresult, do_compress = 0;
1049 unsigned long worklen;
1050 #endif /* HAVE_ZLIB */
1051 struct slave_results returns;
1057 union u_spcl *uspclptr;
1058 struct s_spcl *spclptr;
1059 #endif /* USA_QFA */
1062 * Need our own seek pointer.
1064 (void) close(diskfd);
1065 if ((diskfd = open(disk, O_RDONLY)) < 0)
1066 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1069 retval = dump_fs_open(disk, &fs);
1071 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1072 #endif /* __linux__ */
1075 * Need the pid of the next slave in the loop...
1077 if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1078 != sizeof nextslave) {
1079 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1083 /* if we're doing a compressed dump, allocate the compress buffer */
1085 comp_buf = malloc(sizeof(struct tapebuf) + TP_BSIZE + writesize);
1086 if (comp_buf == NULL)
1087 quit("couldn't allocate a compress buffer.\n");
1088 comp_buf->flags = 0;
1090 #endif /* HAVE_ZLIB */
1093 * Get list of blocks to dump, read the blocks into tape buffer
1095 while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1096 register struct req *p = slp->req;
1098 for (trecno = 0; trecno < ntrec;
1099 trecno += p->count, p += p->count) {
1100 if (p->dblk) { /* read a disk block */
1101 bread(p->dblk, slp->tblock[trecno],
1102 p->count * TP_BSIZE);
1103 } else { /* read record from pipe */
1104 if (p->count != 1 || atomic_read( cmd,
1105 (char *)slp->tblock[trecno],
1106 TP_BSIZE) != TP_BSIZE)
1107 quit("master/slave protocol botched.\n");
1112 if (gTapeposfd >= 0) {
1113 uspclptr = (union u_spcl *)&slp->tblock[0];
1114 spclptr = &uspclptr->s_spcl;
1115 if ((spclptr->c_magic == NFS_MAGIC) &&
1116 (spclptr->c_type == TS_INODE)) {
1117 /* if an error occured previously don't
1120 if ((gtperr = GetTapePos(&curtapepos)) == 0) {
1122 msg("inode %ld at tapepos %ld\n", spclptr->c_inumber, curtapepos);
1124 sprintf(gTps, "%ld\t%d\t%ld\n", (unsigned long)spclptr->c_inumber, tapeno, curtapepos);
1125 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps)) {
1126 quit("error writing tapepos file.\n");
1132 #endif /* USE_QFA */
1134 /* Try to write the data... */
1138 buffer = (char *) slp->tblock[0]; /* set write pointer */
1139 bufsize = writesize; /* length to write */
1140 returns.clen = returns.unclen = bufsize;
1144 * When writing a compressed dump, each block is
1145 * written from struct tapebuf with an 4 byte prefix
1146 * followed by the data. This can be less than
1147 * writesize. Restore, on a short read, can compare the
1148 * length read to the compressed length in the header
1149 * to verify that the read was good. Blocks which don't
1150 * compress well are written uncompressed.
1151 * The first block written by each slave is not compressed.
1155 comp_buf->length = bufsize;
1156 worklen = TP_BSIZE + writesize;
1158 compresult = compress2(comp_buf->buf, &worklen,
1159 (char *)slp->tblock[0], writesize, compressed);
1160 if (compresult == Z_OK && worklen <= writesize-32) {
1161 /* write the compressed buffer */
1162 comp_buf->length = worklen;
1163 comp_buf->compressed = 1;
1164 buffer = (char *) comp_buf;
1165 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1168 /* write the data uncompressed */
1169 comp_buf->length = writesize;
1170 comp_buf->compressed = 0;
1171 buffer = (char *) comp_buf;
1172 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1173 returns.unclen = returns.clen;
1174 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1177 /* compress the remaining blocks */
1178 do_compress = compressed;
1179 #endif /* HAVE_ZLIB */
1181 if (sigsetjmp(jmpbuf, 1) == 0) {
1189 while (eot_count < 10 && size < bufsize) {
1192 wrote = rmtwrite(buffer + size, bufsize - size);
1195 wrote = write(tapefd, buffer + size, bufsize - size);
1197 printf("slave %d wrote %d\n", slave_number, wrote);
1207 if (size != bufsize)
1208 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1209 slave_number, size, bufsize);
1213 * Handle ENOSPC as an EOT condition.
1215 if (wrote < 0 && errno == ENOSPC) {
1221 returns.clen = returns.unclen = 0;
1224 * pass errno back to master for special handling
1227 returns.unclen = -errno;
1230 * pass size of data and size of write back to master
1231 * (for EOT handling)
1233 (void) atomic_write( cmd, (char *)&returns, sizeof returns);
1236 * Signal the next slave to go.
1238 (void) kill(nextslave, SIGUSR2);
1241 quit("error reading command pipe: %s\n", strerror(errno));
1245 * Since a read from a pipe may not return all we asked for,
1246 * or a write may not write all we ask if we get a signal,
1247 * loop until the count is satisfied (or error).
1250 atomic_read(int fd, void *buf, size_t count)
1252 int got, need = count;
1255 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1257 } while (got == -1 && errno == EINTR);
1258 return (got < 0 ? got : count - need);
1262 * Since a read from a pipe may not return all we asked for,
1263 * or a write may not write all we ask if we get a signal,
1264 * loop until the count is satisfied (or error).
1267 atomic_write(int fd, const void *buf, size_t count)
1269 int got, need = count;
1272 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1274 } while (got == -1 && errno == EINTR);
1275 return (got < 0 ? got : count - need);
1281 * read the current tape position
1284 GetTapePos(long *pos)
1289 if (ioctl(tapefd, MTIOCPOS, pos) == -1) {
1291 msg("[%ld] error: %d (getting tapepos: %ld)\n", getpid(),
1297 #endif /* USE_QFA */