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*46585Storek static char sccsid[] = "@(#)tape.c 5.12 (Berkeley) 02/23/91"; 9*46585Storek #endif /* not lint */ 1017527Ssam 11*46585Storek #include "dump.h" 1246239Storek #include <sys/types.h> 13*46585Storek #include <sys/wait.h> 14*46585Storek #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 */ 2225219Smckusick extern int ntrec; /* blocking factor on tape */ 2325219Smckusick extern int cartridge; 2425219Smckusick extern int read(), write(); 2525219Smckusick #ifdef RDUMP 2625219Smckusick extern char *host; 27*46585Storek int rmtopen(), rmtwrite(); 28*46585Storek void rmtclose(); 2925219Smckusick #endif RDUMP 301425Sroot 31*46585Storek int atomic(); 32*46585Storek void doslave(), enslave(), flusht(), killall(); 33*46585Storek 3410911Ssam /* 3524181Smckusick * Concurrent dump mods (Caltech) - disk block reading and tape writing 3618012Smckusick * are exported to several slave processes. While one slave writes the 3718012Smckusick * tape, the others read disk blocks; they pass control of the tape in 3824181Smckusick * a ring via flock(). The parent process traverses the filesystem and 3925219Smckusick * sends spclrec()'s and lists of daddr's to the slaves via pipes. 4010911Ssam */ 4118012Smckusick struct req { /* instruction packets sent to slaves */ 4218012Smckusick daddr_t dblk; 4318012Smckusick int count; 4418012Smckusick } *req; 4518012Smckusick int reqsiz; 4618012Smckusick 4724181Smckusick #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 4825219Smckusick int slavefd[SLAVES]; /* pipes from master to each slave */ 4925219Smckusick int slavepid[SLAVES]; /* used by killall() */ 5025219Smckusick int rotor; /* next slave to be instructed */ 5125219Smckusick int master; /* pid of master, for sending error signals */ 5225219Smckusick int tenths; /* length of tape used per block written */ 5318012Smckusick 54*46585Storek int 5510911Ssam alloctape() 5610911Ssam { 5725219Smckusick int pgoff = getpagesize() - 1; 5810911Ssam 5910911Ssam writesize = ntrec * TP_BSIZE; 6025219Smckusick reqsiz = ntrec * sizeof(struct req); 6124181Smckusick /* 6225219Smckusick * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 6325219Smckusick * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 6425219Smckusick * repositioning after stopping, i.e, streaming mode, where the gap is 6525219Smckusick * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 6624181Smckusick */ 6725219Smckusick tenths = writesize/density + (cartridge ? 16 : density == 625 ? 5 : 8); 6825219Smckusick /* 6925219Smckusick * Allocate tape buffer contiguous with the array of instruction 7025219Smckusick * packets, so flusht() can write them together with one write(). 7125219Smckusick * Align tape buffer on page boundary to speed up tape write(). 7225219Smckusick */ 7324181Smckusick req = (struct req *)malloc(reqsiz + writesize + pgoff); 7424181Smckusick if (req == NULL) 7524181Smckusick return(0); 7624181Smckusick tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff); 7725219Smckusick req = (struct req *)tblock - ntrec; 7824181Smckusick return(1); 7910911Ssam } 8010911Ssam 8125219Smckusick 82*46585Storek void 831425Sroot taprec(dp) 845329Smckusic char *dp; 851425Sroot { 8618012Smckusick req[trecno].dblk = (daddr_t)0; 8718012Smckusick req[trecno].count = 1; 8824181Smckusick *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */ 8929899Smckusick lastspclrec = spcl.c_tapea; 9024181Smckusick trecno++; 911425Sroot spcl.c_tapea++; 92*46585Storek if (trecno >= ntrec) 931425Sroot flusht(); 941425Sroot } 951425Sroot 96*46585Storek void 974774Smckusic dmpblk(blkno, size) 984774Smckusic daddr_t blkno; 994774Smckusic int size; 1001425Sroot { 10125219Smckusick int avail, tpblks, dblkno; 1021425Sroot 1035329Smckusic dblkno = fsbtodb(sblock, blkno); 104*46585Storek tpblks = size >> tp_bshift; 10518012Smckusick while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { 10618012Smckusick req[trecno].dblk = dblkno; 10718012Smckusick req[trecno].count = avail; 10825219Smckusick trecno += avail; 1094774Smckusic spcl.c_tapea += avail; 11025219Smckusick if (trecno >= ntrec) 11118012Smckusick flusht(); 112*46585Storek dblkno += avail << (tp_bshift - dev_bshift); 1135329Smckusic tpblks -= avail; 1144774Smckusic } 1151425Sroot } 1161425Sroot 1171425Sroot int nogripe = 0; 1181425Sroot 119*46585Storek void 120*46585Storek tperror() 121*46585Storek { 12218012Smckusick if (pipeout) { 12318012Smckusick msg("Tape write error on %s\n", tape); 124*46585Storek quit("Cannot recover\n"); 12518012Smckusick /* NOTREACHED */ 12618012Smckusick } 12725219Smckusick msg("Tape write error %d feet into tape %d\n", asize/120L, tapeno); 12818012Smckusick broadcast("TAPE ERROR!\n"); 12918012Smckusick if (!query("Do you want to restart?")) 13018012Smckusick dumpabort(); 13118012Smckusick msg("This tape will rewind. After it is rewound,\n"); 13218012Smckusick msg("replace the faulty tape with a new one;\n"); 13318012Smckusick msg("this dump volume will be rewritten.\n"); 13424181Smckusick killall(); 13518012Smckusick nogripe = 1; 13618012Smckusick close_rewind(); 13718012Smckusick Exit(X_REWRITE); 13818012Smckusick } 13918012Smckusick 140*46585Storek void 14125219Smckusick sigpipe() 14225219Smckusick { 14325219Smckusick 144*46585Storek quit("Broken pipe\n"); 14525219Smckusick } 14625219Smckusick 14718012Smckusick #ifdef RDUMP 14825219Smckusick /* 14925219Smckusick * compatibility routine 15025219Smckusick */ 151*46585Storek void 15225219Smckusick tflush(i) 15325219Smckusick int i; 15418012Smckusick { 15518012Smckusick 15618012Smckusick for (i = 0; i < ntrec; i++) 15718012Smckusick spclrec(); 15818012Smckusick } 15918012Smckusick #endif RDUMP 16018012Smckusick 161*46585Storek void 1621425Sroot flusht() 1631425Sroot { 16425219Smckusick int siz = (char *)tblock - (char *)req; 1651425Sroot 166*46585Storek if (atomic(write, slavefd[rotor], req, siz) != siz) 167*46585Storek quit("error writing command pipe: %s\n", strerror(errno)); 168*46585Storek if (++rotor >= SLAVES) 169*46585Storek rotor = 0; 17018012Smckusick tblock = (char (*)[TP_BSIZE]) &req[ntrec]; 1711425Sroot trecno = 0; 17224181Smckusick asize += tenths; 17310911Ssam blockswritten += ntrec; 17412331Smckusick if (!pipeout && asize > tsize) { 1751425Sroot close_rewind(); 1761425Sroot otape(); 1771425Sroot } 1781425Sroot timeest(); 1791425Sroot } 1801425Sroot 181*46585Storek void 18246239Storek trewind() 1831425Sroot { 18424181Smckusick int f; 18512331Smckusick 18612331Smckusick if (pipeout) 18712331Smckusick return; 18818012Smckusick for (f = 0; f < SLAVES; f++) 18918012Smckusick close(slavefd[f]); 190*46585Storek while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ 191*46585Storek /* void */; 19218012Smckusick msg("Tape rewinding\n"); 19318012Smckusick #ifdef RDUMP 19425219Smckusick if (host) { 19525219Smckusick rmtclose(); 19625219Smckusick while (rmtopen(tape, 0) < 0) 19725219Smckusick sleep(10); 19825219Smckusick rmtclose(); 19925219Smckusick return; 20025219Smckusick } 20125219Smckusick #endif RDUMP 2023214Swnj close(to); 2033214Swnj while ((f = open(tape, 0)) < 0) 2043214Swnj sleep (10); 2053214Swnj close(f); 2061425Sroot } 2071425Sroot 208*46585Storek void 2091425Sroot close_rewind() 2101425Sroot { 21146239Storek trewind(); 21218012Smckusick if (!nogripe) { 2131425Sroot msg("Change Tapes: Mount tape #%d\n", tapeno+1); 2141425Sroot broadcast("CHANGE TAPES!\7\7\n"); 2151425Sroot } 21618012Smckusick while (!query("Is the new tape mounted and ready to go?")) 21725219Smckusick if (query("Do you want to abort?")) { 2181425Sroot dumpabort(); 21925219Smckusick /*NOTREACHED*/ 22025219Smckusick } 2211425Sroot } 2221425Sroot 2231425Sroot /* 22418012Smckusick * We implement taking and restoring checkpoints on the tape level. 2251425Sroot * When each tape is opened, a new process is created by forking; this 2261425Sroot * saves all of the necessary context in the parent. The child 2271425Sroot * continues the dump; the parent waits around, saving the context. 2281425Sroot * If the child returns X_REWRITE, then it had problems writing that tape; 2291425Sroot * this causes the parent to fork again, duplicating the context, and 2301425Sroot * everything continues as if nothing had happened. 2311425Sroot */ 2321425Sroot 233*46585Storek void 2341425Sroot otape() 2351425Sroot { 2361425Sroot int parentpid; 2371425Sroot int childpid; 2381425Sroot int status; 2391425Sroot int waitpid; 24039164Sbostic sig_t interrupt; 24129899Smckusick int blks, i; 2421425Sroot 24339164Sbostic interrupt = signal(SIGINT, SIG_IGN); 2441425Sroot parentpid = getpid(); 2451425Sroot 2461425Sroot restore_check_point: 24739164Sbostic (void)signal(SIGINT, interrupt); 24825219Smckusick /* 24925219Smckusick * All signals are inherited... 25025219Smckusick */ 2511425Sroot childpid = fork(); 25218012Smckusick if (childpid < 0) { 2531425Sroot msg("Context save fork fails in parent %d\n", parentpid); 2541425Sroot Exit(X_ABORT); 2551425Sroot } 25618012Smckusick if (childpid != 0) { 2571425Sroot /* 2581425Sroot * PARENT: 2591425Sroot * save the context by waiting 2601425Sroot * until the child doing all of the work returns. 26118012Smckusick * don't catch the interrupt 2621425Sroot */ 26325219Smckusick signal(SIGINT, SIG_IGN); 2641425Sroot #ifdef TDEBUG 2651425Sroot msg("Tape: %d; parent process: %d child process %d\n", 2661425Sroot tapeno+1, parentpid, childpid); 2671425Sroot #endif TDEBUG 26818012Smckusick while ((waitpid = wait(&status)) != childpid) 26918012Smckusick msg("Parent %d waiting for child %d has another child %d return\n", 27018012Smckusick parentpid, childpid, waitpid); 27118012Smckusick if (status & 0xFF) { 2721425Sroot msg("Child %d returns LOB status %o\n", 2731425Sroot childpid, status&0xFF); 2741425Sroot } 2751425Sroot status = (status >> 8) & 0xFF; 2761425Sroot #ifdef TDEBUG 27718012Smckusick switch(status) { 2781425Sroot case X_FINOK: 2791425Sroot msg("Child %d finishes X_FINOK\n", childpid); 2801425Sroot break; 2811425Sroot case X_ABORT: 2821425Sroot msg("Child %d finishes X_ABORT\n", childpid); 2831425Sroot break; 2841425Sroot case X_REWRITE: 2851425Sroot msg("Child %d finishes X_REWRITE\n", childpid); 2861425Sroot break; 2871425Sroot default: 28818012Smckusick msg("Child %d finishes unknown %d\n", 28925219Smckusick childpid, status); 2901425Sroot break; 2911425Sroot } 2921425Sroot #endif TDEBUG 29318012Smckusick switch(status) { 2941425Sroot case X_FINOK: 2951425Sroot Exit(X_FINOK); 2961425Sroot case X_ABORT: 2971425Sroot Exit(X_ABORT); 2981425Sroot case X_REWRITE: 2991425Sroot goto restore_check_point; 3001425Sroot default: 3011425Sroot msg("Bad return code from dump: %d\n", status); 3021425Sroot Exit(X_ABORT); 3031425Sroot } 3041425Sroot /*NOTREACHED*/ 3051425Sroot } else { /* we are the child; just continue */ 3061425Sroot #ifdef TDEBUG 3071425Sroot sleep(4); /* allow time for parent's message to get out */ 3081425Sroot msg("Child on Tape %d has parent %d, my pid = %d\n", 3091425Sroot tapeno+1, parentpid, getpid()); 31025219Smckusick #endif TDEBUG 31118012Smckusick #ifdef RDUMP 31225219Smckusick while ((to = (host ? rmtopen(tape, 2) : 31325219Smckusick pipeout ? 1 : creat(tape, 0666))) < 0) 31425219Smckusick #else RDUMP 31518012Smckusick while ((to = pipeout ? 1 : creat(tape, 0666)) < 0) 31624181Smckusick #endif RDUMP 31739128Smckusick { 31839128Smckusick msg("Cannot open tape \"%s\".\n", tape); 31939128Smckusick if (!query("Do you want to retry the open?")) 32018012Smckusick dumpabort(); 32139128Smckusick } 3221425Sroot 32318012Smckusick enslave(); /* Share open tape file descriptor with slaves */ 32418012Smckusick 3251425Sroot asize = 0; 3261425Sroot tapeno++; /* current tape sequence */ 3271425Sroot newtape++; /* new tape signal */ 32829899Smckusick blks = 0; 32929899Smckusick if (spcl.c_type != TS_END) 33029899Smckusick for (i = 0; i < spcl.c_count; i++) 33129899Smckusick if (spcl.c_addr[i] != 0) 33229899Smckusick blks++; 33329899Smckusick spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec; 3341425Sroot spcl.c_volume++; 3351425Sroot spcl.c_type = TS_TAPE; 33630432Smckusick spcl.c_flags |= DR_NEWHEADER; 3371425Sroot spclrec(); 33830432Smckusick spcl.c_flags &=~ DR_NEWHEADER; 3391425Sroot if (tapeno > 1) 3401425Sroot msg("Tape %d begins with blocks from ino %d\n", 3411425Sroot tapeno, ino); 3421425Sroot } 3431425Sroot } 3441425Sroot 345*46585Storek void 3461425Sroot dumpabort() 3471425Sroot { 34818012Smckusick if (master != 0 && master != getpid()) 34925219Smckusick kill(master, SIGTERM); /* Signals master to call dumpabort */ 35024181Smckusick else { 35124181Smckusick killall(); 35224181Smckusick msg("The ENTIRE dump is aborted.\n"); 35324181Smckusick } 3541425Sroot Exit(X_ABORT); 3551425Sroot } 3561425Sroot 357*46585Storek void 3581425Sroot Exit(status) 35946239Storek int status; 3601425Sroot { 3611425Sroot #ifdef TDEBUG 3621425Sroot msg("pid = %d exits with status %d\n", getpid(), status); 3631425Sroot #endif TDEBUG 3641925Swnj exit(status); 3651425Sroot } 36618012Smckusick 36724181Smckusick /* 36825219Smckusick * could use pipe() for this if flock() worked on pipes 36924181Smckusick */ 370*46585Storek void 37124181Smckusick lockfile(fd) 37224181Smckusick int fd[2]; 37324181Smckusick { 37424181Smckusick char tmpname[20]; 37518012Smckusick 37639128Smckusick strcpy(tmpname, _PATH_LOCK); 37724181Smckusick mktemp(tmpname); 378*46585Storek if ((fd[1] = creat(tmpname, 0400)) < 0) 379*46585Storek quit("cannot create lockfile %s: %s\n", 380*46585Storek tmpname, strerror(errno)); 381*46585Storek if ((fd[0] = open(tmpname, 0)) < 0) 382*46585Storek quit("cannot reopen lockfile %s: %s\n", 383*46585Storek tmpname, strerror(errno)); 384*46585Storek (void) unlink(tmpname); 38524181Smckusick } 38624181Smckusick 387*46585Storek void 38818012Smckusick enslave() 38918012Smckusick { 39024181Smckusick int first[2], prev[2], next[2], cmd[2]; /* file descriptors */ 39124181Smckusick register int i, j; 39218012Smckusick 39318012Smckusick master = getpid(); 39425219Smckusick signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ 39525219Smckusick signal(SIGPIPE, sigpipe); 39625219Smckusick signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ 39724181Smckusick lockfile(first); 39824181Smckusick for (i = 0; i < SLAVES; i++) { 39924181Smckusick if (i == 0) { 40024181Smckusick prev[0] = first[1]; 40124181Smckusick prev[1] = first[0]; 40224181Smckusick } else { 40324181Smckusick prev[0] = next[0]; 40424181Smckusick prev[1] = next[1]; 40524181Smckusick flock(prev[1], LOCK_EX); 40624181Smckusick } 40726485Smckusick if (i < SLAVES - 1) { 40826485Smckusick lockfile(next); 40926485Smckusick } else { 41026485Smckusick next[0] = first[0]; 41126485Smckusick next[1] = first[1]; /* Last slave loops back */ 41226485Smckusick } 413*46585Storek if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) 414*46585Storek quit("too many slaves, %d (recompile smaller): %s\n", 415*46585Storek i, strerror(errno)); 41618012Smckusick slavefd[i] = cmd[1]; 41725219Smckusick if (slavepid[i] == 0) { /* Slave starts up here */ 41818012Smckusick for (j = 0; j <= i; j++) 41918012Smckusick close(slavefd[j]); 42025219Smckusick signal(SIGINT, SIG_IGN); /* Master handles this */ 42125219Smckusick doslave(cmd[0], prev, next); 42218012Smckusick Exit(X_FINOK); 42318012Smckusick } 42418012Smckusick close(cmd[0]); 42524181Smckusick if (i > 0) { 42624181Smckusick close(prev[0]); 42724181Smckusick close(prev[1]); 42824181Smckusick } 42918012Smckusick } 43024181Smckusick close(first[0]); 43124181Smckusick close(first[1]); 43224181Smckusick master = 0; rotor = 0; 43318012Smckusick } 43418012Smckusick 435*46585Storek void 43624181Smckusick killall() 43718012Smckusick { 43824181Smckusick register int i; 43919982Smckusick 44024181Smckusick for (i = 0; i < SLAVES; i++) 44124181Smckusick if (slavepid[i] > 0) 44224181Smckusick kill(slavepid[i], SIGKILL); 44318012Smckusick } 44418012Smckusick 44524181Smckusick /* 44624181Smckusick * Synchronization - each process has a lockfile, and shares file 44724181Smckusick * descriptors to the following process's lockfile. When our write 44824181Smckusick * completes, we release our lock on the following process's lock- 44924181Smckusick * file, allowing the following process to lock it and proceed. We 45024181Smckusick * get the lock back for the next cycle by swapping descriptors. 45124181Smckusick */ 452*46585Storek void 45325219Smckusick doslave(cmd, prev, next) 45425219Smckusick register int cmd, prev[2], next[2]; 45519982Smckusick { 45625219Smckusick register int nread, toggle = 0; 45719982Smckusick 45818012Smckusick close(fi); 459*46585Storek if ((fi = open(disk, 0)) < 0) /* Need our own seek pointer */ 460*46585Storek quit("slave couldn't reopen disk: %s\n", strerror(errno)); 46124181Smckusick /* 46225219Smckusick * Get list of blocks to dump, read the blocks into tape buffer 46324181Smckusick */ 46425219Smckusick while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) { 46518012Smckusick register struct req *p = req; 46618012Smckusick for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) { 46718012Smckusick if (p->dblk) { 46818012Smckusick bread(p->dblk, tblock[trecno], 46925219Smckusick p->count * TP_BSIZE); 47018012Smckusick } else { 47125219Smckusick if (p->count != 1 || atomic(read, cmd, 472*46585Storek tblock[trecno], TP_BSIZE) != TP_BSIZE) 473*46585Storek quit("master/slave protocol botched.\n"); 47418012Smckusick } 47518012Smckusick } 47624181Smckusick flock(prev[toggle], LOCK_EX); /* Wait our turn */ 47725219Smckusick 47818012Smckusick #ifdef RDUMP 47925219Smckusick if ((host ? rmtwrite(tblock[0], writesize) 48025219Smckusick : write(to, tblock[0], writesize)) != writesize) { 48125219Smckusick #else RDUMP 48225219Smckusick if (write(to, tblock[0], writesize) != writesize) { 48324181Smckusick #endif RDUMP 48425219Smckusick kill(master, SIGUSR1); 48525219Smckusick for (;;) 48625219Smckusick sigpause(0); 48718012Smckusick } 48824181Smckusick toggle ^= 1; 48924181Smckusick flock(next[toggle], LOCK_UN); /* Next slave's turn */ 49024181Smckusick } /* Also jolts him awake */ 491*46585Storek if (nread != 0) 492*46585Storek quit("error reading command pipe: %s\n", strerror(errno)); 49318012Smckusick } 49419947Smckusick 49519947Smckusick /* 49625219Smckusick * Since a read from a pipe may not return all we asked for, 49725219Smckusick * or a write may not write all we ask if we get a signal, 49825219Smckusick * loop until the count is satisfied (or error). 49919947Smckusick */ 500*46585Storek int 50125219Smckusick atomic(func, fd, buf, count) 50225219Smckusick int (*func)(), fd, count; 50319947Smckusick char *buf; 50419947Smckusick { 50525219Smckusick int got, need = count; 50619947Smckusick 50725219Smckusick while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) 50819947Smckusick buf += got; 50925219Smckusick return (got < 0 ? got : count - need); 51019947Smckusick } 511