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*39128Smckusick static char sccsid[] = "@(#)tape.c 5.9 (Berkeley) 09/12/89"; 922040Sdist #endif not lint 1017527Ssam 1125219Smckusick #include <sys/file.h> 121425Sroot #include "dump.h" 13*39128Smckusick #include "pathnames.h" 141425Sroot 1529899Smckusick char (*tblock)[TP_BSIZE]; /* pointer to malloc()ed buffer for tape */ 1629899Smckusick int writesize; /* size of malloc()ed buffer for tape */ 1729899Smckusick long lastspclrec = -1; /* tape block number of last written header */ 1829899Smckusick int trecno = 0; /* next record to write in current block */ 1925219Smckusick extern int ntrec; /* blocking factor on tape */ 2025219Smckusick extern int cartridge; 2125219Smckusick extern int read(), write(); 2225219Smckusick #ifdef RDUMP 2325219Smckusick extern char *host; 2425219Smckusick #endif RDUMP 251425Sroot 2610911Ssam /* 2724181Smckusick * Concurrent dump mods (Caltech) - disk block reading and tape writing 2818012Smckusick * are exported to several slave processes. While one slave writes the 2918012Smckusick * tape, the others read disk blocks; they pass control of the tape in 3024181Smckusick * a ring via flock(). The parent process traverses the filesystem and 3125219Smckusick * sends spclrec()'s and lists of daddr's to the slaves via pipes. 3210911Ssam */ 3318012Smckusick struct req { /* instruction packets sent to slaves */ 3418012Smckusick daddr_t dblk; 3518012Smckusick int count; 3618012Smckusick } *req; 3718012Smckusick int reqsiz; 3818012Smckusick 3924181Smckusick #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 4025219Smckusick int slavefd[SLAVES]; /* pipes from master to each slave */ 4125219Smckusick int slavepid[SLAVES]; /* used by killall() */ 4225219Smckusick int rotor; /* next slave to be instructed */ 4325219Smckusick int master; /* pid of master, for sending error signals */ 4425219Smckusick int tenths; /* length of tape used per block written */ 4518012Smckusick 4610911Ssam alloctape() 4710911Ssam { 4825219Smckusick int pgoff = getpagesize() - 1; 4910911Ssam 5010911Ssam writesize = ntrec * TP_BSIZE; 5125219Smckusick reqsiz = ntrec * sizeof(struct req); 5224181Smckusick /* 5325219Smckusick * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 5425219Smckusick * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 5525219Smckusick * repositioning after stopping, i.e, streaming mode, where the gap is 5625219Smckusick * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 5724181Smckusick */ 5825219Smckusick tenths = writesize/density + (cartridge ? 16 : density == 625 ? 5 : 8); 5925219Smckusick /* 6025219Smckusick * Allocate tape buffer contiguous with the array of instruction 6125219Smckusick * packets, so flusht() can write them together with one write(). 6225219Smckusick * Align tape buffer on page boundary to speed up tape write(). 6325219Smckusick */ 6424181Smckusick req = (struct req *)malloc(reqsiz + writesize + pgoff); 6524181Smckusick if (req == NULL) 6624181Smckusick return(0); 6724181Smckusick tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff); 6825219Smckusick req = (struct req *)tblock - ntrec; 6924181Smckusick return(1); 7010911Ssam } 7110911Ssam 7225219Smckusick 731425Sroot taprec(dp) 745329Smckusic char *dp; 751425Sroot { 7618012Smckusick req[trecno].dblk = (daddr_t)0; 7718012Smckusick req[trecno].count = 1; 7824181Smckusick *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */ 7929899Smckusick lastspclrec = spcl.c_tapea; 8024181Smckusick trecno++; 811425Sroot spcl.c_tapea++; 8224181Smckusick if(trecno >= ntrec) 831425Sroot flusht(); 841425Sroot } 851425Sroot 864774Smckusic dmpblk(blkno, size) 874774Smckusic daddr_t blkno; 884774Smckusic int size; 891425Sroot { 9025219Smckusick int avail, tpblks, dblkno; 911425Sroot 925329Smckusic dblkno = fsbtodb(sblock, blkno); 9318012Smckusick tpblks = size / TP_BSIZE; 9418012Smckusick while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { 9518012Smckusick req[trecno].dblk = dblkno; 9618012Smckusick req[trecno].count = avail; 9725219Smckusick trecno += avail; 984774Smckusic spcl.c_tapea += avail; 9925219Smckusick if (trecno >= ntrec) 10018012Smckusick flusht(); 10130560Smckusick dblkno += avail * (TP_BSIZE / dev_bsize); 1025329Smckusic tpblks -= avail; 1034774Smckusic } 1041425Sroot } 1051425Sroot 1061425Sroot int nogripe = 0; 1071425Sroot 10818012Smckusick tperror() { 10918012Smckusick if (pipeout) { 11018012Smckusick msg("Tape write error on %s\n", tape); 11118012Smckusick msg("Cannot recover\n"); 11218012Smckusick dumpabort(); 11318012Smckusick /* NOTREACHED */ 11418012Smckusick } 11525219Smckusick msg("Tape write error %d feet into tape %d\n", asize/120L, tapeno); 11618012Smckusick broadcast("TAPE ERROR!\n"); 11718012Smckusick if (!query("Do you want to restart?")) 11818012Smckusick dumpabort(); 11918012Smckusick msg("This tape will rewind. After it is rewound,\n"); 12018012Smckusick msg("replace the faulty tape with a new one;\n"); 12118012Smckusick msg("this dump volume will be rewritten.\n"); 12224181Smckusick killall(); 12318012Smckusick nogripe = 1; 12418012Smckusick close_rewind(); 12518012Smckusick Exit(X_REWRITE); 12618012Smckusick } 12718012Smckusick 12825219Smckusick sigpipe() 12925219Smckusick { 13025219Smckusick 13125219Smckusick msg("Broken pipe\n"); 13225219Smckusick dumpabort(); 13325219Smckusick } 13425219Smckusick 13518012Smckusick #ifdef RDUMP 13625219Smckusick /* 13725219Smckusick * compatibility routine 13825219Smckusick */ 13925219Smckusick tflush(i) 14025219Smckusick int i; 14118012Smckusick { 14218012Smckusick 14318012Smckusick for (i = 0; i < ntrec; i++) 14418012Smckusick spclrec(); 14518012Smckusick } 14618012Smckusick #endif RDUMP 14718012Smckusick 1481425Sroot flusht() 1491425Sroot { 15025219Smckusick int siz = (char *)tblock - (char *)req; 1511425Sroot 15225219Smckusick if (atomic(write, slavefd[rotor], req, siz) != siz) { 15325219Smckusick perror(" DUMP: error writing command pipe"); 15424181Smckusick dumpabort(); 15524181Smckusick } 15618012Smckusick if (++rotor >= SLAVES) rotor = 0; 15718012Smckusick tblock = (char (*)[TP_BSIZE]) &req[ntrec]; 1581425Sroot trecno = 0; 15924181Smckusick asize += tenths; 16010911Ssam blockswritten += ntrec; 16112331Smckusick if (!pipeout && asize > tsize) { 1621425Sroot close_rewind(); 1631425Sroot otape(); 1641425Sroot } 1651425Sroot timeest(); 1661425Sroot } 1671425Sroot 1681425Sroot rewind() 1691425Sroot { 17024181Smckusick int f; 17112331Smckusick 17212331Smckusick if (pipeout) 17312331Smckusick return; 17418012Smckusick for (f = 0; f < SLAVES; f++) 17518012Smckusick close(slavefd[f]); 17618012Smckusick while (wait(NULL) >= 0) ; /* wait for any signals from slaves */ 17718012Smckusick msg("Tape rewinding\n"); 17818012Smckusick #ifdef RDUMP 17925219Smckusick if (host) { 18025219Smckusick rmtclose(); 18125219Smckusick while (rmtopen(tape, 0) < 0) 18225219Smckusick sleep(10); 18325219Smckusick rmtclose(); 18425219Smckusick return; 18525219Smckusick } 18625219Smckusick #endif RDUMP 1873214Swnj close(to); 1883214Swnj while ((f = open(tape, 0)) < 0) 1893214Swnj sleep (10); 1903214Swnj close(f); 1911425Sroot } 1921425Sroot 1931425Sroot close_rewind() 1941425Sroot { 19518012Smckusick rewind(); 19618012Smckusick if (!nogripe) { 1971425Sroot msg("Change Tapes: Mount tape #%d\n", tapeno+1); 1981425Sroot broadcast("CHANGE TAPES!\7\7\n"); 1991425Sroot } 20018012Smckusick while (!query("Is the new tape mounted and ready to go?")) 20125219Smckusick if (query("Do you want to abort?")) { 2021425Sroot dumpabort(); 20325219Smckusick /*NOTREACHED*/ 20425219Smckusick } 2051425Sroot } 2061425Sroot 2071425Sroot /* 20818012Smckusick * We implement taking and restoring checkpoints on the tape level. 2091425Sroot * When each tape is opened, a new process is created by forking; this 2101425Sroot * saves all of the necessary context in the parent. The child 2111425Sroot * continues the dump; the parent waits around, saving the context. 2121425Sroot * If the child returns X_REWRITE, then it had problems writing that tape; 2131425Sroot * this causes the parent to fork again, duplicating the context, and 2141425Sroot * everything continues as if nothing had happened. 2151425Sroot */ 2161425Sroot 2171425Sroot otape() 2181425Sroot { 2191425Sroot int parentpid; 2201425Sroot int childpid; 2211425Sroot int status; 2221425Sroot int waitpid; 22325219Smckusick int (*interrupt)() = signal(SIGINT, SIG_IGN); 22429899Smckusick int blks, i; 2251425Sroot 2261425Sroot parentpid = getpid(); 2271425Sroot 2281425Sroot restore_check_point: 22925219Smckusick signal(SIGINT, interrupt); 23025219Smckusick /* 23125219Smckusick * All signals are inherited... 23225219Smckusick */ 2331425Sroot childpid = fork(); 23418012Smckusick if (childpid < 0) { 2351425Sroot msg("Context save fork fails in parent %d\n", parentpid); 2361425Sroot Exit(X_ABORT); 2371425Sroot } 23818012Smckusick if (childpid != 0) { 2391425Sroot /* 2401425Sroot * PARENT: 2411425Sroot * save the context by waiting 2421425Sroot * until the child doing all of the work returns. 24318012Smckusick * don't catch the interrupt 2441425Sroot */ 24525219Smckusick signal(SIGINT, SIG_IGN); 2461425Sroot #ifdef TDEBUG 2471425Sroot msg("Tape: %d; parent process: %d child process %d\n", 2481425Sroot tapeno+1, parentpid, childpid); 2491425Sroot #endif TDEBUG 25018012Smckusick while ((waitpid = wait(&status)) != childpid) 25118012Smckusick msg("Parent %d waiting for child %d has another child %d return\n", 25218012Smckusick parentpid, childpid, waitpid); 25318012Smckusick if (status & 0xFF) { 2541425Sroot msg("Child %d returns LOB status %o\n", 2551425Sroot childpid, status&0xFF); 2561425Sroot } 2571425Sroot status = (status >> 8) & 0xFF; 2581425Sroot #ifdef TDEBUG 25918012Smckusick switch(status) { 2601425Sroot case X_FINOK: 2611425Sroot msg("Child %d finishes X_FINOK\n", childpid); 2621425Sroot break; 2631425Sroot case X_ABORT: 2641425Sroot msg("Child %d finishes X_ABORT\n", childpid); 2651425Sroot break; 2661425Sroot case X_REWRITE: 2671425Sroot msg("Child %d finishes X_REWRITE\n", childpid); 2681425Sroot break; 2691425Sroot default: 27018012Smckusick msg("Child %d finishes unknown %d\n", 27125219Smckusick childpid, status); 2721425Sroot break; 2731425Sroot } 2741425Sroot #endif TDEBUG 27518012Smckusick switch(status) { 2761425Sroot case X_FINOK: 2771425Sroot Exit(X_FINOK); 2781425Sroot case X_ABORT: 2791425Sroot Exit(X_ABORT); 2801425Sroot case X_REWRITE: 2811425Sroot goto restore_check_point; 2821425Sroot default: 2831425Sroot msg("Bad return code from dump: %d\n", status); 2841425Sroot Exit(X_ABORT); 2851425Sroot } 2861425Sroot /*NOTREACHED*/ 2871425Sroot } else { /* we are the child; just continue */ 2881425Sroot #ifdef TDEBUG 2891425Sroot sleep(4); /* allow time for parent's message to get out */ 2901425Sroot msg("Child on Tape %d has parent %d, my pid = %d\n", 2911425Sroot tapeno+1, parentpid, getpid()); 29225219Smckusick #endif TDEBUG 29318012Smckusick #ifdef RDUMP 29425219Smckusick while ((to = (host ? rmtopen(tape, 2) : 29525219Smckusick pipeout ? 1 : creat(tape, 0666))) < 0) 29625219Smckusick #else RDUMP 29718012Smckusick while ((to = pipeout ? 1 : creat(tape, 0666)) < 0) 29824181Smckusick #endif RDUMP 299*39128Smckusick { 300*39128Smckusick msg("Cannot open tape \"%s\".\n", tape); 301*39128Smckusick if (!query("Do you want to retry the open?")) 30218012Smckusick dumpabort(); 303*39128Smckusick } 3041425Sroot 30518012Smckusick enslave(); /* Share open tape file descriptor with slaves */ 30618012Smckusick 3071425Sroot asize = 0; 3081425Sroot tapeno++; /* current tape sequence */ 3091425Sroot newtape++; /* new tape signal */ 31029899Smckusick blks = 0; 31129899Smckusick if (spcl.c_type != TS_END) 31229899Smckusick for (i = 0; i < spcl.c_count; i++) 31329899Smckusick if (spcl.c_addr[i] != 0) 31429899Smckusick blks++; 31529899Smckusick spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec; 3161425Sroot spcl.c_volume++; 3171425Sroot spcl.c_type = TS_TAPE; 31830432Smckusick spcl.c_flags |= DR_NEWHEADER; 3191425Sroot spclrec(); 32030432Smckusick spcl.c_flags &=~ DR_NEWHEADER; 3211425Sroot if (tapeno > 1) 3221425Sroot msg("Tape %d begins with blocks from ino %d\n", 3231425Sroot tapeno, ino); 3241425Sroot } 3251425Sroot } 3261425Sroot 3271425Sroot dumpabort() 3281425Sroot { 32918012Smckusick if (master != 0 && master != getpid()) 33025219Smckusick kill(master, SIGTERM); /* Signals master to call dumpabort */ 33124181Smckusick else { 33224181Smckusick killall(); 33324181Smckusick msg("The ENTIRE dump is aborted.\n"); 33424181Smckusick } 3351425Sroot Exit(X_ABORT); 3361425Sroot } 3371425Sroot 3381425Sroot Exit(status) 3391425Sroot { 3401425Sroot #ifdef TDEBUG 3411425Sroot msg("pid = %d exits with status %d\n", getpid(), status); 3421425Sroot #endif TDEBUG 3431925Swnj exit(status); 3441425Sroot } 34518012Smckusick 34624181Smckusick /* 34725219Smckusick * could use pipe() for this if flock() worked on pipes 34824181Smckusick */ 34924181Smckusick lockfile(fd) 35024181Smckusick int fd[2]; 35124181Smckusick { 35224181Smckusick char tmpname[20]; 35318012Smckusick 354*39128Smckusick strcpy(tmpname, _PATH_LOCK); 35524181Smckusick mktemp(tmpname); 35626485Smckusick if ((fd[1] = creat(tmpname, 0400)) < 0) { 35726485Smckusick msg("Could not create lockfile "); 35826485Smckusick perror(tmpname); 35926485Smckusick dumpabort(); 36026485Smckusick } 36126485Smckusick if ((fd[0] = open(tmpname, 0)) < 0) { 36226485Smckusick msg("Could not reopen lockfile "); 36326485Smckusick perror(tmpname); 36426485Smckusick dumpabort(); 36526485Smckusick } 36624181Smckusick unlink(tmpname); 36724181Smckusick } 36824181Smckusick 36918012Smckusick enslave() 37018012Smckusick { 37124181Smckusick int first[2], prev[2], next[2], cmd[2]; /* file descriptors */ 37224181Smckusick register int i, j; 37318012Smckusick 37418012Smckusick master = getpid(); 37525219Smckusick signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ 37625219Smckusick signal(SIGPIPE, sigpipe); 37725219Smckusick signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ 37824181Smckusick lockfile(first); 37924181Smckusick for (i = 0; i < SLAVES; i++) { 38024181Smckusick if (i == 0) { 38124181Smckusick prev[0] = first[1]; 38224181Smckusick prev[1] = first[0]; 38324181Smckusick } else { 38424181Smckusick prev[0] = next[0]; 38524181Smckusick prev[1] = next[1]; 38624181Smckusick flock(prev[1], LOCK_EX); 38724181Smckusick } 38826485Smckusick if (i < SLAVES - 1) { 38926485Smckusick lockfile(next); 39026485Smckusick } else { 39126485Smckusick next[0] = first[0]; 39226485Smckusick next[1] = first[1]; /* Last slave loops back */ 39326485Smckusick } 39426485Smckusick if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) { 39526485Smckusick msg("too many slaves, %d (recompile smaller) ", i); 39626485Smckusick perror(""); 39718012Smckusick dumpabort(); 39818012Smckusick } 39918012Smckusick slavefd[i] = cmd[1]; 40025219Smckusick if (slavepid[i] == 0) { /* Slave starts up here */ 40118012Smckusick for (j = 0; j <= i; j++) 40218012Smckusick close(slavefd[j]); 40325219Smckusick signal(SIGINT, SIG_IGN); /* Master handles this */ 40425219Smckusick doslave(cmd[0], prev, next); 40518012Smckusick Exit(X_FINOK); 40618012Smckusick } 40718012Smckusick close(cmd[0]); 40824181Smckusick if (i > 0) { 40924181Smckusick close(prev[0]); 41024181Smckusick close(prev[1]); 41124181Smckusick } 41218012Smckusick } 41324181Smckusick close(first[0]); 41424181Smckusick close(first[1]); 41524181Smckusick master = 0; rotor = 0; 41618012Smckusick } 41718012Smckusick 41824181Smckusick killall() 41918012Smckusick { 42024181Smckusick register int i; 42119982Smckusick 42224181Smckusick for (i = 0; i < SLAVES; i++) 42324181Smckusick if (slavepid[i] > 0) 42424181Smckusick kill(slavepid[i], SIGKILL); 42518012Smckusick } 42618012Smckusick 42724181Smckusick /* 42824181Smckusick * Synchronization - each process has a lockfile, and shares file 42924181Smckusick * descriptors to the following process's lockfile. When our write 43024181Smckusick * completes, we release our lock on the following process's lock- 43124181Smckusick * file, allowing the following process to lock it and proceed. We 43224181Smckusick * get the lock back for the next cycle by swapping descriptors. 43324181Smckusick */ 43425219Smckusick doslave(cmd, prev, next) 43525219Smckusick register int cmd, prev[2], next[2]; 43619982Smckusick { 43725219Smckusick register int nread, toggle = 0; 43819982Smckusick 43918012Smckusick close(fi); 44025219Smckusick if ((fi = open(disk, 0)) < 0) { /* Need our own seek pointer */ 44124181Smckusick perror(" DUMP: slave couldn't reopen disk"); 44225219Smckusick dumpabort(); 44318012Smckusick } 44424181Smckusick /* 44525219Smckusick * Get list of blocks to dump, read the blocks into tape buffer 44624181Smckusick */ 44725219Smckusick while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) { 44818012Smckusick register struct req *p = req; 44918012Smckusick for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) { 45018012Smckusick if (p->dblk) { 45118012Smckusick bread(p->dblk, tblock[trecno], 45225219Smckusick p->count * TP_BSIZE); 45318012Smckusick } else { 45425219Smckusick if (p->count != 1 || atomic(read, cmd, 45525219Smckusick tblock[trecno], TP_BSIZE) != TP_BSIZE) { 45628644Smckusick msg("Master/slave protocol botched.\n"); 45724181Smckusick dumpabort(); 45824181Smckusick } 45918012Smckusick } 46018012Smckusick } 46124181Smckusick flock(prev[toggle], LOCK_EX); /* Wait our turn */ 46225219Smckusick 46318012Smckusick #ifdef RDUMP 46425219Smckusick if ((host ? rmtwrite(tblock[0], writesize) 46525219Smckusick : write(to, tblock[0], writesize)) != writesize) { 46625219Smckusick #else RDUMP 46725219Smckusick if (write(to, tblock[0], writesize) != writesize) { 46824181Smckusick #endif RDUMP 46925219Smckusick kill(master, SIGUSR1); 47025219Smckusick for (;;) 47125219Smckusick sigpause(0); 47218012Smckusick } 47324181Smckusick toggle ^= 1; 47424181Smckusick flock(next[toggle], LOCK_UN); /* Next slave's turn */ 47524181Smckusick } /* Also jolts him awake */ 47625219Smckusick if (nread != 0) { 47725219Smckusick perror(" DUMP: error reading command pipe"); 47825219Smckusick dumpabort(); 47918012Smckusick } 48018012Smckusick } 48119947Smckusick 48219947Smckusick /* 48325219Smckusick * Since a read from a pipe may not return all we asked for, 48425219Smckusick * or a write may not write all we ask if we get a signal, 48525219Smckusick * loop until the count is satisfied (or error). 48619947Smckusick */ 48725219Smckusick atomic(func, fd, buf, count) 48825219Smckusick int (*func)(), fd, count; 48919947Smckusick char *buf; 49019947Smckusick { 49125219Smckusick int got, need = count; 49219947Smckusick 49325219Smckusick while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) 49419947Smckusick buf += got; 49525219Smckusick return (got < 0 ? got : count - need); 49619947Smckusick } 497