122040Sdist /* 222040Sdist * Copyright (c) 1980 Regents of the University of California. 322040Sdist * All rights reserved. The Berkeley software License Agreement 422040Sdist * specifies the terms and conditions for redistribution. 522040Sdist */ 622040Sdist 717527Ssam #ifndef lint 8*46614Smckusick static char sccsid[] = "@(#)tape.c 5.13 (Berkeley) 02/24/91"; 946585Storek #endif /* not lint */ 1017527Ssam 1146585Storek #include "dump.h" 1246239Storek #include <sys/types.h> 1346585Storek #include <sys/wait.h> 1446585Storek #include <errno.h> 1546239Storek #include <fcntl.h> 1639128Smckusick #include "pathnames.h" 171425Sroot 1829899Smckusick char (*tblock)[TP_BSIZE]; /* pointer to malloc()ed buffer for tape */ 1929899Smckusick int writesize; /* size of malloc()ed buffer for tape */ 2029899Smckusick long lastspclrec = -1; /* tape block number of last written header */ 2129899Smckusick int trecno = 0; /* next record to write in current block */ 22*46614Smckusick extern long blocksperfile; /* number of blocks per output file */ 2325219Smckusick extern int ntrec; /* blocking factor on tape */ 2425219Smckusick extern int cartridge; 2525219Smckusick extern int read(), write(); 2625219Smckusick #ifdef RDUMP 2725219Smckusick extern char *host; 2846585Storek int rmtopen(), rmtwrite(); 2946585Storek void rmtclose(); 3025219Smckusick #endif RDUMP 311425Sroot 3246585Storek int atomic(); 3346585Storek void doslave(), enslave(), flusht(), killall(); 3446585Storek 3510911Ssam /* 3624181Smckusick * Concurrent dump mods (Caltech) - disk block reading and tape writing 3718012Smckusick * are exported to several slave processes. While one slave writes the 3818012Smckusick * tape, the others read disk blocks; they pass control of the tape in 3924181Smckusick * a ring via flock(). The parent process traverses the filesystem and 4025219Smckusick * sends spclrec()'s and lists of daddr's to the slaves via pipes. 4110911Ssam */ 4218012Smckusick struct req { /* instruction packets sent to slaves */ 4318012Smckusick daddr_t dblk; 4418012Smckusick int count; 4518012Smckusick } *req; 4618012Smckusick int reqsiz; 4718012Smckusick 4824181Smckusick #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 4925219Smckusick int slavefd[SLAVES]; /* pipes from master to each slave */ 5025219Smckusick int slavepid[SLAVES]; /* used by killall() */ 5125219Smckusick int rotor; /* next slave to be instructed */ 5225219Smckusick int master; /* pid of master, for sending error signals */ 5325219Smckusick int tenths; /* length of tape used per block written */ 5418012Smckusick 5546585Storek int 5610911Ssam alloctape() 5710911Ssam { 5825219Smckusick int pgoff = getpagesize() - 1; 5910911Ssam 6010911Ssam writesize = ntrec * TP_BSIZE; 6125219Smckusick reqsiz = ntrec * sizeof(struct req); 6224181Smckusick /* 6325219Smckusick * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 6425219Smckusick * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 6525219Smckusick * repositioning after stopping, i.e, streaming mode, where the gap is 6625219Smckusick * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 6724181Smckusick */ 6825219Smckusick tenths = writesize/density + (cartridge ? 16 : density == 625 ? 5 : 8); 6925219Smckusick /* 7025219Smckusick * Allocate tape buffer contiguous with the array of instruction 7125219Smckusick * packets, so flusht() can write them together with one write(). 7225219Smckusick * Align tape buffer on page boundary to speed up tape write(). 7325219Smckusick */ 7424181Smckusick req = (struct req *)malloc(reqsiz + writesize + pgoff); 7524181Smckusick if (req == NULL) 7624181Smckusick return(0); 7724181Smckusick tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff); 7825219Smckusick req = (struct req *)tblock - ntrec; 7924181Smckusick return(1); 8010911Ssam } 8110911Ssam 8225219Smckusick 8346585Storek void 841425Sroot taprec(dp) 855329Smckusic char *dp; 861425Sroot { 8718012Smckusick req[trecno].dblk = (daddr_t)0; 8818012Smckusick req[trecno].count = 1; 8924181Smckusick *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */ 9029899Smckusick lastspclrec = spcl.c_tapea; 9124181Smckusick trecno++; 921425Sroot spcl.c_tapea++; 9346585Storek if (trecno >= ntrec) 941425Sroot flusht(); 951425Sroot } 961425Sroot 9746585Storek void 984774Smckusic dmpblk(blkno, size) 994774Smckusic daddr_t blkno; 1004774Smckusic int size; 1011425Sroot { 10225219Smckusick int avail, tpblks, dblkno; 1031425Sroot 1045329Smckusic dblkno = fsbtodb(sblock, blkno); 10546585Storek tpblks = size >> tp_bshift; 10618012Smckusick while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { 10718012Smckusick req[trecno].dblk = dblkno; 10818012Smckusick req[trecno].count = avail; 10925219Smckusick trecno += avail; 1104774Smckusic spcl.c_tapea += avail; 11125219Smckusick if (trecno >= ntrec) 11218012Smckusick flusht(); 11346585Storek dblkno += avail << (tp_bshift - dev_bshift); 1145329Smckusic tpblks -= avail; 1154774Smckusic } 1161425Sroot } 1171425Sroot 1181425Sroot int nogripe = 0; 1191425Sroot 12046585Storek void 12146585Storek tperror() 12246585Storek { 12318012Smckusick if (pipeout) { 124*46614Smckusick msg("write error on %s\n", tape); 12546585Storek quit("Cannot recover\n"); 12618012Smckusick /* NOTREACHED */ 12718012Smckusick } 128*46614Smckusick msg("write error %d blocks into volume %d\n", blockswritten, tapeno); 129*46614Smckusick broadcast("DUMP WRITE ERROR!\n"); 13018012Smckusick if (!query("Do you want to restart?")) 13118012Smckusick dumpabort(); 132*46614Smckusick msg("Closing this volume. Prepare to restart with new media;\n"); 13318012Smckusick msg("this dump volume will be rewritten.\n"); 13424181Smckusick killall(); 13518012Smckusick nogripe = 1; 13618012Smckusick close_rewind(); 13718012Smckusick Exit(X_REWRITE); 13818012Smckusick } 13918012Smckusick 14046585Storek void 14125219Smckusick sigpipe() 14225219Smckusick { 14325219Smckusick 14446585Storek quit("Broken pipe\n"); 14525219Smckusick } 14625219Smckusick 14718012Smckusick #ifdef RDUMP 14825219Smckusick /* 14925219Smckusick * compatibility routine 15025219Smckusick */ 15146585Storek void 15225219Smckusick tflush(i) 15325219Smckusick int i; 15418012Smckusick { 15518012Smckusick 15618012Smckusick for (i = 0; i < ntrec; i++) 15718012Smckusick spclrec(); 15818012Smckusick } 15918012Smckusick #endif RDUMP 16018012Smckusick 16146585Storek void 1621425Sroot flusht() 1631425Sroot { 16425219Smckusick int siz = (char *)tblock - (char *)req; 1651425Sroot 16646585Storek if (atomic(write, slavefd[rotor], req, siz) != siz) 16746585Storek quit("error writing command pipe: %s\n", strerror(errno)); 16846585Storek if (++rotor >= SLAVES) 16946585Storek rotor = 0; 17018012Smckusick tblock = (char (*)[TP_BSIZE]) &req[ntrec]; 1711425Sroot trecno = 0; 17224181Smckusick asize += tenths; 17310911Ssam blockswritten += ntrec; 174*46614Smckusick if (!pipeout && (blocksperfile ? 175*46614Smckusick (blockswritten >= blocksperfile) : (asize > tsize))) { 1761425Sroot close_rewind(); 1771425Sroot otape(); 1781425Sroot } 1791425Sroot timeest(); 1801425Sroot } 1811425Sroot 18246585Storek void 18346239Storek trewind() 1841425Sroot { 18524181Smckusick int f; 18612331Smckusick 18712331Smckusick if (pipeout) 18812331Smckusick return; 18918012Smckusick for (f = 0; f < SLAVES; f++) 19018012Smckusick close(slavefd[f]); 19146585Storek while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ 19246585Storek /* void */; 19318012Smckusick msg("Tape rewinding\n"); 19418012Smckusick #ifdef RDUMP 19525219Smckusick if (host) { 19625219Smckusick rmtclose(); 19725219Smckusick while (rmtopen(tape, 0) < 0) 19825219Smckusick sleep(10); 19925219Smckusick rmtclose(); 20025219Smckusick return; 20125219Smckusick } 20225219Smckusick #endif RDUMP 2033214Swnj close(to); 2043214Swnj while ((f = open(tape, 0)) < 0) 2053214Swnj sleep (10); 2063214Swnj close(f); 2071425Sroot } 2081425Sroot 20946585Storek void 2101425Sroot close_rewind() 2111425Sroot { 21246239Storek trewind(); 21318012Smckusick if (!nogripe) { 214*46614Smckusick msg("Change Volumes: Mount volume #%d\n", tapeno+1); 215*46614Smckusick broadcast("CHANGE DUMP VOLUMES!\7\7\n"); 2161425Sroot } 217*46614Smckusick while (!query("Is the new volume mounted and ready to go?")) 21825219Smckusick if (query("Do you want to abort?")) { 2191425Sroot dumpabort(); 22025219Smckusick /*NOTREACHED*/ 22125219Smckusick } 2221425Sroot } 2231425Sroot 2241425Sroot /* 22518012Smckusick * We implement taking and restoring checkpoints on the tape level. 2261425Sroot * When each tape is opened, a new process is created by forking; this 2271425Sroot * saves all of the necessary context in the parent. The child 2281425Sroot * continues the dump; the parent waits around, saving the context. 2291425Sroot * If the child returns X_REWRITE, then it had problems writing that tape; 2301425Sroot * this causes the parent to fork again, duplicating the context, and 2311425Sroot * everything continues as if nothing had happened. 2321425Sroot */ 2331425Sroot 23446585Storek void 2351425Sroot otape() 2361425Sroot { 2371425Sroot int parentpid; 2381425Sroot int childpid; 2391425Sroot int status; 2401425Sroot int waitpid; 24139164Sbostic sig_t interrupt; 24229899Smckusick int blks, i; 2431425Sroot 24439164Sbostic interrupt = signal(SIGINT, SIG_IGN); 2451425Sroot parentpid = getpid(); 2461425Sroot 2471425Sroot restore_check_point: 24839164Sbostic (void)signal(SIGINT, interrupt); 24925219Smckusick /* 25025219Smckusick * All signals are inherited... 25125219Smckusick */ 2521425Sroot childpid = fork(); 25318012Smckusick if (childpid < 0) { 2541425Sroot msg("Context save fork fails in parent %d\n", parentpid); 2551425Sroot Exit(X_ABORT); 2561425Sroot } 25718012Smckusick if (childpid != 0) { 2581425Sroot /* 2591425Sroot * PARENT: 2601425Sroot * save the context by waiting 2611425Sroot * until the child doing all of the work returns. 26218012Smckusick * don't catch the interrupt 2631425Sroot */ 26425219Smckusick signal(SIGINT, SIG_IGN); 2651425Sroot #ifdef TDEBUG 2661425Sroot msg("Tape: %d; parent process: %d child process %d\n", 2671425Sroot tapeno+1, parentpid, childpid); 2681425Sroot #endif TDEBUG 26918012Smckusick while ((waitpid = wait(&status)) != childpid) 27018012Smckusick msg("Parent %d waiting for child %d has another child %d return\n", 27118012Smckusick parentpid, childpid, waitpid); 27218012Smckusick if (status & 0xFF) { 2731425Sroot msg("Child %d returns LOB status %o\n", 2741425Sroot childpid, status&0xFF); 2751425Sroot } 2761425Sroot status = (status >> 8) & 0xFF; 2771425Sroot #ifdef TDEBUG 27818012Smckusick switch(status) { 2791425Sroot case X_FINOK: 2801425Sroot msg("Child %d finishes X_FINOK\n", childpid); 2811425Sroot break; 2821425Sroot case X_ABORT: 2831425Sroot msg("Child %d finishes X_ABORT\n", childpid); 2841425Sroot break; 2851425Sroot case X_REWRITE: 2861425Sroot msg("Child %d finishes X_REWRITE\n", childpid); 2871425Sroot break; 2881425Sroot default: 28918012Smckusick msg("Child %d finishes unknown %d\n", 29025219Smckusick childpid, status); 2911425Sroot break; 2921425Sroot } 2931425Sroot #endif TDEBUG 29418012Smckusick switch(status) { 2951425Sroot case X_FINOK: 2961425Sroot Exit(X_FINOK); 2971425Sroot case X_ABORT: 2981425Sroot Exit(X_ABORT); 2991425Sroot case X_REWRITE: 3001425Sroot goto restore_check_point; 3011425Sroot default: 3021425Sroot msg("Bad return code from dump: %d\n", status); 3031425Sroot Exit(X_ABORT); 3041425Sroot } 3051425Sroot /*NOTREACHED*/ 3061425Sroot } else { /* we are the child; just continue */ 3071425Sroot #ifdef TDEBUG 3081425Sroot sleep(4); /* allow time for parent's message to get out */ 3091425Sroot msg("Child on Tape %d has parent %d, my pid = %d\n", 3101425Sroot tapeno+1, parentpid, getpid()); 31125219Smckusick #endif TDEBUG 31218012Smckusick #ifdef RDUMP 31325219Smckusick while ((to = (host ? rmtopen(tape, 2) : 31425219Smckusick pipeout ? 1 : creat(tape, 0666))) < 0) 31525219Smckusick #else RDUMP 31618012Smckusick while ((to = pipeout ? 1 : creat(tape, 0666)) < 0) 31724181Smckusick #endif RDUMP 31839128Smckusick { 319*46614Smckusick msg("Cannot open output \"%s\".\n", tape); 32039128Smckusick if (!query("Do you want to retry the open?")) 32118012Smckusick dumpabort(); 32239128Smckusick } 3231425Sroot 32418012Smckusick enslave(); /* Share open tape file descriptor with slaves */ 32518012Smckusick 3261425Sroot asize = 0; 3271425Sroot tapeno++; /* current tape sequence */ 3281425Sroot newtape++; /* new tape signal */ 32929899Smckusick blks = 0; 33029899Smckusick if (spcl.c_type != TS_END) 33129899Smckusick for (i = 0; i < spcl.c_count; i++) 33229899Smckusick if (spcl.c_addr[i] != 0) 33329899Smckusick blks++; 33429899Smckusick spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec; 3351425Sroot spcl.c_volume++; 3361425Sroot spcl.c_type = TS_TAPE; 33730432Smckusick spcl.c_flags |= DR_NEWHEADER; 3381425Sroot spclrec(); 33930432Smckusick spcl.c_flags &=~ DR_NEWHEADER; 3401425Sroot if (tapeno > 1) 3411425Sroot msg("Tape %d begins with blocks from ino %d\n", 3421425Sroot tapeno, ino); 3431425Sroot } 3441425Sroot } 3451425Sroot 34646585Storek void 3471425Sroot dumpabort() 3481425Sroot { 34918012Smckusick if (master != 0 && master != getpid()) 35025219Smckusick kill(master, SIGTERM); /* Signals master to call dumpabort */ 35124181Smckusick else { 35224181Smckusick killall(); 35324181Smckusick msg("The ENTIRE dump is aborted.\n"); 35424181Smckusick } 3551425Sroot Exit(X_ABORT); 3561425Sroot } 3571425Sroot 35846585Storek void 3591425Sroot Exit(status) 36046239Storek int status; 3611425Sroot { 3621425Sroot #ifdef TDEBUG 3631425Sroot msg("pid = %d exits with status %d\n", getpid(), status); 3641425Sroot #endif TDEBUG 3651925Swnj exit(status); 3661425Sroot } 36718012Smckusick 36824181Smckusick /* 36925219Smckusick * could use pipe() for this if flock() worked on pipes 37024181Smckusick */ 37146585Storek void 37224181Smckusick lockfile(fd) 37324181Smckusick int fd[2]; 37424181Smckusick { 37524181Smckusick char tmpname[20]; 37618012Smckusick 37739128Smckusick strcpy(tmpname, _PATH_LOCK); 37824181Smckusick mktemp(tmpname); 37946585Storek if ((fd[1] = creat(tmpname, 0400)) < 0) 38046585Storek quit("cannot create lockfile %s: %s\n", 38146585Storek tmpname, strerror(errno)); 38246585Storek if ((fd[0] = open(tmpname, 0)) < 0) 38346585Storek quit("cannot reopen lockfile %s: %s\n", 38446585Storek tmpname, strerror(errno)); 38546585Storek (void) unlink(tmpname); 38624181Smckusick } 38724181Smckusick 38846585Storek void 38918012Smckusick enslave() 39018012Smckusick { 39124181Smckusick int first[2], prev[2], next[2], cmd[2]; /* file descriptors */ 39224181Smckusick register int i, j; 39318012Smckusick 39418012Smckusick master = getpid(); 39525219Smckusick signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ 39625219Smckusick signal(SIGPIPE, sigpipe); 39725219Smckusick signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ 39824181Smckusick lockfile(first); 39924181Smckusick for (i = 0; i < SLAVES; i++) { 40024181Smckusick if (i == 0) { 40124181Smckusick prev[0] = first[1]; 40224181Smckusick prev[1] = first[0]; 40324181Smckusick } else { 40424181Smckusick prev[0] = next[0]; 40524181Smckusick prev[1] = next[1]; 40624181Smckusick flock(prev[1], LOCK_EX); 40724181Smckusick } 40826485Smckusick if (i < SLAVES - 1) { 40926485Smckusick lockfile(next); 41026485Smckusick } else { 41126485Smckusick next[0] = first[0]; 41226485Smckusick next[1] = first[1]; /* Last slave loops back */ 41326485Smckusick } 41446585Storek if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) 41546585Storek quit("too many slaves, %d (recompile smaller): %s\n", 41646585Storek i, strerror(errno)); 41718012Smckusick slavefd[i] = cmd[1]; 41825219Smckusick if (slavepid[i] == 0) { /* Slave starts up here */ 41918012Smckusick for (j = 0; j <= i; j++) 42018012Smckusick close(slavefd[j]); 42125219Smckusick signal(SIGINT, SIG_IGN); /* Master handles this */ 42225219Smckusick doslave(cmd[0], prev, next); 42318012Smckusick Exit(X_FINOK); 42418012Smckusick } 42518012Smckusick close(cmd[0]); 42624181Smckusick if (i > 0) { 42724181Smckusick close(prev[0]); 42824181Smckusick close(prev[1]); 42924181Smckusick } 43018012Smckusick } 43124181Smckusick close(first[0]); 43224181Smckusick close(first[1]); 43324181Smckusick master = 0; rotor = 0; 43418012Smckusick } 43518012Smckusick 43646585Storek void 43724181Smckusick killall() 43818012Smckusick { 43924181Smckusick register int i; 44019982Smckusick 44124181Smckusick for (i = 0; i < SLAVES; i++) 44224181Smckusick if (slavepid[i] > 0) 44324181Smckusick kill(slavepid[i], SIGKILL); 44418012Smckusick } 44518012Smckusick 44624181Smckusick /* 44724181Smckusick * Synchronization - each process has a lockfile, and shares file 44824181Smckusick * descriptors to the following process's lockfile. When our write 44924181Smckusick * completes, we release our lock on the following process's lock- 45024181Smckusick * file, allowing the following process to lock it and proceed. We 45124181Smckusick * get the lock back for the next cycle by swapping descriptors. 45224181Smckusick */ 45346585Storek void 45425219Smckusick doslave(cmd, prev, next) 45525219Smckusick register int cmd, prev[2], next[2]; 45619982Smckusick { 45725219Smckusick register int nread, toggle = 0; 45819982Smckusick 45918012Smckusick close(fi); 46046585Storek if ((fi = open(disk, 0)) < 0) /* Need our own seek pointer */ 46146585Storek quit("slave couldn't reopen disk: %s\n", strerror(errno)); 46224181Smckusick /* 46325219Smckusick * Get list of blocks to dump, read the blocks into tape buffer 46424181Smckusick */ 46525219Smckusick while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) { 46618012Smckusick register struct req *p = req; 46718012Smckusick for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) { 46818012Smckusick if (p->dblk) { 46918012Smckusick bread(p->dblk, tblock[trecno], 47025219Smckusick p->count * TP_BSIZE); 47118012Smckusick } else { 47225219Smckusick if (p->count != 1 || atomic(read, cmd, 47346585Storek tblock[trecno], TP_BSIZE) != TP_BSIZE) 47446585Storek quit("master/slave protocol botched.\n"); 47518012Smckusick } 47618012Smckusick } 47724181Smckusick flock(prev[toggle], LOCK_EX); /* Wait our turn */ 47825219Smckusick 47918012Smckusick #ifdef RDUMP 48025219Smckusick if ((host ? rmtwrite(tblock[0], writesize) 48125219Smckusick : write(to, tblock[0], writesize)) != writesize) { 48225219Smckusick #else RDUMP 48325219Smckusick if (write(to, tblock[0], writesize) != writesize) { 48424181Smckusick #endif RDUMP 48525219Smckusick kill(master, SIGUSR1); 48625219Smckusick for (;;) 48725219Smckusick sigpause(0); 48818012Smckusick } 48924181Smckusick toggle ^= 1; 49024181Smckusick flock(next[toggle], LOCK_UN); /* Next slave's turn */ 49124181Smckusick } /* Also jolts him awake */ 49246585Storek if (nread != 0) 49346585Storek quit("error reading command pipe: %s\n", strerror(errno)); 49418012Smckusick } 49519947Smckusick 49619947Smckusick /* 49725219Smckusick * Since a read from a pipe may not return all we asked for, 49825219Smckusick * or a write may not write all we ask if we get a signal, 49925219Smckusick * loop until the count is satisfied (or error). 50019947Smckusick */ 50146585Storek int 50225219Smckusick atomic(func, fd, buf, count) 50325219Smckusick int (*func)(), fd, count; 50419947Smckusick char *buf; 50519947Smckusick { 50625219Smckusick int got, need = count; 50719947Smckusick 50825219Smckusick while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) 50919947Smckusick buf += got; 51025219Smckusick return (got < 0 ? got : count - need); 51119947Smckusick } 512