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.86 2004/07/07 11:07:29 stelian Exp $";
44 #include <compatlfs.h>
50 #include <compaterr.h>
61 #include <sys/types.h>
63 #include <sys/ioctl.h>
64 #include <sys/mount.h> /* for definition of BLKFLSBUF */
65 #ifndef BLKFLSBUF /* last resort... */
66 #define BLKFLSBUF _IO(0x12, 97) /* Flush buffer cache. */
70 #include <sys/param.h>
71 #include <sys/socket.h>
75 #ifdef HAVE_EXT2FS_EXT2_FS_H
76 #include <ext2fs/ext2_fs.h>
78 #include <linux/ext2_fs.h>
80 #include <ext2fs/ext2fs.h>
82 #include <bsdcompat.h>
84 #include <sys/vnode.h>
87 #include <ufs/inode.h>
89 #include <ufs/ufs/dinode.h>
90 #include <ufs/ffs/fs.h>
91 #endif /* __linux__ */
93 #include <protocols/dumprestore.h>
97 #endif /* HAVE_ZLIB */
101 #endif /* HAVE_BZLIB */
105 #endif /* HAVE_LZO */
109 int writesize; /* size of malloc()ed buffer for tape */
110 long lastspclrec = -1; /* tape block number of last written header */
111 int trecno = 0; /* next record to write in current block */
112 extern long *blocksperfiles; /* number of blocks per output file(s) */
113 long blocksperfiles_current; /* current position in blocksperfiles */
114 long blocksthisvol; /* number of blocks on current output file */
115 extern int ntrec; /* blocking factor on tape */
116 extern int cartridge;
120 long long tapea_bytes = 0; /* bytes_written at start of current volume */
121 static int magtapeout; /* output is really a tape */
123 static ssize_t dump_atomic_read __P((int, char *, size_t));
124 static ssize_t dump_atomic_write __P((int, const char *, size_t));
126 static void doslave __P((int, int, int));
128 static void doslave __P((int, int));
130 static void enslave __P((void));
131 static void flushtape __P((void));
132 static void killall __P((void));
133 static void rollforward __P((void));
135 static int GetTapePos __P((long long *));
136 static int MkTapeString __P((struct s_spcl *, long long));
137 #define FILESQFAPOS 20
141 * Concurrent dump mods (Caltech) - disk block reading and tape writing
142 * are exported to several slave processes. While one slave writes the
143 * tape, the others read disk blocks; they pass control of the tape in
144 * a ring via signals. The parent process traverses the filesystem and
145 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
146 * The following structure defines the instruction packets sent to slaves.
154 struct slave_results {
155 ssize_t unclen; /* uncompressed length */
156 ssize_t clen; /* compressed length */
159 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
161 int tapea; /* header number at start of this chunk */
162 int count; /* count to next header (used for TS_TAPE */
164 int inode; /* inode that we are currently dealing with */
165 int fd; /* FD for this slave */
166 int pid; /* PID for this slave */
167 int sent; /* 1 == we've sent this slave requests */
168 int firstrec; /* record number of this block */
169 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
170 struct req *req; /* buffer for requests */
174 char (*nextblock)[TP_BSIZE];
176 static time_t tstart_volume; /* time of volume start */
177 static int tapea_volume; /* value of spcl.c_tapea at volume start */
179 int master; /* pid of master, for sending error signals */
180 int tenths; /* length of tape overhead per block written */
181 static int caught; /* have we caught the signal to proceed? */
182 static int ready; /* have we reached the lock point without having */
183 /* received the SIGUSR2 signal from the prev slave? */
184 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
185 /* SIGUSR2 arrives from the previous slave */
187 static int gtperr = 0;
193 int pgoff = getpagesize() - 1;
197 writesize = ntrec * TP_BSIZE;
198 reqsiz = (ntrec + 1) * sizeof(struct req);
200 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
201 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
202 * repositioning after stopping, i.e, streaming mode, where the gap is
203 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
205 if (!blocksperfiles && !unlimited)
206 tenths = (cartridge ? 16 : density == 625 ? 5 : 8);
212 * Allocate tape buffer contiguous with the array of instruction
213 * packets, so flushtape() can write them together with one write().
214 * Align tape buffer on page boundary to speed up tape write().
216 for (i = 0; i <= SLAVES; i++) {
218 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
221 slaves[i].tblock = (char (*)[TP_BSIZE])
223 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
225 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
227 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
233 nextblock = slp->tblock;
238 writerec(const void *dp, int isspcl)
241 slp->req[trecno].dblk = (ext2_loff_t)0;
242 slp->req[trecno].count = 1;
243 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
244 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
246 /* Need to write it to the archive file */
247 if (! AfileActive && isspcl && (spcl.c_type == TS_END))
249 if (AfileActive && Afile >= 0) {
250 /* When we dump an inode which is not a directory,
251 * it means we ended the archive contents */
252 if (isspcl && (spcl.c_type == TS_INODE) &&
253 ((spcl.c_dinode.di_mode & S_IFMT) != IFDIR))
257 tmp = *(union u_spcl *)dp;
258 /* Write the record, _uncompressed_ */
260 tmp.s_spcl.c_flags &= ~DR_COMPRESSED;
263 if (write(Afile, &tmp, TP_BSIZE) != TP_BSIZE)
264 msg("error writing archive file: %s\n",
271 lastspclrec = spcl.c_tapea;
279 dumpblock(blk_t blkno, int size)
284 dblkno = fsbtodb(sblock, blkno);
285 tpblks = size >> tp_bshift;
286 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
287 slp->req[trecno].dblk = dblkno;
288 slp->req[trecno].count = avail;
290 spcl.c_tapea += avail;
293 dblkno += avail << (tp_bshift - dev_bshift);
305 msg("write error on %s: %s\n", tape, strerror(errnum));
306 quit("Cannot recover\n");
309 msg("write error %d blocks into volume %d: %s\n",
310 blocksthisvol, tapeno, strerror(errnum));
311 broadcast("DUMP WRITE ERROR!\n");
312 if (query("Do you want to rewrite this volume?")) {
313 msg("Closing this volume. Prepare to restart with new media;\n");
314 msg("this dump volume will be rewritten.\n");
320 if (query("Do you want to start the next tape?"))
326 sigpipe(UNUSED(int signo))
329 quit("Broken pipe\n");
334 * Update xferrate stats
343 ttaken = tnow - tstart_volume;
344 blocks = spcl.c_tapea - tapea_volume;
345 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
347 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno,
348 blocks, ((double)blocks * TP_BSIZE / 1048576));
350 long volkb = (bytes_written - tapea_bytes) / 1024;
351 long txfrate = volkb / ttaken;
352 msg("Volume %d took %d:%02d:%02d\n", tapeno,
353 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
354 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
358 double rate = .0005 + (double) blocks / (double) volkb;
359 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
361 tapeno, blocks, volkb, rate);
368 mktimeest(time_t tnow)
370 static char msgbuf[128];
375 if (blockswritten < 500)
377 if (blockswritten > tapesize)
378 tapesize = blockswritten;
379 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
380 / blockswritten * tapesize;
381 if (tnow > tstart_volume)
382 (void)snprintf(msgbuf, sizeof(msgbuf),
383 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
384 (blockswritten * 100.0) / tapesize,
385 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
386 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
388 (void)snprintf(msgbuf, sizeof(msgbuf),
389 "%3.2f%% done, finished in %d:%02d\n",
390 (blockswritten * 100.0) / tapesize,
391 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
399 * information message upon receipt of SIGINFO
402 statussig(int notused)
404 int save_errno = errno;
407 buf = mktimeest(time(NULL));
409 write(STDERR_FILENO, buf, strlen(buf));
419 struct slave_results returned;
421 int siz = (char *)nextblock - (char *)slp->req;
423 slp->req[trecno].count = 0; /* Sentinel */
425 if (dump_atomic_write( slp->fd, (char *)slp->req, siz) != siz)
426 quit("error writing command pipe: %s\n", strerror(errno));
427 slp->sent = 1; /* we sent a request, read the response later */
429 lastfirstrec = slp->firstrec;
431 if (++slp >= &slaves[SLAVES])
434 /* Read results back from next slave */
436 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
437 != sizeof returned) {
438 perror(" DUMP: error reading command pipe in master");
441 got = returned.unclen;
442 bytes_written += returned.clen;
443 if (returned.unclen == returned.clen)
447 /* Check for errors or end of tape */
449 /* Check for errors */
453 msg("End of tape detected\n");
456 * Drain the results, don't care what the values were.
457 * If we read them here then trewind won't...
459 for (i = 0; i < SLAVES; i++) {
460 if (slaves[i].sent) {
461 if (dump_atomic_read( slaves[i].fd,
462 (char *)&returned, sizeof returned)
463 != sizeof returned) {
464 perror(" DUMP: error reading command pipe in master");
478 if (spcl.c_type != TS_END) {
479 for (i = 0; i < spcl.c_count; i++)
480 if (spcl.c_addr[i] != 0)
483 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
484 slp->tapea = spcl.c_tapea;
485 slp->firstrec = lastfirstrec + ntrec;
487 nextblock = slp->tblock;
489 asize += tenths + returned.clen / density;
490 blockswritten += ntrec;
491 blocksthisvol += ntrec;
492 if (!pipeout && !unlimited) {
493 if (blocksperfiles && blocksperfiles[blocksperfiles_current]) {
494 if ( compressed ? (bytes_written - tapea_bytes + SLAVES * (writesize + sizeof(struct tapebuf))) >= (((long long)blocksperfiles[blocksperfiles_current]) * 1024)
495 : blocksthisvol >= blocksperfiles[blocksperfiles_current] ) {
500 else if (asize > tsize) {
513 struct slave_results returned;
515 for (f = 0; f < SLAVES; f++) {
517 * Drain the results, but unlike EOT we DO (or should) care
518 * what the return values were, since if we detect EOT after
519 * we think we've written the last blocks to the tape anyway,
520 * we have to replay those blocks with rollforward.
522 * fixme: punt for now.
524 if (slaves[f].sent) {
525 if (dump_atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
526 != sizeof returned) {
527 perror(" DUMP: error reading command pipe in master");
530 got = returned.unclen;
531 bytes_written += returned.clen;
532 if (returned.unclen == returned.clen)
540 msg("EOT detected in last 2 tape records!\n");
541 msg("Use a longer tape, decrease the size estimate\n");
542 quit("or use no size estimate at all.\n");
545 (void) close(slaves[f].fd);
547 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
552 msg("Closing %s\n", tape);
557 while (rmtopen(tape, O_RDONLY) < 0)
564 (void) close(tapefd);
566 while ((f = OPEN(tape, O_RDONLY)) < 0)
582 msg("Launching %s\n", eot_script);
583 eot_code = system_command(eot_script, tape, tapeno);
585 if (eot_code != 0 && eot_code != 1) {
586 msg("Dump aborted by the end of tape script\n");
591 if (nexttape || Mflag)
594 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
595 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
597 while (!query("Is the new volume mounted and ready to go?"))
598 if (query("Do you want to abort?")) {
607 struct req *p, *q = NULL, *prev;
609 int i, size, savedtapea, got;
610 union u_spcl *ntb, *otb;
611 struct slave_results returned;
616 tslp = &slaves[SLAVES];
617 ntb = (union u_spcl *)tslp->tblock[1];
620 * Each of the N slaves should have requests that need to
621 * be replayed on the next tape. Use the extra slave buffers
622 * (slaves[SLAVES]) to construct request lists to be sent to
623 * each slave in turn.
625 for (i = 0; i < SLAVES; i++) {
627 otb = (union u_spcl *)slp->tblock;
630 * For each request in the current slave, copy it to tslp.
634 for (p = slp->req; p->count > 0; p += p->count) {
637 *ntb++ = *otb++; /* copy the datablock also */
642 quit("rollforward: protocol botch");
654 nextblock = tslp->tblock;
655 savedtapea = spcl.c_tapea;
656 spcl.c_tapea = slp->tapea;
658 spcl.c_tapea = savedtapea;
659 lastspclrec = savedtapea - 1;
661 size = (char *)ntb - (char *)q;
662 if (dump_atomic_write( slp->fd, (char *)q, size) != size) {
663 perror(" DUMP: error writing command pipe");
668 lastfirstrec = slp->firstrec;
670 if (++slp >= &slaves[SLAVES])
675 if (prev->dblk != 0) {
677 * If the last one was a disk block, make the
678 * first of this one be the last bit of that disk
681 q->dblk = prev->dblk +
682 prev->count * (TP_BSIZE / DEV_BSIZE);
683 ntb = (union u_spcl *)tslp->tblock;
686 * It wasn't a disk block. Copy the data to its
687 * new location in the buffer.
690 *((union u_spcl *)tslp->tblock) = *ntb;
691 ntb = (union u_spcl *)tslp->tblock[1];
695 nextblock = slp->tblock;
698 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
699 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
706 * Clear the first slaves' response. One hopes that it
707 * worked ok, otherwise the tape is much too short!
710 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
711 != sizeof returned) {
712 perror(" DUMP: error reading command pipe in master");
715 got = returned.unclen;
716 bytes_written += returned.clen;
717 if (returned.clen == returned.unclen)
725 quit("EOT detected at start of the tape!\n");
731 if (spcl.c_type != TS_END) {
732 for (i = 0; i < spcl.c_count; i++)
733 if (spcl.c_addr[i] != 0)
737 slp->firstrec = lastfirstrec + ntrec;
738 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
740 asize += tenths + returned.clen / density;
741 blockswritten += ntrec;
742 blocksthisvol += ntrec;
747 * We implement taking and restoring checkpoints on the tape level.
748 * When each tape is opened, a new process is created by forking; this
749 * saves all of the necessary context in the parent. The child
750 * continues the dump; the parent waits around, saving the context.
751 * If the child returns X_REWRITE, then it had problems writing that tape;
752 * this causes the parent to fork again, duplicating the context, and
753 * everything continues as if nothing had happened.
756 startnewtape(int top)
767 sigaddset(&sigs, SIGINT);
768 sigprocmask(SIG_BLOCK, &sigs, NULL);
769 #else /* __linux__ */
771 void (*interrupt_save)();
773 sig_t interrupt_save;
775 interrupt_save = signal(SIGINT, SIG_IGN);
776 #endif /* __linux__ */
778 parentpid = getpid();
779 tapea_volume = spcl.c_tapea;
780 tapea_bytes = bytes_written;
781 tstart_volume = time(NULL);
785 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
787 (void)signal(SIGINT, interrupt_save);
790 * All signals are inherited...
794 msg("Context save fork fails in parent %d\n", parentpid);
800 * save the context by waiting
801 * until the child doing all of the work returns.
802 * don't catch the interrupt
805 sigprocmask(SIG_BLOCK, &sigs, NULL);
807 signal(SIGINT, SIG_IGN);
810 msg("Tape: %d; parent process: %d child process %d\n",
811 tapeno+1, parentpid, childpid);
813 while ((waitpid = wait(&status)) != childpid)
814 if (waitpid != rshpid)
815 msg("Parent %d waiting for child %d has another child %d return\n",
816 parentpid, childpid, waitpid);
818 msg("Child %d returns LOB status %o\n",
819 childpid, status&0xFF);
821 status = (status >> 8) & 0xFF;
825 msg("Child %d finishes X_FINOK\n", childpid);
828 msg("Child %d finishes X_ABORT\n", childpid);
831 msg("Child %d finishes X_REWRITE\n", childpid);
834 msg("Child %d finishes unknown %d\n",
845 goto restore_check_point;
847 msg("Bad return code from dump: %d\n", status);
851 } else { /* we are the child; just continue */
853 sleep(4); /* allow time for parent's message to get out */
854 msg("Child on Tape %d has parent %d, my pid = %d\n",
855 tapeno+1, parentpid, getpid());
858 * If we have a name like "/dev/rmt0,/dev/rmt1",
859 * use the name before the comma first, and save
860 * the remaining names for subsequent volumes.
862 tapeno++; /* current tape sequence */
864 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
865 tape[MAXPATHLEN - 1] = '\0';
866 msg("Dumping volume %d on %s\n", tapeno, tape);
868 else if (nexttape || strchr(tapeprefix, ',')) {
869 if (nexttape && *nexttape)
870 tapeprefix = nexttape;
871 if ((p = strchr(tapeprefix, ',')) != NULL) {
876 strncpy(tape, tapeprefix, MAXPATHLEN);
877 tape[MAXPATHLEN - 1] = '\0';
878 msg("Dumping volume %d on %s\n", tapeno, tape);
880 if (blocksperfiles && blocksperfiles_current < *blocksperfiles)
881 blocksperfiles_current++;
883 while ((tapefd = (host ? rmtopen(tape, O_WRONLY|O_CREAT|O_TRUNC) : pipeout ?
885 OPEN(tape, O_WRONLY|O_CREAT|O_TRUNC, 0666))) < 0)
887 while ((tapefd = (pipeout ? fileno(stdout) :
888 OPEN(tape, O_WRONLY|O_CREAT|O_TRUNC, 0666))) < 0)
891 msg("Cannot open output \"%s\": %s\n", tape,
893 if (!query("Do you want to retry the open?"))
900 struct mtget mt_stat;
901 magtapeout = ioctl(tapefd, MTIOCGET, (char *)&mt_stat) == 0;
903 msg("Output is to %s\n",
904 magtapeout ? "tape" : "file/pipe");
908 enslave(); /* Share open tape file descriptor with slaves */
913 newtape++; /* new tape signal */
914 spcl.c_count = slp->count;
916 * measure firstrec in TP_BSIZE units since restore doesn't
917 * know the correct ntrec value...
919 spcl.c_firstrec = slp->firstrec;
921 spcl.c_type = TS_TAPE;
922 spcl.c_flags |= DR_NEWHEADER;
923 spcl.c_ntrec = ntrec;
925 spcl.c_flags |= DR_COMPRESSED;
926 writeheader((dump_ino_t)slp->inode);
927 spcl.c_flags &=~ DR_NEWHEADER;
928 msg("Volume %d started with block %ld at: %s", tapeno,
929 spcl.c_tapea, ctime(&tstart_volume));
931 msg("Volume %d begins with blocks from inode %d\n",
933 if (tapeno < (int)TP_NINOS)
934 volinfo[tapeno] = slp->inode;
939 dumpabort(UNUSED(int signo))
942 if (master != 0 && master != getpid())
943 /* Signals master to call dumpabort */
944 (void) kill(master, SIGTERM);
947 msg("The ENTIRE dump is aborted.\n");
960 msg("pid = %d exits with status %d\n", getpid(), status);
966 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
969 proceed(UNUSED(int signo))
972 siglongjmp(jmpbuf, 1);
980 #ifdef LINUX_FORK_BUG
988 { struct sigaction sa;
989 memset(&sa, 0, sizeof sa);
990 sigemptyset(&sa.sa_mask);
991 sa.sa_handler = dumpabort;
992 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
993 sa.sa_handler = sigpipe;
994 sigaction(SIGPIPE, &sa, NULL);
995 sa.sa_handler = proceed;
996 sa.sa_flags = SA_RESTART;
997 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
1000 for (i = 0; i < SLAVES; i++) {
1001 if (i == slp - &slaves[0]) {
1007 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
1008 (slaves[i].pid = fork()) < 0)
1009 quit("too many slaves, %d (recompile smaller): %s\n",
1010 i, strerror(errno));
1012 slaves[i].fd = cmd[1];
1014 if (slaves[i].pid == 0) { /* Slave starts up here */
1016 for (j = 0; j <= i; j++)
1017 (void) close(slaves[j].fd);
1019 sigaddset(&sigs, SIGINT); /* Master handles this */
1020 #if defined(SIGINFO)
1021 sigaddset(&sigs, SIGINFO);
1023 sigprocmask(SIG_BLOCK, &sigs, NULL);
1025 #ifdef LINUX_FORK_BUG
1026 if (dump_atomic_write( cmd[0], (char *) &i, sizeof i)
1028 quit("master/slave protocol botched 3\n");
1034 (slaves[i].pid == slp->pid));
1041 #ifdef LINUX_FORK_BUG
1043 * Wait for all slaves to _actually_ start to circumvent a bug in
1044 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1045 * returned from fork() causes a SEGV in the child process
1047 for (i = 0; i < SLAVES; i++)
1048 if (dump_atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
1049 quit("master/slave protocol botched 4\n");
1052 for (i = 0; i < SLAVES; i++)
1053 (void) dump_atomic_write( slaves[i].fd,
1054 (char *) &slaves[(i + 1) % SLAVES].pid,
1055 sizeof slaves[0].pid);
1065 for (i = 0; i < SLAVES; i++)
1066 if (slaves[i].pid > 0) {
1067 (void) kill(slaves[i].pid, SIGKILL);
1073 * Synchronization - each process waits for a SIGUSR2 from the
1074 * previous process before writing to the tape, and sends SIGUSR2
1075 * to the next process when the tape write completes. On tape errors
1076 * a SIGUSR1 is sent to the master which then terminates all of the
1088 volatile int wrote = 0, size, eot_count, bufsize;
1089 char * volatile buffer;
1090 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1091 struct tapebuf * volatile comp_buf = NULL;
1093 volatile int do_compress = !first;
1094 unsigned long worklen;
1096 lzo_align_t __LZO_MMODEL *LZO_WorkMem;
1098 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1099 struct slave_results returns;
1104 long long curtapepos;
1105 union u_spcl *uspclptr;
1106 struct s_spcl *spclptr;
1107 /* long maxntrecs = 300000000 / (ntrec * 1024); last tested: 50 000 000 */
1108 long maxntrecs = 50000; /* every 50MB */
1109 long cntntrecs = maxntrecs;
1110 #endif /* USE_QFA */
1114 sigaddset(&set, SIGUSR2);
1115 sigprocmask(SIG_BLOCK, &set, NULL);
1119 * Need our own seek pointer.
1121 (void) close(diskfd);
1122 if ((diskfd = OPEN(disk, O_RDONLY)) < 0)
1123 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1126 (void)ioctl(diskfd, BLKFLSBUF, 0);
1129 retval = dump_fs_open(disk, &fs);
1131 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1132 #endif /* __linux__ */
1135 * Need the pid of the next slave in the loop...
1137 if ((nread = dump_atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1138 != sizeof nextslave) {
1139 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1142 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1143 /* if we're doing a compressed dump, allocate the compress buffer */
1145 int bsiz = sizeof(struct tapebuf) + writesize;
1146 /* Add extra space to deal with compression enlarging the buffer */
1147 if (TP_BSIZE > writesize/16 + 67)
1150 bsiz += writesize/16 + 67;
1151 comp_buf = malloc(bsiz);
1152 if (comp_buf == NULL)
1153 quit("couldn't allocate a compress buffer.\n");
1154 if (zipflag == COMPRESS_ZLIB)
1155 comp_buf->flags = COMPRESS_ZLIB;
1156 else if (zipflag == COMPRESS_BZLIB)
1157 comp_buf->flags = COMPRESS_BZLIB;
1158 else if (zipflag == COMPRESS_LZO) {
1159 comp_buf->flags = COMPRESS_LZO;
1160 if (lzo_init() != LZO_E_OK) quit("lzo_init failed\n");
1162 quit("internal error - unknown compression method: %d\n", zipflag);
1165 LZO_WorkMem = malloc(LZO1X_1_MEM_COMPRESS);
1167 quit("couldn't allocate a compress buffer.\n");
1169 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1172 * Get list of blocks to dump, read the blocks into tape buffer
1174 while ((nread = dump_atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1175 struct req *p = slp->req;
1177 for (trecno = 0; trecno < ntrec;
1178 trecno += p->count, p += p->count) {
1179 if (p->dblk) { /* read a disk block */
1180 bread(p->dblk, slp->tblock[trecno],
1181 p->count * TP_BSIZE);
1182 } else { /* read record from pipe */
1183 if (p->count != 1 || dump_atomic_read( cmd,
1184 (char *)slp->tblock[trecno],
1185 TP_BSIZE) != TP_BSIZE)
1186 quit("master/slave protocol botched.\n");
1190 /* Try to write the data... */
1194 buffer = (char *) slp->tblock[0]; /* set write pointer */
1195 bufsize = writesize; /* length to write */
1196 returns.clen = returns.unclen = bufsize;
1198 #if defined(HAVE_ZLIB) || defined(HAVE_BZLIB) || defined(HAVE_LZO)
1200 * When writing a compressed dump, each block except
1201 * the first one on each tape is written
1202 * from struct tapebuf with an 4 byte prefix
1203 * followed by the data. This can be less than
1204 * writesize. Restore, on a short read, can compare the
1205 * length read to the compressed length in the header
1206 * to verify that the read was good. Blocks which don't
1207 * compress well are written uncompressed.
1208 * The first block written by each slave is not compressed
1209 * and does not have a prefix.
1212 if (compressed && do_compress) {
1213 comp_buf->length = bufsize;
1214 worklen = TP_BSIZE + writesize;
1217 if (zipflag == COMPRESS_ZLIB) {
1218 compresult = compress2(comp_buf->buf,
1220 (char *)slp->tblock[0],
1223 if (compresult == Z_OK)
1228 #endif /* HAVE_ZLIB */
1230 if (zipflag == COMPRESS_BZLIB) {
1231 unsigned int worklen2 = worklen;
1232 compresult = BZ2_bzBuffToBuffCompress(
1235 (char *)slp->tblock[0],
1240 if (compresult == BZ_OK)
1246 #endif /* HAVE_BZLIB */
1248 if (zipflag == COMPRESS_LZO) {
1249 lzo_uint worklen2 = worklen;
1250 compresult = lzo1x_1_compress((char *)slp->tblock[0],writesize,
1255 if (compresult == LZO_E_OK)
1260 #endif /* HAVE_LZO */
1261 if (compresult && worklen <= ((unsigned long)writesize - 16)) {
1262 /* write the compressed buffer */
1263 comp_buf->length = worklen;
1264 comp_buf->compressed = 1;
1265 buffer = (char *) comp_buf;
1266 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1269 /* write the data uncompressed */
1270 comp_buf->length = writesize;
1271 comp_buf->compressed = 0;
1272 buffer = (char *) comp_buf;
1273 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1274 returns.unclen = returns.clen;
1275 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1278 /* compress the remaining blocks if we're compressing */
1279 do_compress = compressed;
1280 #endif /* HAVE_ZLIB || HAVE_BZLIB || HAVE_LZO */
1282 if (sigsetjmp(jmpbuf, 1) == 0) {
1291 if (gTapeposfd >= 0) {
1295 for (i = 0; (i < ntrec) && !foundone; ++i) {
1296 uspclptr = (union u_spcl *)&slp->tblock[i];
1297 spclptr = &uspclptr->s_spcl;
1298 if ((spclptr->c_magic == NFS_MAGIC) &&
1299 (spclptr->c_type == TS_INODE) &&
1300 (spclptr->c_date == gThisDumpDate) &&
1301 !(spclptr->c_dinode.di_mode & S_IFDIR)
1304 /* if (cntntrecs >= maxntrecs) { only write every maxntrecs amount of data */
1307 gtperr = GetTapePos(&curtapepos);
1308 /* if an error occured previously don't
1312 msg("inode %ld at tapepos %ld\n", spclptr->c_inumber, curtapepos);
1314 gtperr = MkTapeString(spclptr, curtapepos);
1320 #endif /* USE_QFA */
1322 while (eot_count < 10 && size < bufsize) {
1325 wrote = rmtwrite(buffer + size, bufsize - size);
1328 wrote = write(tapefd, buffer + size, bufsize - size);
1330 printf("slave %d wrote %d\n", slave_number, wrote);
1332 if (wrote < 0 && errno != ENOSPC)
1334 if (wrote == 0 || (wrote < 0 && errno == ENOSPC))
1341 if (size != bufsize)
1342 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1343 slave_number, size, bufsize);
1347 * Handle ENOSPC as an EOT condition.
1349 if (wrote < 0 && errno == ENOSPC) {
1355 returns.clen = returns.unclen = 0;
1358 * pass errno back to master for special handling
1361 returns.unclen = -errno;
1364 * pass size of data and size of write back to master
1365 * (for EOT handling)
1367 (void) dump_atomic_write( cmd, (char *)&returns, sizeof returns);
1370 * Signal the next slave to go.
1372 (void) kill(nextslave, SIGUSR2);
1374 if (gTapeposfd >= 0) {
1377 #endif /* USE_QFA */
1380 quit("error reading command pipe: %s\n", strerror(errno));
1384 * Since a read from a pipe may not return all we asked for,
1385 * or a write may not write all we ask if we get a signal,
1386 * loop until the count is satisfied (or error).
1389 dump_atomic_read(int fd, char *buf, size_t count)
1391 int got, need = count;
1394 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1396 } while (got == -1 && errno == EINTR);
1397 return (got < 0 ? got : (ssize_t)count - need);
1401 * Since a read from a pipe may not return all we asked for,
1402 * or a write may not write all we ask if we get a signal,
1403 * loop until the count is satisfied (or error).
1406 dump_atomic_write(int fd, const char *buf, size_t count)
1408 int got, need = count;
1411 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1413 } while (got == -1 && errno == EINTR);
1414 return (got < 0 ? got : (ssize_t)count - need);
1425 buf.mt_op = MTSETDRVBUFFER;
1426 buf.mt_count = MT_ST_BOOLEANS | MT_ST_SCSI2LOGICAL;
1427 if (ioctl(tapefd, MTIOCTOP, &buf) == -1) {
1429 msg("[%ld] error: %d (setting logical)\n",
1430 (unsigned long)getpid(), err);
1437 #define LSEEK_GET_TAPEPOS 10
1438 #define LSEEK_GO2_TAPEPOS 11
1440 * read the current tape position
1443 GetTapePos(long long *pos)
1449 *pos = (long long) rmtseek((OFF_T)0, (int)LSEEK_GET_TAPEPOS);
1458 err = (ioctl(tapefd, MTIOCPOS, &mtpos) < 0);
1459 *pos = (long long)mtpos;
1462 *pos = LSEEK(tapefd, 0, SEEK_CUR);
1468 msg("[%ld] error: %d (getting tapepos: %lld)\n", getpid(),
1476 MkTapeString(struct s_spcl *spclptr, long long curtapepos)
1481 msg("inode %ld at tapepos %lld\n", spclptr->c_inumber, curtapepos);
1484 snprintf(gTps, sizeof(gTps), "%ld\t%d\t%lld\n",
1485 (unsigned long)spclptr->c_inumber,
1488 gTps[sizeof(gTps) - 1] = '\0';
1489 if (write(gTapeposfd, gTps, strlen(gTps)) != (ssize_t)strlen(gTps)) {
1491 warn("error writing tapepos file. (error %d)\n", errno);
1495 #endif /* USE_QFA */