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.83 2004/04/21 09:15:08 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, void *, size_t));
120 static ssize_t dump_atomic_write __P((int, const void *, 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 = (daddr_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(daddr_t blkno, int size)
277 int avail, tpblks, dblkno;
279 dblkno = fsbtodb(sblock, blkno);
280 tpblks = size >> tp_bshift;
281 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
282 slp->req[trecno].dblk = dblkno;
283 slp->req[trecno].count = avail;
285 spcl.c_tapea += avail;
288 dblkno += avail << (tp_bshift - dev_bshift);
300 msg("write error on %s: %s\n", tape, strerror(errnum));
301 quit("Cannot recover\n");
304 msg("write error %d blocks into volume %d: %s\n",
305 blocksthisvol, tapeno, strerror(errnum));
306 broadcast("DUMP WRITE ERROR!\n");
307 if (query("Do you want to rewrite this volume?")) {
308 msg("Closing this volume. Prepare to restart with new media;\n");
309 msg("this dump volume will be rewritten.\n");
315 if (query("Do you want to start the next tape?"))
321 sigpipe(UNUSED(int signo))
324 quit("Broken pipe\n");
329 * Update xferrate stats
338 ttaken = tnow - tstart_volume;
339 blocks = spcl.c_tapea - tapea_volume;
340 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
342 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno,
343 blocks, ((double)blocks * TP_BSIZE / 1048576));
345 long volkb = (bytes_written - tapea_bytes) / 1024;
346 long txfrate = volkb / ttaken;
347 msg("Volume %d took %d:%02d:%02d\n", tapeno,
348 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
349 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
353 double rate = .0005 + (double) blocks / (double) volkb;
354 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
356 tapeno, blocks, volkb, rate);
363 mktimeest(time_t tnow)
365 static char msgbuf[128];
370 if (blockswritten < 500)
372 if (blockswritten > tapesize)
373 tapesize = blockswritten;
374 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
375 / blockswritten * tapesize;
376 if (tnow > tstart_volume)
377 (void)snprintf(msgbuf, sizeof(msgbuf),
378 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
379 (blockswritten * 100.0) / tapesize,
380 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
381 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
383 (void)snprintf(msgbuf, sizeof(msgbuf),
384 "%3.2f%% done, finished in %d:%02d\n",
385 (blockswritten * 100.0) / tapesize,
386 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
394 * information message upon receipt of SIGINFO
397 statussig(int notused)
399 int save_errno = errno;
402 buf = mktimeest(time(NULL));
404 write(STDERR_FILENO, buf, strlen(buf));
414 struct slave_results returned;
416 int siz = (char *)nextblock - (char *)slp->req;
418 slp->req[trecno].count = 0; /* Sentinel */
420 if (dump_atomic_write( slp->fd, (char *)slp->req, siz) != siz)
421 quit("error writing command pipe: %s\n", strerror(errno));
422 slp->sent = 1; /* we sent a request, read the response later */
424 lastfirstrec = slp->firstrec;
426 if (++slp >= &slaves[SLAVES])
429 /* Read results back from next slave */
431 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
432 != sizeof returned) {
433 perror(" DUMP: error reading command pipe in master");
436 got = returned.unclen;
437 bytes_written += returned.clen;
438 if (returned.unclen == returned.clen)
442 /* Check for errors or end of tape */
444 /* Check for errors */
448 msg("End of tape detected\n");
451 * Drain the results, don't care what the values were.
452 * If we read them here then trewind won't...
454 for (i = 0; i < SLAVES; i++) {
455 if (slaves[i].sent) {
456 if (dump_atomic_read( slaves[i].fd,
457 (char *)&returned, sizeof returned)
458 != sizeof returned) {
459 perror(" DUMP: error reading command pipe in master");
473 if (spcl.c_type != TS_END) {
474 for (i = 0; i < spcl.c_count; i++)
475 if (spcl.c_addr[i] != 0)
478 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
479 slp->tapea = spcl.c_tapea;
480 slp->firstrec = lastfirstrec + ntrec;
482 nextblock = slp->tblock;
484 asize += tenths + returned.clen / density;
485 blockswritten += ntrec;
486 blocksthisvol += ntrec;
487 if (!pipeout && !unlimited) {
488 if (blocksperfiles && blocksperfiles[blocksperfiles_current]) {
489 if ( compressed ? (bytes_written - tapea_bytes + SLAVES * (writesize + sizeof(struct tapebuf))) >= (((long long)blocksperfiles[blocksperfiles_current]) * 1024)
490 : blocksthisvol >= blocksperfiles[blocksperfiles_current] ) {
495 else if (asize > tsize) {
508 struct slave_results returned;
510 for (f = 0; f < SLAVES; f++) {
512 * Drain the results, but unlike EOT we DO (or should) care
513 * what the return values were, since if we detect EOT after
514 * we think we've written the last blocks to the tape anyway,
515 * we have to replay those blocks with rollforward.
517 * fixme: punt for now.
519 if (slaves[f].sent) {
520 if (dump_atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
521 != sizeof returned) {
522 perror(" DUMP: error reading command pipe in master");
525 got = returned.unclen;
526 bytes_written += returned.clen;
527 if (returned.unclen == returned.clen)
535 msg("EOT detected in last 2 tape records!\n");
536 msg("Use a longer tape, decrease the size estimate\n");
537 quit("or use no size estimate at all.\n");
540 (void) close(slaves[f].fd);
542 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
547 msg("Closing %s\n", tape);
552 while (rmtopen(tape, O_RDONLY) < 0)
559 (void) close(tapefd);
561 while ((f = OPEN(tape, O_RDONLY)) < 0)
577 msg("Launching %s\n", eot_script);
578 eot_code = system_command(eot_script, tape, tapeno);
580 if (eot_code != 0 && eot_code != 1) {
581 msg("Dump aborted by the end of tape script\n");
586 if (nexttape || Mflag)
589 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
590 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
592 while (!query("Is the new volume mounted and ready to go?"))
593 if (query("Do you want to abort?")) {
602 struct req *p, *q = NULL, *prev;
604 int i, size, savedtapea, got;
605 union u_spcl *ntb, *otb;
606 struct slave_results returned;
611 tslp = &slaves[SLAVES];
612 ntb = (union u_spcl *)tslp->tblock[1];
615 * Each of the N slaves should have requests that need to
616 * be replayed on the next tape. Use the extra slave buffers
617 * (slaves[SLAVES]) to construct request lists to be sent to
618 * each slave in turn.
620 for (i = 0; i < SLAVES; i++) {
622 otb = (union u_spcl *)slp->tblock;
625 * For each request in the current slave, copy it to tslp.
629 for (p = slp->req; p->count > 0; p += p->count) {
632 *ntb++ = *otb++; /* copy the datablock also */
637 quit("rollforward: protocol botch");
649 nextblock = tslp->tblock;
650 savedtapea = spcl.c_tapea;
651 spcl.c_tapea = slp->tapea;
653 spcl.c_tapea = savedtapea;
654 lastspclrec = savedtapea - 1;
656 size = (char *)ntb - (char *)q;
657 if (dump_atomic_write( slp->fd, (char *)q, size) != size) {
658 perror(" DUMP: error writing command pipe");
663 lastfirstrec = slp->firstrec;
665 if (++slp >= &slaves[SLAVES])
670 if (prev->dblk != 0) {
672 * If the last one was a disk block, make the
673 * first of this one be the last bit of that disk
676 q->dblk = prev->dblk +
677 prev->count * (TP_BSIZE / DEV_BSIZE);
678 ntb = (union u_spcl *)tslp->tblock;
681 * It wasn't a disk block. Copy the data to its
682 * new location in the buffer.
685 *((union u_spcl *)tslp->tblock) = *ntb;
686 ntb = (union u_spcl *)tslp->tblock[1];
690 nextblock = slp->tblock;
693 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
694 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
701 * Clear the first slaves' response. One hopes that it
702 * worked ok, otherwise the tape is much too short!
705 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
706 != sizeof returned) {
707 perror(" DUMP: error reading command pipe in master");
710 got = returned.unclen;
711 bytes_written += returned.clen;
712 if (returned.clen == returned.unclen)
720 quit("EOT detected at start of the tape!\n");
726 if (spcl.c_type != TS_END) {
727 for (i = 0; i < spcl.c_count; i++)
728 if (spcl.c_addr[i] != 0)
732 slp->firstrec = lastfirstrec + ntrec;
733 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
735 asize += tenths + returned.clen / density;
736 blockswritten += ntrec;
737 blocksthisvol += ntrec;
742 * We implement taking and restoring checkpoints on the tape level.
743 * When each tape is opened, a new process is created by forking; this
744 * saves all of the necessary context in the parent. The child
745 * continues the dump; the parent waits around, saving the context.
746 * If the child returns X_REWRITE, then it had problems writing that tape;
747 * this causes the parent to fork again, duplicating the context, and
748 * everything continues as if nothing had happened.
751 startnewtape(int top)
762 sigaddset(&sigs, SIGINT);
763 sigprocmask(SIG_BLOCK, &sigs, NULL);
764 #else /* __linux__ */
766 void (*interrupt_save)();
768 sig_t interrupt_save;
770 interrupt_save = signal(SIGINT, SIG_IGN);
771 #endif /* __linux__ */
773 parentpid = getpid();
774 tapea_volume = spcl.c_tapea;
775 tapea_bytes = bytes_written;
776 tstart_volume = time(NULL);
780 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
782 (void)signal(SIGINT, interrupt_save);
785 * All signals are inherited...
789 msg("Context save fork fails in parent %d\n", parentpid);
795 * save the context by waiting
796 * until the child doing all of the work returns.
797 * don't catch the interrupt
800 sigprocmask(SIG_BLOCK, &sigs, NULL);
802 signal(SIGINT, SIG_IGN);
805 msg("Tape: %d; parent process: %d child process %d\n",
806 tapeno+1, parentpid, childpid);
808 while ((waitpid = wait(&status)) != childpid)
809 if (waitpid != rshpid)
810 msg("Parent %d waiting for child %d has another child %d return\n",
811 parentpid, childpid, waitpid);
813 msg("Child %d returns LOB status %o\n",
814 childpid, status&0xFF);
816 status = (status >> 8) & 0xFF;
820 msg("Child %d finishes X_FINOK\n", childpid);
823 msg("Child %d finishes X_ABORT\n", childpid);
826 msg("Child %d finishes X_REWRITE\n", childpid);
829 msg("Child %d finishes unknown %d\n",
840 goto restore_check_point;
842 msg("Bad return code from dump: %d\n", status);
846 } else { /* we are the child; just continue */
848 sleep(4); /* allow time for parent's message to get out */
849 msg("Child on Tape %d has parent %d, my pid = %d\n",
850 tapeno+1, parentpid, getpid());
853 * If we have a name like "/dev/rmt0,/dev/rmt1",
854 * use the name before the comma first, and save
855 * the remaining names for subsequent volumes.
857 tapeno++; /* current tape sequence */
859 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
860 tape[MAXPATHLEN - 1] = '\0';
861 msg("Dumping volume %d on %s\n", tapeno, tape);
863 else if (nexttape || strchr(tapeprefix, ',')) {
864 if (nexttape && *nexttape)
865 tapeprefix = nexttape;
866 if ((p = strchr(tapeprefix, ',')) != NULL) {
871 strncpy(tape, tapeprefix, MAXPATHLEN);
872 tape[MAXPATHLEN - 1] = '\0';
873 msg("Dumping volume %d on %s\n", tapeno, tape);
875 if (blocksperfiles && blocksperfiles_current < *blocksperfiles)
876 blocksperfiles_current++;
878 while ((tapefd = (host ? rmtopen(tape, O_WRONLY|O_CREAT|O_TRUNC) : pipeout ?
880 OPEN(tape, O_WRONLY|O_CREAT|O_TRUNC, 0666))) < 0)
882 while ((tapefd = (pipeout ? fileno(stdout) :
883 OPEN(tape, O_WRONLY|O_CREAT|O_TRUNC, 0666))) < 0)
886 msg("Cannot open output \"%s\": %s\n", tape,
888 if (!query("Do you want to retry the open?"))
895 struct mtget mt_stat;
896 magtapeout = ioctl(tapefd, MTIOCGET, (char *)&mt_stat) == 0;
898 msg("Output is to %s\n",
899 magtapeout ? "tape" : "file/pipe");
903 enslave(); /* Share open tape file descriptor with slaves */
908 newtape++; /* new tape signal */
909 spcl.c_count = slp->count;
911 * measure firstrec in TP_BSIZE units since restore doesn't
912 * know the correct ntrec value...
914 spcl.c_firstrec = slp->firstrec;
916 spcl.c_type = TS_TAPE;
917 spcl.c_flags |= DR_NEWHEADER;
918 spcl.c_ntrec = ntrec;
920 spcl.c_flags |= DR_COMPRESSED;
921 writeheader((dump_ino_t)slp->inode);
922 spcl.c_flags &=~ DR_NEWHEADER;
923 msg("Volume %d started with block %ld at: %s", tapeno,
924 spcl.c_tapea, ctime(&tstart_volume));
926 msg("Volume %d begins with blocks from inode %d\n",
928 if (tapeno < (int)TP_NINOS)
929 volinfo[tapeno] = slp->inode;
934 dumpabort(UNUSED(int signo))
937 if (master != 0 && master != getpid())
938 /* Signals master to call dumpabort */
939 (void) kill(master, SIGTERM);
942 msg("The ENTIRE dump is aborted.\n");
955 msg("pid = %d exits with status %d\n", getpid(), status);
961 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
964 proceed(UNUSED(int signo))
967 siglongjmp(jmpbuf, 1);
975 #ifdef LINUX_FORK_BUG
983 { struct sigaction sa;
984 memset(&sa, 0, sizeof sa);
985 sigemptyset(&sa.sa_mask);
986 sa.sa_handler = dumpabort;
987 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
988 sa.sa_handler = sigpipe;
989 sigaction(SIGPIPE, &sa, NULL);
990 sa.sa_handler = proceed;
991 sa.sa_flags = SA_RESTART;
992 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
995 for (i = 0; i < SLAVES; i++) {
996 if (i == slp - &slaves[0]) {
1002 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
1003 (slaves[i].pid = fork()) < 0)
1004 quit("too many slaves, %d (recompile smaller): %s\n",
1005 i, strerror(errno));
1007 slaves[i].fd = cmd[1];
1009 if (slaves[i].pid == 0) { /* Slave starts up here */
1011 for (j = 0; j <= i; j++)
1012 (void) close(slaves[j].fd);
1014 sigaddset(&sigs, SIGINT); /* Master handles this */
1015 #if defined(SIGINFO)
1016 sigaddset(&sigs, SIGINFO);
1018 sigprocmask(SIG_BLOCK, &sigs, NULL);
1020 #ifdef LINUX_FORK_BUG
1021 if (dump_atomic_write( cmd[0], (char *) &i, sizeof i)
1023 quit("master/slave protocol botched 3\n");
1029 (slaves[i].pid == slp->pid));
1036 #ifdef LINUX_FORK_BUG
1038 * Wait for all slaves to _actually_ start to circumvent a bug in
1039 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1040 * returned from fork() causes a SEGV in the child process
1042 for (i = 0; i < SLAVES; i++)
1043 if (dump_atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
1044 quit("master/slave protocol botched 4\n");
1047 for (i = 0; i < SLAVES; i++)
1048 (void) dump_atomic_write( slaves[i].fd,
1049 (char *) &slaves[(i + 1) % SLAVES].pid,
1050 sizeof slaves[0].pid);
1060 for (i = 0; i < SLAVES; i++)
1061 if (slaves[i].pid > 0) {
1062 (void) kill(slaves[i].pid, SIGKILL);
1068 * Synchronization - each process waits for a SIGUSR2 from the
1069 * previous process before writing to the tape, and sends SIGUSR2
1070 * to the next process when the tape write completes. On tape errors
1071 * a SIGUSR1 is sent to the master which then terminates all of the
1083 volatile int wrote = 0, size, eot_count, bufsize;
1084 char * volatile buffer;
1085 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1086 struct tapebuf * volatile comp_buf = NULL;
1088 volatile int do_compress = !first;
1089 unsigned long worklen;
1091 lzo_align_t __LZO_MMODEL *LZO_WorkMem;
1093 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1094 struct slave_results returns;
1099 long long curtapepos;
1100 union u_spcl *uspclptr;
1101 struct s_spcl *spclptr;
1102 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1103 long maxntrecs = 50000; /* every 50MB */
1104 long cntntrecs = maxntrecs;
1105 #endif /* USE_QFA */
1109 sigaddset(&set, SIGUSR2);
1110 sigprocmask(SIG_BLOCK, &set, NULL);
1114 * Need our own seek pointer.
1116 (void) close(diskfd);
1117 if ((diskfd = OPEN(disk, O_RDONLY)) < 0)
1118 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1121 (void)ioctl(diskfd, BLKFLSBUF, 0);
1124 retval = dump_fs_open(disk, &fs);
1126 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1127 #endif /* __linux__ */
1130 * Need the pid of the next slave in the loop...
1132 if ((nread = dump_atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1133 != sizeof nextslave) {
1134 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1137 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1138 /* if we're doing a compressed dump, allocate the compress buffer */
1140 int bsiz = sizeof(struct tapebuf) + writesize;
1141 /* Add extra space to deal with compression enlarging the buffer */
1142 if (TP_BSIZE > writesize/16 + 67)
1145 bsiz += writesize/16 + 67;
1146 comp_buf = malloc(bsiz);
1147 if (comp_buf == NULL)
1148 quit("couldn't allocate a compress buffer.\n");
1149 if (zipflag == COMPRESS_ZLIB)
1150 comp_buf->flags = COMPRESS_ZLIB;
1151 else if (zipflag == COMPRESS_BZLIB)
1152 comp_buf->flags = COMPRESS_BZLIB;
1153 else if (zipflag == COMPRESS_LZO) {
1154 comp_buf->flags = COMPRESS_LZO;
1155 if (lzo_init() != LZO_E_OK) quit("lzo_init failed\n");
1157 quit("internal error - unknown compression method: %d\n", zipflag);
1160 LZO_WorkMem = malloc(LZO1X_1_MEM_COMPRESS);
1162 quit("couldn't allocate a compress buffer.\n");
1164 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1167 * Get list of blocks to dump, read the blocks into tape buffer
1169 while ((nread = dump_atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1170 struct req *p = slp->req;
1172 for (trecno = 0; trecno < ntrec;
1173 trecno += p->count, p += p->count) {
1174 if (p->dblk) { /* read a disk block */
1175 bread(p->dblk, slp->tblock[trecno],
1176 p->count * TP_BSIZE);
1177 } else { /* read record from pipe */
1178 if (p->count != 1 || dump_atomic_read( cmd,
1179 (char *)slp->tblock[trecno],
1180 TP_BSIZE) != TP_BSIZE)
1181 quit("master/slave protocol botched.\n");
1185 /* Try to write the data... */
1189 buffer = (char *) slp->tblock[0]; /* set write pointer */
1190 bufsize = writesize; /* length to write */
1191 returns.clen = returns.unclen = bufsize;
1193 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1195 * When writing a compressed dump, each block except
1196 * the first one on each tape is written
1197 * from struct tapebuf with an 4 byte prefix
1198 * followed by the data. This can be less than
1199 * writesize. Restore, on a short read, can compare the
1200 * length read to the compressed length in the header
1201 * to verify that the read was good. Blocks which don't
1202 * compress well are written uncompressed.
1203 * The first block written by each slave is not compressed
1204 * and does not have a prefix.
1207 if (compressed && do_compress) {
1208 comp_buf->length = bufsize;
1209 worklen = TP_BSIZE + writesize;
1212 if (zipflag == COMPRESS_ZLIB) {
1213 compresult = compress2(comp_buf->buf,
1215 (char *)slp->tblock[0],
1218 if (compresult == Z_OK)
1223 #endif /* HAVE_ZLIB */
1225 if (zipflag == COMPRESS_BZLIB) {
1226 unsigned int worklen2 = worklen;
1227 compresult = BZ2_bzBuffToBuffCompress(
1230 (char *)slp->tblock[0],
1235 if (compresult == BZ_OK)
1241 #endif /* HAVE_BZLIB */
1243 if (zipflag == COMPRESS_LZO) {
1244 lzo_uint worklen2 = worklen;
1245 compresult = lzo1x_1_compress((char *)slp->tblock[0],writesize,
1250 if (compresult == LZO_E_OK)
1255 #endif /* HAVE_LZO */
1256 if (compresult && worklen <= ((unsigned long)writesize - 16)) {
1257 /* write the compressed buffer */
1258 comp_buf->length = worklen;
1259 comp_buf->compressed = 1;
1260 buffer = (char *) comp_buf;
1261 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1264 /* write the data uncompressed */
1265 comp_buf->length = writesize;
1266 comp_buf->compressed = 0;
1267 buffer = (char *) comp_buf;
1268 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1269 returns.unclen = returns.clen;
1270 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1273 /* compress the remaining blocks if we're compressing */
1274 do_compress = compressed;
1275 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1277 if (sigsetjmp(jmpbuf, 1) == 0) {
1286 if (gTapeposfd >= 0) {
1290 for (i = 0; (i < ntrec) && !foundone; ++i) {
1291 uspclptr = (union u_spcl *)&slp->tblock[i];
1292 spclptr = &uspclptr->s_spcl;
1293 if ((spclptr->c_magic == NFS_MAGIC) &&
1294 (spclptr->c_type == TS_INODE) &&
1295 (spclptr->c_date == gThisDumpDate) &&
1296 !(spclptr->c_dinode.di_mode & S_IFDIR)
1299 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1302 gtperr = GetTapePos(&curtapepos);
1303 /* if an error occured previously don't
1307 msg("inode %ld at tapepos %ld\n", spclptr->c_inumber, curtapepos);
1309 gtperr = MkTapeString(spclptr, curtapepos);
1315 #endif /* USE_QFA */
1317 while (eot_count < 10 && size < bufsize) {
1320 wrote = rmtwrite(buffer + size, bufsize - size);
1323 wrote = write(tapefd, buffer + size, bufsize - size);
1325 printf("slave %d wrote %d\n", slave_number, wrote);
1327 if (wrote < 0 && errno != ENOSPC)
1329 if (wrote == 0 || (wrote < 0 && errno == ENOSPC))
1336 if (size != bufsize)
1337 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1338 slave_number, size, bufsize);
1342 * Handle ENOSPC as an EOT condition.
1344 if (wrote < 0 && errno == ENOSPC) {
1350 returns.clen = returns.unclen = 0;
1353 * pass errno back to master for special handling
1356 returns.unclen = -errno;
1359 * pass size of data and size of write back to master
1360 * (for EOT handling)
1362 (void) dump_atomic_write( cmd, (char *)&returns, sizeof returns);
1365 * Signal the next slave to go.
1367 (void) kill(nextslave, SIGUSR2);
1369 if (gTapeposfd >= 0) {
1372 #endif /* USE_QFA */
1375 quit("error reading command pipe: %s\n", strerror(errno));
1379 * Since a read from a pipe may not return all we asked for,
1380 * or a write may not write all we ask if we get a signal,
1381 * loop until the count is satisfied (or error).
1384 dump_atomic_read(int fd, void *buf, size_t count)
1386 int got, need = count;
1389 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1391 } while (got == -1 && errno == EINTR);
1392 return (got < 0 ? got : (ssize_t)count - need);
1396 * Since a read from a pipe may not return all we asked for,
1397 * or a write may not write all we ask if we get a signal,
1398 * loop until the count is satisfied (or error).
1401 dump_atomic_write(int fd, const void *buf, size_t count)
1403 int got, need = count;
1406 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1408 } while (got == -1 && errno == EINTR);
1409 return (got < 0 ? got : (ssize_t)count - need);
1420 buf.mt_op = MTSETDRVBUFFER;
1421 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1422 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1424 msg("[%ld] error: %d (setting logical)\n",
1425 (unsigned long)getpid(), err);
1432 #define LSEEK_GET_TAPEPOS 10
1433 #define LSEEK_GO2_TAPEPOS 11
1435 * read the current tape position
1438 GetTapePos(long long *pos)
1444 *pos = (long long) rmtseek((OFF_T)0, (int)LSEEK_GET_TAPEPOS);
1453 err = (ioctl(tapefd, MTIOCPOS, &mtpos) < 0);
1454 *pos = (long long)mtpos;
1457 *pos = LSEEK(tapefd, 0, SEEK_CUR);
1463 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1471 MkTapeString(struct s_spcl *spclptr, long long curtapepos)
1476 msg("inode %ld at tapepos %lld\n", spclptr->c_inumber, curtapepos);
1479 snprintf(gTps, sizeof(gTps), "%ld\t%d\t%lld\n",
1480 (unsigned long)spclptr->c_inumber,
1483 gTps[sizeof(gTps) - 1] = '\0';
1484 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps)) {
1486 warn("error writing tapepos file. (error %d)\n", errno);
1490 #endif /* USE_QFA */