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.46 2001/05/12 11:36:12 stelian Exp $";
48 #include <compatlfs.h>
54 #include <compaterr.h>
64 #include <sys/types.h>
66 #include <linux/types.h>
68 #include <sys/param.h>
69 #include <sys/socket.h>
74 #include <linux/ext2_fs.h>
75 #include <ext2fs/ext2fs.h>
76 #include <bsdcompat.h>
78 #include <sys/vnode.h>
81 #include <ufs/inode.h>
83 #include <ufs/ufs/dinode.h>
84 #include <ufs/ffs/fs.h>
85 #endif /* __linux__ */
87 #include <protocols/dumprestore.h>
91 #endif /* HAVE_ZLIB */
95 int writesize; /* size of malloc()ed buffer for tape */
96 long lastspclrec = -1; /* tape block number of last written header */
97 int trecno = 0; /* next record to write in current block */
98 extern long blocksperfile; /* number of blocks per output file */
99 long blocksthisvol; /* number of blocks on current output file */
100 extern int ntrec; /* blocking factor on tape */
101 extern int cartridge;
106 long long tapea_bytes = 0; /* bytes_written at start of current volume */
108 static ssize_t atomic_read __P((int, void *, size_t));
109 static ssize_t atomic_write __P((int, const void *, size_t));
110 static void doslave __P((int, int));
111 static void enslave __P((void));
112 static void flushtape __P((void));
113 static void killall __P((void));
114 static void rollforward __P((void));
115 static int system_command __P((const char *, const char *, int));
118 * Concurrent dump mods (Caltech) - disk block reading and tape writing
119 * are exported to several slave processes. While one slave writes the
120 * tape, the others read disk blocks; they pass control of the tape in
121 * a ring via signals. The parent process traverses the filesystem and
122 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
123 * The following structure defines the instruction packets sent to slaves.
131 struct slave_results {
132 ssize_t unclen; /* uncompressed length */
133 ssize_t clen; /* compressed length */
136 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
138 int tapea; /* header number at start of this chunk */
139 int count; /* count to next header (used for TS_TAPE */
141 int inode; /* inode that we are currently dealing with */
142 int fd; /* FD for this slave */
143 int pid; /* PID for this slave */
144 int sent; /* 1 == we've sent this slave requests */
145 int firstrec; /* record number of this block */
146 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
147 struct req *req; /* buffer for requests */
151 char (*nextblock)[TP_BSIZE];
153 static time_t tstart_volume; /* time of volume start */
154 static int tapea_volume; /* value of spcl.c_tapea at volume start */
156 int master; /* pid of master, for sending error signals */
157 int tenths; /* length of tape overhead per block written */
158 static int caught; /* have we caught the signal to proceed? */
159 static int ready; /* have we reached the lock point without having */
160 /* received the SIGUSR2 signal from the prev slave? */
161 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
162 /* SIGUSR2 arrives from the previous slave */
164 static int gtperr = 0;
170 int pgoff = getpagesize() - 1;
174 writesize = ntrec * TP_BSIZE;
175 reqsiz = (ntrec + 1) * sizeof(struct req);
177 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
178 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
179 * repositioning after stopping, i.e, streaming mode, where the gap is
180 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
182 if (blocksperfile == 0 && !unlimited)
183 tenths = (cartridge ? 16 : density == 625 ? 5 : 8);
189 * Allocate tape buffer contiguous with the array of instruction
190 * packets, so flushtape() can write them together with one write().
191 * Align tape buffer on page boundary to speed up tape write().
193 for (i = 0; i <= SLAVES; i++) {
195 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
198 slaves[i].tblock = (char (*)[TP_BSIZE])
200 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
202 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
204 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
210 nextblock = slp->tblock;
215 writerec(const void *dp, int isspcl)
218 slp->req[trecno].dblk = (daddr_t)0;
219 slp->req[trecno].count = 1;
220 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
221 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
224 lastspclrec = spcl.c_tapea;
232 dumpblock(daddr_t blkno, int size)
234 int avail, tpblks, dblkno;
236 dblkno = fsbtodb(sblock, blkno);
237 tpblks = size >> tp_bshift;
238 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
239 slp->req[trecno].dblk = dblkno;
240 slp->req[trecno].count = avail;
242 spcl.c_tapea += avail;
245 dblkno += avail << (tp_bshift - dev_bshift);
257 msg("write error on %s: %s\n", tape, strerror(errnum));
258 quit("Cannot recover\n");
261 msg("write error %d blocks into volume %d: %s\n",
262 blocksthisvol, tapeno, strerror(errnum));
263 broadcast("DUMP WRITE ERROR!\n");
264 if (query("Do you want to rewrite this volume?")) {
265 msg("Closing this volume. Prepare to restart with new media;\n");
266 msg("this dump volume will be rewritten.\n");
272 if (query("Do you want to start the next tape?"))
281 quit("Broken pipe\n");
286 * Update xferrate stats
295 ttaken = tnow - tstart_volume;
296 blocks = spcl.c_tapea - tapea_volume;
297 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
299 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno,
300 blocks, ((double)blocks * TP_BSIZE / 1048576));
302 long volkb = (bytes_written - tapea_bytes) / 1024;
303 long txfrate = volkb / ttaken;
304 msg("Volume %d took %d:%02d:%02d\n", tapeno,
305 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
306 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
310 double rate = .0005 + (double) blocks / (double) volkb;
311 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
313 tapeno, blocks, volkb, rate);
320 mktimeest(time_t tnow)
322 static char msgbuf[128];
327 if (blockswritten < 500)
329 if (blockswritten > tapesize)
330 tapesize = blockswritten;
331 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
332 / blockswritten * tapesize;
333 if (tnow > tstart_volume)
334 (void)snprintf(msgbuf, sizeof(msgbuf),
335 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
336 (blockswritten * 100.0) / tapesize,
337 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
338 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
340 (void)snprintf(msgbuf, sizeof(msgbuf),
341 "%3.2f%% done, finished in %d:%02d\n",
342 (blockswritten * 100.0) / tapesize,
343 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
351 * information message upon receipt of SIGINFO
354 statussig(int notused)
356 int save_errno = errno;
359 buf = mktimeest(time(NULL));
361 write(STDERR_FILENO, buf, strlen(buf));
371 struct slave_results returned;
373 int siz = (char *)nextblock - (char *)slp->req;
375 slp->req[trecno].count = 0; /* Sentinel */
377 if (atomic_write( slp->fd, (char *)slp->req, siz) != siz)
378 quit("error writing command pipe: %s\n", strerror(errno));
379 slp->sent = 1; /* we sent a request, read the response later */
381 lastfirstrec = slp->firstrec;
383 if (++slp >= &slaves[SLAVES])
386 /* Read results back from next slave */
388 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
389 != sizeof returned) {
390 perror(" DUMP: error reading command pipe in master");
393 got = returned.unclen;
394 bytes_written += returned.clen;
395 if (returned.unclen == returned.clen)
399 /* Check for errors or end of tape */
401 /* Check for errors */
405 msg("End of tape detected\n");
408 * Drain the results, don't care what the values were.
409 * If we read them here then trewind won't...
411 for (i = 0; i < SLAVES; i++) {
412 if (slaves[i].sent) {
413 if (atomic_read( slaves[i].fd,
414 (char *)&returned, sizeof returned)
415 != sizeof returned) {
416 perror(" DUMP: error reading command pipe in master");
430 if (spcl.c_type != TS_END) {
431 for (i = 0; i < spcl.c_count; i++)
432 if (spcl.c_addr[i] != 0)
435 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
436 slp->tapea = spcl.c_tapea;
437 slp->firstrec = lastfirstrec + ntrec;
439 nextblock = slp->tblock;
441 asize += tenths + returned.clen / density;
442 blockswritten += ntrec;
443 blocksthisvol += ntrec;
444 if (!pipeout && !unlimited && (blocksperfile ?
445 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
453 * Executes the command in a shell.
454 * Returns -1 if an error occured, the exit status of
455 * the command on success.
457 int system_command(const char *command, const char *device, int volnum) {
459 char commandstr[4096];
463 perror(" DUMP: unable to fork");
469 #if OLD_STYLE_FSCRIPT
470 snprintf(commandstr, sizeof(commandstr), "%s", command);
472 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
474 commandstr[sizeof(commandstr) - 1] = '\0';
475 execl("/bin/sh", "sh", "-c", commandstr, NULL);
476 perror(" DUMP: unable to execute shell");
480 if (waitpid(pid, &status, 0) == -1) {
481 if (errno != EINTR) {
482 perror(" DUMP: waitpid error");
486 if (WIFEXITED(status))
487 return WEXITSTATUS(status);
499 struct slave_results returned;
501 for (f = 0; f < SLAVES; f++) {
503 * Drain the results, but unlike EOT we DO (or should) care
504 * what the return values were, since if we detect EOT after
505 * we think we've written the last blocks to the tape anyway,
506 * we have to replay those blocks with rollforward.
508 * fixme: punt for now.
510 if (slaves[f].sent) {
511 if (atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
512 != sizeof returned) {
513 perror(" DUMP: error reading command pipe in master");
516 got = returned.unclen;
517 bytes_written += returned.clen;
518 if (returned.unclen == returned.clen)
526 msg("EOT detected in last 2 tape records!\n");
527 msg("Use a longer tape, decrease the size estimate\n");
528 quit("or use no size estimate at all.\n");
531 (void) close(slaves[f].fd);
533 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
538 msg("Closing %s\n", tape);
543 while (rmtopen(tape, 0) < 0)
550 (void) close(tapefd);
551 while ((f = OPEN(tape, 0)) < 0)
556 if (eot_script && spcl.c_type != TS_END) {
557 msg("Launching %s\n", eot_script);
558 eot_code = system_command(eot_script, tape, tapeno);
560 if (eot_code != 0 && eot_code != 1) {
561 msg("Dump aborted by the end of tape script\n");
573 if (nexttape || Mflag || (eot_code == 0) )
576 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
577 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
579 while (!query("Is the new volume mounted and ready to go?"))
580 if (query("Do you want to abort?")) {
589 register struct req *p, *q, *prev;
590 register struct slave *tslp;
591 int i, size, savedtapea, got;
592 union u_spcl *ntb, *otb;
593 struct slave_results returned;
598 tslp = &slaves[SLAVES];
599 ntb = (union u_spcl *)tslp->tblock[1];
602 * Each of the N slaves should have requests that need to
603 * be replayed on the next tape. Use the extra slave buffers
604 * (slaves[SLAVES]) to construct request lists to be sent to
605 * each slave in turn.
607 for (i = 0; i < SLAVES; i++) {
609 otb = (union u_spcl *)slp->tblock;
612 * For each request in the current slave, copy it to tslp.
616 for (p = slp->req; p->count > 0; p += p->count) {
619 *ntb++ = *otb++; /* copy the datablock also */
624 quit("rollforward: protocol botch");
636 nextblock = tslp->tblock;
637 savedtapea = spcl.c_tapea;
638 spcl.c_tapea = slp->tapea;
640 spcl.c_tapea = savedtapea;
641 lastspclrec = savedtapea - 1;
643 size = (char *)ntb - (char *)q;
644 if (atomic_write( slp->fd, (char *)q, size) != size) {
645 perror(" DUMP: error writing command pipe");
650 lastfirstrec = slp->firstrec;
652 if (++slp >= &slaves[SLAVES])
657 if (prev->dblk != 0) {
659 * If the last one was a disk block, make the
660 * first of this one be the last bit of that disk
663 q->dblk = prev->dblk +
664 prev->count * (TP_BSIZE / DEV_BSIZE);
665 ntb = (union u_spcl *)tslp->tblock;
668 * It wasn't a disk block. Copy the data to its
669 * new location in the buffer.
672 *((union u_spcl *)tslp->tblock) = *ntb;
673 ntb = (union u_spcl *)tslp->tblock[1];
677 nextblock = slp->tblock;
680 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
681 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
688 * Clear the first slaves' response. One hopes that it
689 * worked ok, otherwise the tape is much too short!
692 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
693 != sizeof returned) {
694 perror(" DUMP: error reading command pipe in master");
697 got = returned.unclen;
698 bytes_written += returned.clen;
699 if (returned.clen == returned.unclen)
707 quit("EOT detected at start of the tape!\n");
713 if (spcl.c_type != TS_END) {
714 for (i = 0; i < spcl.c_count; i++)
715 if (spcl.c_addr[i] != 0)
719 slp->firstrec = lastfirstrec + ntrec;
720 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
722 asize += tenths + returned.clen / density;
723 blockswritten += ntrec;
724 blocksthisvol += ntrec;
729 * We implement taking and restoring checkpoints on the tape level.
730 * When each tape is opened, a new process is created by forking; this
731 * saves all of the necessary context in the parent. The child
732 * continues the dump; the parent waits around, saving the context.
733 * If the child returns X_REWRITE, then it had problems writing that tape;
734 * this causes the parent to fork again, duplicating the context, and
735 * everything continues as if nothing had happened.
738 startnewtape(int top)
749 sigaddset(&sigs, SIGINT);
750 sigprocmask(SIG_BLOCK, &sigs, NULL);
751 #else /* __linux__ */
753 void (*interrupt_save)();
755 sig_t interrupt_save;
757 interrupt_save = signal(SIGINT, SIG_IGN);
758 #endif /* __linux__ */
760 parentpid = getpid();
761 tapea_volume = spcl.c_tapea;
762 tapea_bytes = bytes_written;
763 tstart_volume = time(NULL);
767 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
769 (void)signal(SIGINT, interrupt_save);
772 * All signals are inherited...
776 msg("Context save fork fails in parent %d\n", parentpid);
782 * save the context by waiting
783 * until the child doing all of the work returns.
784 * don't catch the interrupt
787 sigprocmask(SIG_BLOCK, &sigs, NULL);
789 signal(SIGINT, SIG_IGN);
792 msg("Tape: %d; parent process: %d child process %d\n",
793 tapeno+1, parentpid, childpid);
795 while ((waitpid = wait(&status)) != childpid)
796 if (waitpid != rshpid)
797 msg("Parent %d waiting for child %d has another child %d return\n",
798 parentpid, childpid, waitpid);
800 msg("Child %d returns LOB status %o\n",
801 childpid, status&0xFF);
803 status = (status >> 8) & 0xFF;
807 msg("Child %d finishes X_FINOK\n", childpid);
810 msg("Child %d finishes X_ABORT\n", childpid);
813 msg("Child %d finishes X_REWRITE\n", childpid);
816 msg("Child %d finishes unknown %d\n",
827 goto restore_check_point;
829 msg("Bad return code from dump: %d\n", status);
833 } else { /* we are the child; just continue */
835 sleep(4); /* allow time for parent's message to get out */
836 msg("Child on Tape %d has parent %d, my pid = %d\n",
837 tapeno+1, parentpid, getpid());
840 * If we have a name like "/dev/rmt0,/dev/rmt1",
841 * use the name before the comma first, and save
842 * the remaining names for subsequent volumes.
844 tapeno++; /* current tape sequence */
846 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
847 tape[MAXPATHLEN - 1] = '\0';
848 msg("Dumping volume %d on %s\n", tapeno, tape);
850 else if (nexttape || strchr(tapeprefix, ',')) {
851 if (nexttape && *nexttape)
852 tapeprefix = nexttape;
853 if ((p = strchr(tapeprefix, ',')) != NULL) {
858 strncpy(tape, tapeprefix, MAXPATHLEN);
859 tape[MAXPATHLEN - 1] = '\0';
860 msg("Dumping volume %d on %s\n", tapeno, tape);
863 while ((tapefd = (host ? rmtopen(tape, 2) : pipeout ?
865 OPEN(tape, O_WRONLY|O_CREAT, 0666))) < 0)
867 while ((tapefd = (pipeout ? fileno(stdout) :
868 OPEN(tape, O_RDWR|O_CREAT, 0666))) < 0)
871 msg("Cannot open output \"%s\".\n", tape);
872 if (!query("Do you want to retry the open?"))
876 enslave(); /* Share open tape file descriptor with slaves */
881 newtape++; /* new tape signal */
882 spcl.c_count = slp->count;
884 * measure firstrec in TP_BSIZE units since restore doesn't
885 * know the correct ntrec value...
887 spcl.c_firstrec = slp->firstrec;
889 spcl.c_type = TS_TAPE;
890 spcl.c_flags |= DR_NEWHEADER;
891 spcl.c_ntrec = ntrec;
893 spcl.c_flags |= DR_COMPRESSED;
894 writeheader((dump_ino_t)slp->inode);
895 spcl.c_flags &=~ DR_NEWHEADER;
896 msg("Volume %d started with block %ld at: %s", tapeno,
897 spcl.c_tapea, ctime(&tstart_volume));
899 msg("Volume %d begins with blocks from inode %d\n",
908 if (master != 0 && master != getpid())
909 /* Signals master to call dumpabort */
910 (void) kill(master, SIGTERM);
913 msg("The ENTIRE dump is aborted.\n");
926 msg("pid = %d exits with status %d\n", getpid(), status);
932 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
938 siglongjmp(jmpbuf, 1);
946 #ifdef LINUX_FORK_BUG
954 { struct sigaction sa;
955 memset(&sa, 0, sizeof sa);
956 sigemptyset(&sa.sa_mask);
957 sa.sa_handler = dumpabort;
958 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
959 sa.sa_handler = sigpipe;
960 sigaction(SIGPIPE, &sa, NULL);
961 sa.sa_handler = proceed;
962 sa.sa_flags = SA_RESTART;
963 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
966 for (i = 0; i < SLAVES; i++) {
967 if (i == slp - &slaves[0]) {
973 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
974 (slaves[i].pid = fork()) < 0)
975 quit("too many slaves, %d (recompile smaller): %s\n",
978 slaves[i].fd = cmd[1];
980 if (slaves[i].pid == 0) { /* Slave starts up here */
982 for (j = 0; j <= i; j++)
983 (void) close(slaves[j].fd);
985 sigaddset(&sigs, SIGINT); /* Master handles this */
987 sigaddset(&sigs, SIGINFO);
989 sigprocmask(SIG_BLOCK, &sigs, NULL);
991 #ifdef LINUX_FORK_BUG
992 if (atomic_write( cmd[0], (char *) &i, sizeof i)
994 quit("master/slave protocol botched 3\n");
1003 #ifdef LINUX_FORK_BUG
1005 * Wait for all slaves to _actually_ start to circumvent a bug in
1006 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1007 * returned from fork() causes a SEGV in the child process
1009 for (i = 0; i < SLAVES; i++)
1010 if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
1011 quit("master/slave protocol botched 4\n");
1014 for (i = 0; i < SLAVES; i++)
1015 (void) atomic_write( slaves[i].fd,
1016 (char *) &slaves[(i + 1) % SLAVES].pid,
1017 sizeof slaves[0].pid);
1027 for (i = 0; i < SLAVES; i++)
1028 if (slaves[i].pid > 0) {
1029 (void) kill(slaves[i].pid, SIGKILL);
1035 * Synchronization - each process waits for a SIGUSR2 from the
1036 * previous process before writing to the tape, and sends SIGUSR2
1037 * to the next process when the tape write completes. On tape errors
1038 * a SIGUSR1 is sent to the master which then terminates all of the
1042 doslave(int cmd, int slave_number)
1045 int nextslave, size, eot_count, bufsize;
1046 volatile int wrote = 0;
1049 struct tapebuf *comp_buf = NULL;
1050 int compresult, do_compress = slave_number > 0;
1051 unsigned long worklen;
1052 #endif /* HAVE_ZLIB */
1053 struct slave_results returns;
1059 union u_spcl *uspclptr;
1060 struct s_spcl *spclptr;
1061 #endif /* USE_QFA */
1064 * Need our own seek pointer.
1066 (void) close(diskfd);
1067 if ((diskfd = OPEN(disk, O_RDONLY)) < 0)
1068 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1071 retval = dump_fs_open(disk, &fs);
1073 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1074 #endif /* __linux__ */
1077 * Need the pid of the next slave in the loop...
1079 if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1080 != sizeof nextslave) {
1081 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1085 /* if we're doing a compressed dump, allocate the compress buffer */
1087 comp_buf = malloc(sizeof(struct tapebuf) + TP_BSIZE + writesize);
1088 if (comp_buf == NULL)
1089 quit("couldn't allocate a compress buffer.\n");
1090 comp_buf->flags = 0;
1092 #endif /* HAVE_ZLIB */
1095 * Get list of blocks to dump, read the blocks into tape buffer
1097 while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1098 register struct req *p = slp->req;
1100 for (trecno = 0; trecno < ntrec;
1101 trecno += p->count, p += p->count) {
1102 if (p->dblk) { /* read a disk block */
1103 bread(p->dblk, slp->tblock[trecno],
1104 p->count * TP_BSIZE);
1105 } else { /* read record from pipe */
1106 if (p->count != 1 || atomic_read( cmd,
1107 (char *)slp->tblock[trecno],
1108 TP_BSIZE) != TP_BSIZE)
1109 quit("master/slave protocol botched.\n");
1113 /* Try to write the data... */
1117 buffer = (char *) slp->tblock[0]; /* set write pointer */
1118 bufsize = writesize; /* length to write */
1119 returns.clen = returns.unclen = bufsize;
1123 * When writing a compressed dump, each block except
1124 * the first one on each tape is written
1125 * from struct tapebuf with an 4 byte prefix
1126 * followed by the data. This can be less than
1127 * writesize. Restore, on a short read, can compare the
1128 * length read to the compressed length in the header
1129 * to verify that the read was good. Blocks which don't
1130 * compress well are written uncompressed.
1131 * The first block written by each slave is not compressed
1132 * and does not have a prefix.
1135 if (compressed && do_compress) {
1136 comp_buf->length = bufsize;
1137 worklen = TP_BSIZE + writesize;
1138 compresult = compress2(comp_buf->buf, &worklen,
1139 (char *)slp->tblock[0], writesize, compressed);
1140 if (compresult == Z_OK && worklen <= (writesize - 16)) {
1141 /* write the compressed buffer */
1142 comp_buf->length = worklen;
1143 comp_buf->compressed = 1;
1144 buffer = (char *) comp_buf;
1145 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1148 /* write the data uncompressed */
1149 comp_buf->length = writesize;
1150 comp_buf->compressed = 0;
1151 buffer = (char *) comp_buf;
1152 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1153 returns.unclen = returns.clen;
1154 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1157 /* compress the remaining blocks if we're compressing */
1158 do_compress = compressed;
1159 #endif /* HAVE_ZLIB */
1161 if (sigsetjmp(jmpbuf, 1) == 0) {
1170 if (gTapeposfd >= 0) {
1171 uspclptr = (union u_spcl *)&slp->tblock[0];
1172 spclptr = &uspclptr->s_spcl;
1173 if ((spclptr->c_magic == NFS_MAGIC) &&
1174 (spclptr->c_type == TS_INODE)) {
1175 /* if an error occured previously don't
1178 if ((gtperr = GetTapePos(&curtapepos)) == 0) {
1180 msg("inode %ld at tapepos %ld\n", spclptr->c_inumber, curtapepos);
1182 sprintf(gTps, "%ld\t%d\t%ld\n", (unsigned long)spclptr->c_inumber, tapeno, curtapepos);
1183 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps)) {
1184 warn("error writing tapepos file.\n");
1190 #endif /* USE_QFA */
1192 while (eot_count < 10 && size < bufsize) {
1195 wrote = rmtwrite(buffer + size, bufsize - size);
1198 wrote = write(tapefd, buffer + size, bufsize - size);
1200 printf("slave %d wrote %d\n", slave_number, wrote);
1210 if (size != bufsize)
1211 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1212 slave_number, size, bufsize);
1216 * Handle ENOSPC as an EOT condition.
1218 if (wrote < 0 && errno == ENOSPC) {
1224 returns.clen = returns.unclen = 0;
1227 * pass errno back to master for special handling
1230 returns.unclen = -errno;
1233 * pass size of data and size of write back to master
1234 * (for EOT handling)
1236 (void) atomic_write( cmd, (char *)&returns, sizeof returns);
1239 * Signal the next slave to go.
1241 (void) kill(nextslave, SIGUSR2);
1244 quit("error reading command pipe: %s\n", strerror(errno));
1248 * Since a read from a pipe may not return all we asked for,
1249 * or a write may not write all we ask if we get a signal,
1250 * loop until the count is satisfied (or error).
1253 atomic_read(int fd, void *buf, size_t count)
1255 int got, need = count;
1258 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1260 } while (got == -1 && errno == EINTR);
1261 return (got < 0 ? got : count - need);
1265 * Since a read from a pipe may not return all we asked for,
1266 * or a write may not write all we ask if we get a signal,
1267 * loop until the count is satisfied (or error).
1270 atomic_write(int fd, const void *buf, size_t count)
1272 int got, need = count;
1275 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1277 } while (got == -1 && errno == EINTR);
1278 return (got < 0 ? got : count - need);
1284 * read the current tape position
1287 GetTapePos(long *pos)
1292 if (ioctl(tapefd, MTIOCPOS, pos) == -1) {
1294 msg("[%ld] error: %d (getting tapepos: %ld)\n", getpid(),
1300 #endif /* USE_QFA */