2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999
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
44 static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
46 static const char rcsid[] =
47 "$Id: tape.c,v 1.4 1999/10/11 13:08:08 stelian Exp $";
51 #include <sys/types.h>
52 #include <linux/types.h>
54 #include <sys/param.h>
55 #include <sys/socket.h>
59 #include <linux/ext2_fs.h>
60 #include <bsdcompat.h>
63 #include <sys/vnode.h>
66 #include <ufs/inode.h>
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
71 #endif /* __linux__ */
73 #include <protocols/dumprestore.h>
89 #include <ext2fs/ext2fs.h>
93 int writesize; /* size of malloc()ed buffer for tape */
94 long lastspclrec = -1; /* tape block number of last written header */
95 int trecno = 0; /* next record to write in current block */
96 extern long blocksperfile; /* number of blocks per output file */
97 long blocksthisvol; /* number of blocks on current output file */
98 extern int ntrec; /* blocking factor on tape */
103 static ssize_t atomic_read __P((int, void *, size_t));
104 static ssize_t atomic_write __P((int, const void *, size_t));
105 static void doslave __P((int, int));
106 static void enslave __P((void));
107 static void flushtape __P((void));
108 static void killall __P((void));
109 static void rollforward __P((void));
112 * Concurrent dump mods (Caltech) - disk block reading and tape writing
113 * are exported to several slave processes. While one slave writes the
114 * tape, the others read disk blocks; they pass control of the tape in
115 * a ring via signals. The parent process traverses the filesystem and
116 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
117 * The following structure defines the instruction packets sent to slaves.
125 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
127 int tapea; /* header number at start of this chunk */
128 int count; /* count to next header (used for TS_TAPE */
130 int inode; /* inode that we are currently dealing with */
131 int fd; /* FD for this slave */
132 int pid; /* PID for this slave */
133 int sent; /* 1 == we've sent this slave requests */
134 int firstrec; /* record number of this block */
135 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
136 struct req *req; /* buffer for requests */
140 char (*nextblock)[TP_BSIZE];
142 static time_t tstart_volume; /* time of volume start */
143 static int tapea_volume; /* value of spcl.c_tapea at volume start */
145 int master; /* pid of master, for sending error signals */
146 int tenths; /* length of tape used per block written */
147 static int caught; /* have we caught the signal to proceed? */
148 static int ready; /* have we reached the lock point without having */
149 /* received the SIGUSR2 signal from the prev slave? */
150 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
151 /* SIGUSR2 arrives from the previous slave */
156 int pgoff = getpagesize() - 1;
160 writesize = ntrec * TP_BSIZE;
161 reqsiz = (ntrec + 1) * sizeof(struct req);
163 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
164 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
165 * repositioning after stopping, i.e, streaming mode, where the gap is
166 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
168 if (blocksperfile == 0 && !unlimited)
169 tenths = writesize / density +
170 (cartridge ? 16 : density == 625 ? 5 : 8);
172 * Allocate tape buffer contiguous with the array of instruction
173 * packets, so flushtape() can write them together with one write().
174 * Align tape buffer on page boundary to speed up tape write().
176 for (i = 0; i <= SLAVES; i++) {
178 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
181 slaves[i].tblock = (char (*)[TP_BSIZE])
183 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
185 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
187 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
193 nextblock = slp->tblock;
198 writerec(const void *dp, int isspcl)
201 slp->req[trecno].dblk = (daddr_t)0;
202 slp->req[trecno].count = 1;
203 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
204 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
207 lastspclrec = spcl.c_tapea;
215 dumpblock(daddr_t blkno, int size)
217 int avail, tpblks, dblkno;
219 dblkno = fsbtodb(sblock, blkno);
220 tpblks = size >> tp_bshift;
221 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
222 slp->req[trecno].dblk = dblkno;
223 slp->req[trecno].count = avail;
225 spcl.c_tapea += avail;
228 dblkno += avail << (tp_bshift - dev_bshift);
240 msg("write error on %s\n", tape);
241 quit("Cannot recover\n");
244 msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno);
245 broadcast("DUMP WRITE ERROR!\n");
246 if (!query("Do you want to restart?"))
248 msg("Closing this volume. Prepare to restart with new media;\n");
249 msg("this dump volume will be rewritten.\n");
260 quit("Broken pipe\n");
265 * Update xferrate stats
278 ttaken = tnow - tstart_volume;
279 blocks = spcl.c_tapea - tapea_volume;
280 msg("Volume %d completed at: %s", tapeno,
287 msg("Volume %d took %d:%02d:%02d\n", tapeno,
288 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
289 msg("Volume %d transfer rate: %ld KB/s\n", tapeno,
291 xferrate += blocks / ttaken;
299 * information message upon receipt of SIGINFO
300 * (derived from optr.c::timeest())
303 statussig(int notused)
307 int save_errno = errno;
309 if (blockswritten < 500)
314 (void) time((time_t *) &tnow);
316 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
317 / blockswritten * tapesize;
318 (void)snprintf(msgbuf, sizeof(msgbuf),
319 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
320 (blockswritten * 100.0) / tapesize,
321 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
322 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
323 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
334 int siz = (char *)nextblock - (char *)slp->req;
336 slp->req[trecno].count = 0; /* Sentinel */
338 if (atomic_write( slp->fd, (char *)slp->req, siz) != siz)
339 quit("error writing command pipe: %s\n", strerror(errno));
340 slp->sent = 1; /* we sent a request, read the response later */
342 lastfirstrec = slp->firstrec;
344 if (++slp >= &slaves[SLAVES])
347 /* Read results back from next slave */
349 if (atomic_read( slp->fd, (char *)&got, sizeof got)
351 perror(" DUMP: error reading command pipe in master");
356 /* Check for end of tape */
357 if (got < writesize) {
358 msg("End of tape detected\n");
361 * Drain the results, don't care what the values were.
362 * If we read them here then trewind won't...
364 for (i = 0; i < SLAVES; i++) {
365 if (slaves[i].sent) {
366 if (atomic_read( slaves[i].fd,
367 (char *)&got, sizeof got)
369 perror(" DUMP: error reading command pipe in master");
383 if (spcl.c_type != TS_END) {
384 for (i = 0; i < spcl.c_count; i++)
385 if (spcl.c_addr[i] != 0)
388 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
389 slp->tapea = spcl.c_tapea;
390 slp->firstrec = lastfirstrec + ntrec;
392 nextblock = slp->tblock;
395 blockswritten += ntrec;
396 blocksthisvol += ntrec;
397 if (!pipeout && !unlimited && (blocksperfile ?
398 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
411 for (f = 0; f < SLAVES; f++) {
413 * Drain the results, but unlike EOT we DO (or should) care
414 * what the return values were, since if we detect EOT after
415 * we think we've written the last blocks to the tape anyway,
416 * we have to replay those blocks with rollforward.
418 * fixme: punt for now.
420 if (slaves[f].sent) {
421 if (atomic_read( slaves[f].fd, (char *)&got, sizeof got)
423 perror(" DUMP: error reading command pipe in master");
427 if (got != writesize) {
428 msg("EOT detected in last 2 tape records!\n");
429 msg("Use a longer tape, decrease the size estimate\n");
430 quit("or use no size estimate at all.\n");
433 (void) close(slaves[f].fd);
435 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
441 msg("Closing %s\n", tape);
446 while (rmtopen(tape, 0) < 0)
452 (void) close(tapefd);
453 while ((f = open(tape, 0)) < 0)
466 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
467 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
469 while (!query("Is the new volume mounted and ready to go?"))
470 if (query("Do you want to abort?")) {
479 register struct req *p, *q, *prev;
480 register struct slave *tslp;
481 int i, size, savedtapea, got;
482 union u_spcl *ntb, *otb;
487 tslp = &slaves[SLAVES];
488 ntb = (union u_spcl *)tslp->tblock[1];
491 * Each of the N slaves should have requests that need to
492 * be replayed on the next tape. Use the extra slave buffers
493 * (slaves[SLAVES]) to construct request lists to be sent to
494 * each slave in turn.
496 for (i = 0; i < SLAVES; i++) {
498 otb = (union u_spcl *)slp->tblock;
501 * For each request in the current slave, copy it to tslp.
505 for (p = slp->req; p->count > 0; p += p->count) {
508 *ntb++ = *otb++; /* copy the datablock also */
513 quit("rollforward: protocol botch");
525 nextblock = tslp->tblock;
526 savedtapea = spcl.c_tapea;
527 spcl.c_tapea = slp->tapea;
529 spcl.c_tapea = savedtapea;
530 lastspclrec = savedtapea - 1;
532 size = (char *)ntb - (char *)q;
533 if (atomic_write( slp->fd, (char *)q, size) != size) {
534 perror(" DUMP: error writing command pipe");
539 lastfirstrec = slp->firstrec;
541 if (++slp >= &slaves[SLAVES])
546 if (prev->dblk != 0) {
548 * If the last one was a disk block, make the
549 * first of this one be the last bit of that disk
552 q->dblk = prev->dblk +
553 prev->count * (TP_BSIZE / DEV_BSIZE);
554 ntb = (union u_spcl *)tslp->tblock;
557 * It wasn't a disk block. Copy the data to its
558 * new location in the buffer.
561 *((union u_spcl *)tslp->tblock) = *ntb;
562 ntb = (union u_spcl *)tslp->tblock[1];
566 nextblock = slp->tblock;
569 *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)tslp->tblock;
576 * Clear the first slaves' response. One hopes that it
577 * worked ok, otherwise the tape is much too short!
580 if (atomic_read( slp->fd, (char *)&got, sizeof got)
582 perror(" DUMP: error reading command pipe in master");
587 if (got != writesize) {
588 quit("EOT detected at start of the tape!\n");
594 if (spcl.c_type != TS_END) {
595 for (i = 0; i < spcl.c_count; i++)
596 if (spcl.c_addr[i] != 0)
600 slp->firstrec = lastfirstrec + ntrec;
601 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
604 blockswritten += ntrec;
605 blocksthisvol += ntrec;
610 * We implement taking and restoring checkpoints on the tape level.
611 * When each tape is opened, a new process is created by forking; this
612 * saves all of the necessary context in the parent. The child
613 * continues the dump; the parent waits around, saving the context.
614 * If the child returns X_REWRITE, then it had problems writing that tape;
615 * this causes the parent to fork again, duplicating the context, and
616 * everything continues as if nothing had happened.
619 startnewtape(int top)
627 void (*interrupt_save) __P((int signo));
628 #else /* __linux__ */
630 void (*interrupt_save)();
632 sig_t interrupt_save;
634 #endif /* __linux__ */
636 interrupt_save = signal(SIGINT, SIG_IGN);
637 parentpid = getpid();
638 tapea_volume = spcl.c_tapea;
640 (void)time4(&tstart_volume);
642 (void)time((&tstart_volume);
646 (void)signal(SIGINT, interrupt_save);
648 * All signals are inherited...
652 msg("Context save fork fails in parent %d\n", parentpid);
658 * save the context by waiting
659 * until the child doing all of the work returns.
660 * don't catch the interrupt
662 signal(SIGINT, SIG_IGN);
664 msg("Tape: %d; parent process: %d child process %d\n",
665 tapeno+1, parentpid, childpid);
667 while ((waitpid = wait(&status)) != childpid)
668 msg("Parent %d waiting for child %d has another child %d return\n",
669 parentpid, childpid, waitpid);
671 msg("Child %d returns LOB status %o\n",
672 childpid, status&0xFF);
674 status = (status >> 8) & 0xFF;
678 msg("Child %d finishes X_FINOK\n", childpid);
681 msg("Child %d finishes X_ABORT\n", childpid);
684 msg("Child %d finishes X_REWRITE\n", childpid);
687 msg("Child %d finishes unknown %d\n",
698 goto restore_check_point;
700 msg("Bad return code from dump: %d\n", status);
704 } else { /* we are the child; just continue */
706 sleep(4); /* allow time for parent's message to get out */
707 msg("Child on Tape %d has parent %d, my pid = %d\n",
708 tapeno+1, parentpid, getpid());
711 * If we have a name like "/dev/rmt0,/dev/rmt1",
712 * use the name before the comma first, and save
713 * the remaining names for subsequent volumes.
715 tapeno++; /* current tape sequence */
716 if (nexttape || strchr(tape, ',')) {
717 if (nexttape && *nexttape)
719 if ((p = strchr(tape, ',')) != NULL) {
724 msg("Dumping volume %d on %s\n", tapeno, tape);
727 while ((tapefd = (host ? rmtopen(tape, 2) :
728 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
730 while ((tapefd = (pipeout ? 1 :
731 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
734 msg("Cannot open output \"%s\".\n", tape);
735 if (!query("Do you want to retry the open?"))
739 enslave(); /* Share open tape file descriptor with slaves */
744 newtape++; /* new tape signal */
745 spcl.c_count = slp->count;
747 * measure firstrec in TP_BSIZE units since restore doesn't
748 * know the correct ntrec value...
750 spcl.c_firstrec = slp->firstrec;
752 spcl.c_type = TS_TAPE;
753 spcl.c_flags |= DR_NEWHEADER;
754 writeheader((ino_t)slp->inode);
755 spcl.c_flags &=~ DR_NEWHEADER;
756 msg("Volume %d started at: %s", tapeno,
758 ctime4(&tstart_volume));
760 ctime(&tstart_volume));
763 msg("Volume %d begins with blocks from inode %d\n",
772 if (master != 0 && master != getpid())
773 /* Signals master to call dumpabort */
774 (void) kill(master, SIGTERM);
777 msg("The ENTIRE dump is aborted.\n");
790 msg("pid = %d exits with status %d\n", getpid(), status);
796 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
803 siglongjmp(jmpbuf, 1);
811 #ifdef LINUX_FORK_BUG
819 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
820 signal(SIGPIPE, sigpipe);
821 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
822 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
824 for (i = 0; i < SLAVES; i++) {
825 if (i == slp - &slaves[0]) {
831 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
832 (slaves[i].pid = fork()) < 0)
833 quit("too many slaves, %d (recompile smaller): %s\n",
836 slaves[i].fd = cmd[1];
838 if (slaves[i].pid == 0) { /* Slave starts up here */
839 for (j = 0; j <= i; j++)
840 (void) close(slaves[j].fd);
841 signal(SIGINT, SIG_IGN); /* Master handles this */
843 signal(SIGINFO, SIG_IGN);
846 #ifdef LINUX_FORK_BUG
847 if (atomic_write( cmd[0], (char *) &i, sizeof i)
849 quit("master/slave protocol botched 3\n");
856 #ifdef LINUX_FORK_BUG
858 * Wait for all slaves to _actually_ start to circumvent a bug in
859 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
860 * returned from fork() causes a SEGV in the child process
862 for (i = 0; i < SLAVES; i++)
863 if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
864 quit("master/slave protocol botched 4\n");
867 for (i = 0; i < SLAVES; i++)
868 (void) atomic_write( slaves[i].fd,
869 (char *) &slaves[(i + 1) % SLAVES].pid,
870 sizeof slaves[0].pid);
880 for (i = 0; i < SLAVES; i++)
881 if (slaves[i].pid > 0) {
882 (void) kill(slaves[i].pid, SIGKILL);
888 * Synchronization - each process has a lockfile, and shares file
889 * descriptors to the following process's lockfile. When our write
890 * completes, we release our lock on the following process's lock-
891 * file, allowing the following process to lock it and proceed. We
892 * get the lock back for the next cycle by swapping descriptors.
895 doslave(int cmd, int slave_number)
898 int nextslave, size, eot_count;
899 volatile int wrote = 0;
906 * Need our own seek pointer.
908 (void) close(diskfd);
909 if ((diskfd = open(disk, O_RDONLY)) < 0)
910 quit("slave couldn't reopen disk: %s\n", strerror(errno));
913 retval = ext2fs_open(disk, 0, 0, 0, unix_io_manager, &fs);
915 quit("slave couldn't reopen disk: %s\n", strerror(errno));
916 #endif /* __linux__ */
919 * Need the pid of the next slave in the loop...
921 if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
922 != sizeof nextslave) {
923 quit("master/slave protocol botched - didn't get pid of next slave.\n");
927 * Get list of blocks to dump, read the blocks into tape buffer
929 while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
930 register struct req *p = slp->req;
932 for (trecno = 0; trecno < ntrec;
933 trecno += p->count, p += p->count) {
935 bread(p->dblk, slp->tblock[trecno],
936 p->count * TP_BSIZE);
938 if (p->count != 1 || atomic_read( cmd,
939 (char *)slp->tblock[trecno],
940 TP_BSIZE) != TP_BSIZE)
941 quit("master/slave protocol botched.\n");
944 if (setjmp(jmpbuf) == 0) {
952 /* Try to write the data... */
957 while (eot_count < 10 && size < writesize) {
960 wrote = rmtwrite(slp->tblock[0]+size,
964 wrote = write(tapefd, slp->tblock[0]+size,
967 printf("slave %d wrote %d\n", slave_number, wrote);
977 if (size != writesize)
978 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
979 slave_number, size, writesize);
983 * Handle ENOSPC as an EOT condition.
985 if (wrote < 0 && errno == ENOSPC) {
994 (void) kill(master, SIGUSR1);
995 sigemptyset(&sigset);
1000 * pass size of write back to master
1001 * (for EOT handling)
1003 (void) atomic_write( cmd, (char *)&size, sizeof size);
1007 * If partial write, don't want next slave to go.
1008 * Also jolts him awake.
1010 (void) kill(nextslave, SIGUSR2);
1013 quit("error reading command pipe: %s\n", strerror(errno));
1017 * Since a read from a pipe may not return all we asked for,
1018 * or a write may not write all we ask if we get a signal,
1019 * loop until the count is satisfied (or error).
1022 atomic_read(int fd, void *buf, size_t count)
1024 int got, need = count;
1026 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1028 return (got < 0 ? got : count - need);
1032 * Since a read from a pipe may not return all we asked for,
1033 * or a write may not write all we ask if we get a signal,
1034 * loop until the count is satisfied (or error).
1037 atomic_write(int fd, const void *buf, size_t count)
1039 int got, need = count;
1041 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1043 return (got < 0 ? got : count - need);