147082Smckusick /*- 247082Smckusick * Copyright (c) 1980, 1991 The Regents of the University of California. 347082Smckusick * All rights reserved. 447082Smckusick * 547082Smckusick * %sccs.include.redist.c% 622040Sdist */ 722040Sdist 817527Ssam #ifndef lint 9*54595Smckusick static char sccsid[] = "@(#)tape.c 5.24 (Berkeley) 07/02/92"; 1046585Storek #endif /* not lint */ 1117527Ssam 1250505Smckusick #ifdef sunos 1350574Smckusick #include <sys/param.h> 1450505Smckusick #include <stdio.h> 1550505Smckusick #include <ctype.h> 1650505Smckusick #include <sys/stat.h> 1751605Sbostic #include <ufs/fs.h> 1850505Smckusick #else 1946795Sbostic #include <sys/param.h> 2046585Storek #include <sys/wait.h> 2151605Sbostic #include <ufs/ffs/fs.h> 2250505Smckusick #endif 2354047Smckusick #include <sys/time.h> 2454047Smckusick #include <ufs/ufs/dinode.h> 2546795Sbostic #include <signal.h> 2646795Sbostic #include <fcntl.h> 2746795Sbostic #include <protocols/dumprestore.h> 2846585Storek #include <errno.h> 2950504Smckusick #include <setjmp.h> 3046795Sbostic #ifdef __STDC__ 3146795Sbostic #include <unistd.h> 3246795Sbostic #include <stdlib.h> 3346795Sbostic #include <string.h> 3446795Sbostic #endif 3550504Smckusick #include <sys/socket.h> 3646795Sbostic #include "dump.h" 3739128Smckusick #include "pathnames.h" 381425Sroot 3929899Smckusick int writesize; /* size of malloc()ed buffer for tape */ 4029899Smckusick long lastspclrec = -1; /* tape block number of last written header */ 4129899Smckusick int trecno = 0; /* next record to write in current block */ 4246614Smckusick extern long blocksperfile; /* number of blocks per output file */ 4348621Skarels long blocksthisvol; /* number of blocks on current output file */ 4448621Skarels extern int ntrec; /* blocking factor on tape */ 4548621Skarels extern int cartridge; 4650504Smckusick extern char *host; 4747056Skarels char *nexttape; 4825219Smckusick #ifdef RDUMP 4946585Storek int rmtopen(), rmtwrite(); 5046585Storek void rmtclose(); 5125219Smckusick #endif RDUMP 5250504Smckusick void rollforward(); 5346585Storek int atomic(); 5446789Smckusick void doslave(), enslave(), flushtape(), killall(); 5546585Storek 5610911Ssam /* 5724181Smckusick * Concurrent dump mods (Caltech) - disk block reading and tape writing 5818012Smckusick * are exported to several slave processes. While one slave writes the 5918012Smckusick * tape, the others read disk blocks; they pass control of the tape in 6050504Smckusick * a ring via signals. The parent process traverses the filesystem and 6146789Smckusick * sends writeheader()'s and lists of daddr's to the slaves via pipes. 6250504Smckusick * The following structure defines the instruction packets sent to slaves. 6310911Ssam */ 6450504Smckusick struct req { 6518012Smckusick daddr_t dblk; 6618012Smckusick int count; 6750504Smckusick }; 6818012Smckusick int reqsiz; 6918012Smckusick 7024181Smckusick #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 7150504Smckusick struct slave { 7250504Smckusick int tapea; /* header number at start of this chunk */ 7350504Smckusick int count; /* count to next header (used for TS_TAPE */ 7450504Smckusick /* after EOT) */ 7550504Smckusick int inode; /* inode that we are currently dealing with */ 7650504Smckusick int fd; /* FD for this slave */ 7750504Smckusick int pid; /* PID for this slave */ 7850504Smckusick int sent; /* 1 == we've sent this slave requests */ 7950504Smckusick int firstrec; /* record number of this block */ 8050504Smckusick char (*tblock)[TP_BSIZE]; /* buffer for data blocks */ 8150504Smckusick struct req *req; /* buffer for requests */ 8250504Smckusick } slaves[SLAVES+1]; 8350504Smckusick struct slave *slp; 8418012Smckusick 8550504Smckusick char (*nextblock)[TP_BSIZE]; 8650504Smckusick 8750504Smckusick int master; /* pid of master, for sending error signals */ 8850504Smckusick int tenths; /* length of tape used per block written */ 8950504Smckusick static int caught; /* have we caught the signal to proceed? */ 9050504Smckusick static int ready; /* have we reached the lock point without having */ 9150504Smckusick /* received the SIGUSR2 signal from the prev slave? */ 9250504Smckusick static jmp_buf jmpbuf; /* where to jump to if we are ready when the */ 9350504Smckusick /* SIGUSR2 arrives from the previous slave */ 9450504Smckusick 9546585Storek int 9610911Ssam alloctape() 9710911Ssam { 9825219Smckusick int pgoff = getpagesize() - 1; 9950504Smckusick char *buf; 10050504Smckusick int i; 10110911Ssam 10210911Ssam writesize = ntrec * TP_BSIZE; 10350504Smckusick reqsiz = (ntrec + 1) * sizeof(struct req); 10424181Smckusick /* 10525219Smckusick * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 10625219Smckusick * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 10725219Smckusick * repositioning after stopping, i.e, streaming mode, where the gap is 10825219Smckusick * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 10924181Smckusick */ 11047056Skarels if (blocksperfile == 0) 11147056Skarels tenths = writesize / density + 11247056Skarels (cartridge ? 16 : density == 625 ? 5 : 8); 11325219Smckusick /* 11425219Smckusick * Allocate tape buffer contiguous with the array of instruction 11546789Smckusick * packets, so flushtape() can write them together with one write(). 11625219Smckusick * Align tape buffer on page boundary to speed up tape write(). 11725219Smckusick */ 11850504Smckusick for (i = 0; i <= SLAVES; i++) { 11954047Smckusick buf = (char *) 12054047Smckusick malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE)); 12150504Smckusick if (buf == NULL) 12254047Smckusick return(0); 12350504Smckusick slaves[i].tblock = (char (*)[TP_BSIZE]) 12450504Smckusick (((long)&buf[ntrec + 1] + pgoff) &~ pgoff); 12550504Smckusick slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1; 12650504Smckusick } 12750504Smckusick slp = &slaves[0]; 12850504Smckusick slp->count = 1; 12950504Smckusick slp->tapea = 0; 13050504Smckusick slp->firstrec = 0; 13150504Smckusick nextblock = slp->tblock; 13224181Smckusick return(1); 13310911Ssam } 13410911Ssam 13546585Storek void 136*54595Smckusick writerec(dp, isspcl) 1375329Smckusic char *dp; 138*54595Smckusick int isspcl; 1391425Sroot { 14050504Smckusick 14150504Smckusick slp->req[trecno].dblk = (daddr_t)0; 14250504Smckusick slp->req[trecno].count = 1; 14350504Smckusick *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp; 144*54595Smckusick if (isspcl) 145*54595Smckusick lastspclrec = spcl.c_tapea; 14624181Smckusick trecno++; 1471425Sroot spcl.c_tapea++; 14846585Storek if (trecno >= ntrec) 14946789Smckusick flushtape(); 1501425Sroot } 1511425Sroot 15246585Storek void 15346789Smckusick dumpblock(blkno, size) 1544774Smckusic daddr_t blkno; 1554774Smckusic int size; 1561425Sroot { 15725219Smckusick int avail, tpblks, dblkno; 1581425Sroot 1595329Smckusic dblkno = fsbtodb(sblock, blkno); 16046585Storek tpblks = size >> tp_bshift; 16118012Smckusick while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { 16250504Smckusick slp->req[trecno].dblk = dblkno; 16350504Smckusick slp->req[trecno].count = avail; 16425219Smckusick trecno += avail; 1654774Smckusic spcl.c_tapea += avail; 16625219Smckusick if (trecno >= ntrec) 16746789Smckusick flushtape(); 16846585Storek dblkno += avail << (tp_bshift - dev_bshift); 1695329Smckusic tpblks -= avail; 1704774Smckusic } 1711425Sroot } 1721425Sroot 1731425Sroot int nogripe = 0; 1741425Sroot 17546585Storek void 17646585Storek tperror() 17746585Storek { 17850504Smckusick 17918012Smckusick if (pipeout) { 18046614Smckusick msg("write error on %s\n", tape); 18146585Storek quit("Cannot recover\n"); 18218012Smckusick /* NOTREACHED */ 18318012Smckusick } 18448621Skarels msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno); 18546614Smckusick broadcast("DUMP WRITE ERROR!\n"); 18618012Smckusick if (!query("Do you want to restart?")) 18718012Smckusick dumpabort(); 18846614Smckusick msg("Closing this volume. Prepare to restart with new media;\n"); 18918012Smckusick msg("this dump volume will be rewritten.\n"); 19024181Smckusick killall(); 19118012Smckusick nogripe = 1; 19218012Smckusick close_rewind(); 19318012Smckusick Exit(X_REWRITE); 19418012Smckusick } 19518012Smckusick 19646585Storek void 19725219Smckusick sigpipe() 19825219Smckusick { 19925219Smckusick 20046585Storek quit("Broken pipe\n"); 20125219Smckusick } 20225219Smckusick 20346585Storek void 20446789Smckusick flushtape() 20518012Smckusick { 20650504Smckusick int i, blks, got; 20750504Smckusick long lastfirstrec; 20846795Sbostic #ifndef __STDC__ 20950504Smckusick int write(), read(); 21046795Sbostic #endif 21146795Sbostic 21250504Smckusick int siz = (char *)nextblock - (char *)slp->req; 2131425Sroot 21450504Smckusick slp->req[trecno].count = 0; /* Sentinel */ 21550504Smckusick 21654047Smckusick if (atomic(write, slp->fd, (char *)slp->req, siz) != siz) 21746585Storek quit("error writing command pipe: %s\n", strerror(errno)); 21850504Smckusick slp->sent = 1; /* we sent a request, read the response later */ 21950504Smckusick 22050504Smckusick lastfirstrec = slp->firstrec; 22150504Smckusick 22250504Smckusick if (++slp >= &slaves[SLAVES]) 22350504Smckusick slp = &slaves[0]; 22450504Smckusick 22550504Smckusick /* Read results back from next slave */ 22650504Smckusick if (slp->sent) { 22754047Smckusick if (atomic(read, slp->fd, (char *)&got, sizeof got) 22854047Smckusick != sizeof got) { 22950504Smckusick perror(" DUMP: error reading command pipe in master"); 23050504Smckusick dumpabort(); 23150504Smckusick } 23250504Smckusick slp->sent = 0; 23350504Smckusick 23450504Smckusick /* Check for end of tape */ 23550504Smckusick if (got < writesize) { 23650504Smckusick msg("End of tape detected\n"); 23750504Smckusick 23850504Smckusick /* 23950504Smckusick * Drain the results, don't care what the values were. 24050504Smckusick * If we read them here then trewind won't... 24150504Smckusick */ 24250504Smckusick for (i = 0; i < SLAVES; i++) { 24350504Smckusick if (slaves[i].sent) { 24454047Smckusick if (atomic(read, slaves[i].fd, 24554047Smckusick (char *)&got, sizeof got) 24654047Smckusick != sizeof got) { 24750504Smckusick perror(" DUMP: error reading command pipe in master"); 24850504Smckusick dumpabort(); 24950504Smckusick } 25050504Smckusick slaves[i].sent = 0; 25150504Smckusick } 25250504Smckusick } 25350504Smckusick 25450504Smckusick close_rewind(); 25550504Smckusick rollforward(); 25650504Smckusick return; 25750504Smckusick } 25850504Smckusick } 25950504Smckusick 26050504Smckusick blks = 0; 26150504Smckusick if (spcl.c_type != TS_END) { 26250504Smckusick for (i = 0; i < spcl.c_count; i++) 26350504Smckusick if (spcl.c_addr[i] != 0) 26450504Smckusick blks++; 26550504Smckusick } 26650504Smckusick slp->count = lastspclrec + blks + 1 - spcl.c_tapea; 26750504Smckusick slp->tapea = spcl.c_tapea; 26850504Smckusick slp->firstrec = lastfirstrec + ntrec; 26950504Smckusick slp->inode = curino; 27050504Smckusick nextblock = slp->tblock; 2711425Sroot trecno = 0; 27224181Smckusick asize += tenths; 27310911Ssam blockswritten += ntrec; 27448621Skarels blocksthisvol += ntrec; 27546614Smckusick if (!pipeout && (blocksperfile ? 27648621Skarels (blocksthisvol >= blocksperfile) : (asize > tsize))) { 2771425Sroot close_rewind(); 27850504Smckusick startnewtape(0); 2791425Sroot } 2801425Sroot timeest(); 2811425Sroot } 2821425Sroot 28346585Storek void 28446239Storek trewind() 2851425Sroot { 28624181Smckusick int f; 28750504Smckusick int got; 28812331Smckusick 28950504Smckusick for (f = 0; f < SLAVES; f++) { 29050504Smckusick /* 29150504Smckusick * Drain the results, but unlike EOT we DO (or should) care 29250504Smckusick * what the return values were, since if we detect EOT after 29350504Smckusick * we think we've written the last blocks to the tape anyway, 29450504Smckusick * we have to replay those blocks with rollforward. 29550504Smckusick * 29650504Smckusick * fixme: punt for now. 29750504Smckusick */ 29850504Smckusick if (slaves[f].sent) { 29954047Smckusick if (atomic(read, slaves[f].fd, (char *)&got, sizeof got) 30050504Smckusick != sizeof got) { 30150504Smckusick perror(" DUMP: error reading command pipe in master"); 30250504Smckusick dumpabort(); 30350504Smckusick } 30450504Smckusick slaves[f].sent = 0; 30550504Smckusick if (got != writesize) { 30650504Smckusick msg("EOT detected in last 2 tape records!\n"); 30750504Smckusick msg("Use a longer tape, decrease the size estimate\n"); 30850504Smckusick quit("or use no size estimate at all.\n"); 30950504Smckusick } 31050504Smckusick } 31154047Smckusick (void) close(slaves[f].fd); 31250504Smckusick } 31346585Storek while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ 31446585Storek /* void */; 31554047Smckusick 31654047Smckusick if (pipeout) 31754047Smckusick return; 31854047Smckusick 31948621Skarels msg("Closing %s\n", tape); 32050504Smckusick 32118012Smckusick #ifdef RDUMP 32225219Smckusick if (host) { 32325219Smckusick rmtclose(); 32425219Smckusick while (rmtopen(tape, 0) < 0) 32525219Smckusick sleep(10); 32625219Smckusick rmtclose(); 32725219Smckusick return; 32825219Smckusick } 32950504Smckusick #endif 33054047Smckusick (void) close(tapefd); 3313214Swnj while ((f = open(tape, 0)) < 0) 3323214Swnj sleep (10); 33354047Smckusick (void) close(f); 3341425Sroot } 3351425Sroot 33646585Storek void 3371425Sroot close_rewind() 3381425Sroot { 33946239Storek trewind(); 34048621Skarels if (nexttape) 34148621Skarels return; 34218012Smckusick if (!nogripe) { 34346614Smckusick msg("Change Volumes: Mount volume #%d\n", tapeno+1); 34446614Smckusick broadcast("CHANGE DUMP VOLUMES!\7\7\n"); 3451425Sroot } 34648621Skarels while (!query("Is the new volume mounted and ready to go?")) 34725219Smckusick if (query("Do you want to abort?")) { 3481425Sroot dumpabort(); 34925219Smckusick /*NOTREACHED*/ 35025219Smckusick } 3511425Sroot } 3521425Sroot 35350504Smckusick void 35450504Smckusick rollforward() 35550504Smckusick { 35650504Smckusick register struct req *p, *q, *prev; 35750504Smckusick register struct slave *tslp; 35854047Smckusick int i, size, savedtapea, got; 35950504Smckusick union u_spcl *ntb, *otb; 36050504Smckusick tslp = &slaves[SLAVES]; 36150504Smckusick ntb = (union u_spcl *)tslp->tblock[1]; 36250504Smckusick 36350504Smckusick /* 36450504Smckusick * Each of the N slaves should have requests that need to 36550504Smckusick * be replayed on the next tape. Use the extra slave buffers 36650504Smckusick * (slaves[SLAVES]) to construct request lists to be sent to 36750504Smckusick * each slave in turn. 36850504Smckusick */ 36950504Smckusick for (i = 0; i < SLAVES; i++) { 37050504Smckusick q = &tslp->req[1]; 37150504Smckusick otb = (union u_spcl *)slp->tblock; 37250504Smckusick 37350504Smckusick /* 37450504Smckusick * For each request in the current slave, copy it to tslp. 37550504Smckusick */ 37650504Smckusick 37750504Smckusick for (p = slp->req; p->count > 0; p += p->count) { 37850504Smckusick *q = *p; 37950504Smckusick if (p->dblk == 0) 38050504Smckusick *ntb++ = *otb++; /* copy the datablock also */ 38150504Smckusick prev = q; 38250504Smckusick q += q->count; 38350504Smckusick } 38450504Smckusick if (prev->dblk != 0) 38550504Smckusick prev->count -= 1; 38650504Smckusick else 38750504Smckusick ntb--; 38850504Smckusick q -= 1; 38950504Smckusick q->count = 0; 39050504Smckusick q = &tslp->req[0]; 39150504Smckusick if (i == 0) { 39250504Smckusick q->dblk = 0; 39350504Smckusick q->count = 1; 39450504Smckusick trecno = 0; 39550504Smckusick nextblock = tslp->tblock; 39650504Smckusick savedtapea = spcl.c_tapea; 39750504Smckusick spcl.c_tapea = slp->tapea; 39850504Smckusick startnewtape(0); 39950504Smckusick spcl.c_tapea = savedtapea; 40050504Smckusick lastspclrec = savedtapea - 1; 40150504Smckusick } 40250504Smckusick size = (char *)ntb - (char *)q; 40354047Smckusick if (atomic(write, slp->fd, (char *)q, size) != size) { 40450504Smckusick perror(" DUMP: error writing command pipe"); 40550504Smckusick dumpabort(); 40650504Smckusick } 40750504Smckusick slp->sent = 1; 40850504Smckusick if (++slp >= &slaves[SLAVES]) 40950504Smckusick slp = &slaves[0]; 41050504Smckusick 41150504Smckusick q->count = 1; 41250504Smckusick 41350504Smckusick if (prev->dblk != 0) { 41450504Smckusick /* 41550504Smckusick * If the last one was a disk block, make the 41650504Smckusick * first of this one be the last bit of that disk 41750504Smckusick * block... 41850504Smckusick */ 41950504Smckusick q->dblk = prev->dblk + 42050504Smckusick prev->count * (TP_BSIZE / DEV_BSIZE); 42150504Smckusick ntb = (union u_spcl *)tslp->tblock; 42250504Smckusick } else { 42350504Smckusick /* 42450504Smckusick * It wasn't a disk block. Copy the data to its 42550504Smckusick * new location in the buffer. 42650504Smckusick */ 42750504Smckusick q->dblk = 0; 42850504Smckusick *((union u_spcl *)tslp->tblock) = *ntb; 42950504Smckusick ntb = (union u_spcl *)tslp->tblock[1]; 43050504Smckusick } 43150504Smckusick } 43250504Smckusick slp->req[0] = *q; 43350504Smckusick nextblock = slp->tblock; 43450504Smckusick if (q->dblk == 0) 43550504Smckusick nextblock++; 43650504Smckusick trecno = 1; 43750504Smckusick 43850504Smckusick /* 43950504Smckusick * Clear the first slaves' response. One hopes that it 44050504Smckusick * worked ok, otherwise the tape is much too short! 44150504Smckusick */ 44250504Smckusick if (slp->sent) { 44354047Smckusick if (atomic(read, slp->fd, (char *)&got, sizeof got) 44454047Smckusick != sizeof got) { 44550504Smckusick perror(" DUMP: error reading command pipe in master"); 44650504Smckusick dumpabort(); 44750504Smckusick } 44850504Smckusick slp->sent = 0; 44950504Smckusick 45050504Smckusick if (got != writesize) { 45150504Smckusick quit("EOT detected at start of the tape!\n"); 45250504Smckusick } 45350504Smckusick } 45450504Smckusick } 45550504Smckusick 4561425Sroot /* 45750504Smckusick * We implement taking and restoring checkpoints on the tape level. 45850504Smckusick * When each tape is opened, a new process is created by forking; this 45950504Smckusick * saves all of the necessary context in the parent. The child 46050504Smckusick * continues the dump; the parent waits around, saving the context. 46150504Smckusick * If the child returns X_REWRITE, then it had problems writing that tape; 46250504Smckusick * this causes the parent to fork again, duplicating the context, and 46350504Smckusick * everything continues as if nothing had happened. 4641425Sroot */ 46546585Storek void 46650504Smckusick startnewtape(top) 46750504Smckusick int top; 4681425Sroot { 4691425Sroot int parentpid; 4701425Sroot int childpid; 4711425Sroot int status; 4721425Sroot int waitpid; 47347056Skarels char *p; 47450574Smckusick #ifdef sunos 47554047Smckusick void (*interrupt_save)(); 47650574Smckusick char *index(); 47750574Smckusick #else 47854047Smckusick sig_t interrupt_save; 47950574Smckusick #endif 4801425Sroot 48154047Smckusick interrupt_save = signal(SIGINT, SIG_IGN); 4821425Sroot parentpid = getpid(); 4831425Sroot 4841425Sroot restore_check_point: 48554047Smckusick (void)signal(SIGINT, interrupt_save); 48625219Smckusick /* 48725219Smckusick * All signals are inherited... 48825219Smckusick */ 4891425Sroot childpid = fork(); 49018012Smckusick if (childpid < 0) { 4911425Sroot msg("Context save fork fails in parent %d\n", parentpid); 4921425Sroot Exit(X_ABORT); 4931425Sroot } 49418012Smckusick if (childpid != 0) { 4951425Sroot /* 4961425Sroot * PARENT: 4971425Sroot * save the context by waiting 4981425Sroot * until the child doing all of the work returns. 49918012Smckusick * don't catch the interrupt 5001425Sroot */ 50125219Smckusick signal(SIGINT, SIG_IGN); 5021425Sroot #ifdef TDEBUG 5031425Sroot msg("Tape: %d; parent process: %d child process %d\n", 5041425Sroot tapeno+1, parentpid, childpid); 5051425Sroot #endif TDEBUG 50618012Smckusick while ((waitpid = wait(&status)) != childpid) 50718012Smckusick msg("Parent %d waiting for child %d has another child %d return\n", 50818012Smckusick parentpid, childpid, waitpid); 50918012Smckusick if (status & 0xFF) { 5101425Sroot msg("Child %d returns LOB status %o\n", 5111425Sroot childpid, status&0xFF); 5121425Sroot } 5131425Sroot status = (status >> 8) & 0xFF; 5141425Sroot #ifdef TDEBUG 51518012Smckusick switch(status) { 5161425Sroot case X_FINOK: 5171425Sroot msg("Child %d finishes X_FINOK\n", childpid); 5181425Sroot break; 51950504Smckusick case X_ABORT: 5201425Sroot msg("Child %d finishes X_ABORT\n", childpid); 5211425Sroot break; 5221425Sroot case X_REWRITE: 5231425Sroot msg("Child %d finishes X_REWRITE\n", childpid); 5241425Sroot break; 5251425Sroot default: 52618012Smckusick msg("Child %d finishes unknown %d\n", 52725219Smckusick childpid, status); 5281425Sroot break; 5291425Sroot } 5301425Sroot #endif TDEBUG 53118012Smckusick switch(status) { 5321425Sroot case X_FINOK: 5331425Sroot Exit(X_FINOK); 5341425Sroot case X_ABORT: 5351425Sroot Exit(X_ABORT); 5361425Sroot case X_REWRITE: 5371425Sroot goto restore_check_point; 5381425Sroot default: 5391425Sroot msg("Bad return code from dump: %d\n", status); 5401425Sroot Exit(X_ABORT); 5411425Sroot } 5421425Sroot /*NOTREACHED*/ 5431425Sroot } else { /* we are the child; just continue */ 5441425Sroot #ifdef TDEBUG 5451425Sroot sleep(4); /* allow time for parent's message to get out */ 5461425Sroot msg("Child on Tape %d has parent %d, my pid = %d\n", 5471425Sroot tapeno+1, parentpid, getpid()); 54825219Smckusick #endif TDEBUG 54947056Skarels /* 55047056Skarels * If we have a name like "/dev/rmt0,/dev/rmt1", 55147056Skarels * use the name before the comma first, and save 55248621Skarels * the remaining names for subsequent volumes. 55347056Skarels */ 55450504Smckusick tapeno++; /* current tape sequence */ 55548621Skarels if (nexttape || index(tape, ',')) { 55648621Skarels if (nexttape && *nexttape) 55748621Skarels tape = nexttape; 55848621Skarels if (p = index(tape, ',')) { 55948621Skarels *p = '\0'; 56048621Skarels nexttape = p + 1; 56148621Skarels } else 56248621Skarels nexttape = NULL; 56348621Skarels msg("Dumping volume %d on %s\n", tapeno, tape); 56448621Skarels } 56518012Smckusick #ifdef RDUMP 56646789Smckusick while ((tapefd = (host ? rmtopen(tape, 2) : 56746789Smckusick pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0) 56850504Smckusick #else 56950504Smckusick while ((tapefd = (pipeout ? 1 : 57050504Smckusick open(tape, O_WRONLY|O_CREAT, 0666))) < 0) 57150504Smckusick #endif 57239128Smckusick { 57346614Smckusick msg("Cannot open output \"%s\".\n", tape); 57439128Smckusick if (!query("Do you want to retry the open?")) 57518012Smckusick dumpabort(); 57639128Smckusick } 5771425Sroot 57818012Smckusick enslave(); /* Share open tape file descriptor with slaves */ 57918012Smckusick 5801425Sroot asize = 0; 58148621Skarels blocksthisvol = 0; 58250504Smckusick if (top) 58350504Smckusick newtape++; /* new tape signal */ 58450504Smckusick spcl.c_count = slp->count; 58550504Smckusick /* 58650504Smckusick * measure firstrec in TP_BSIZE units since restore doesn't 58750504Smckusick * know the correct ntrec value... 58850504Smckusick */ 58950504Smckusick spcl.c_firstrec = slp->firstrec; 5901425Sroot spcl.c_volume++; 5911425Sroot spcl.c_type = TS_TAPE; 59230432Smckusick spcl.c_flags |= DR_NEWHEADER; 59354047Smckusick writeheader((ino_t)slp->inode); 59430432Smckusick spcl.c_flags &=~ DR_NEWHEADER; 5951425Sroot if (tapeno > 1) 59648621Skarels msg("Volume %d begins with blocks from inode %d\n", 59750504Smckusick tapeno, slp->inode); 5981425Sroot } 5991425Sroot } 6001425Sroot 60146585Storek void 6021425Sroot dumpabort() 6031425Sroot { 60450504Smckusick 60518012Smckusick if (master != 0 && master != getpid()) 60654047Smckusick /* Signals master to call dumpabort */ 60754047Smckusick (void) kill(master, SIGTERM); 60824181Smckusick else { 60924181Smckusick killall(); 61024181Smckusick msg("The ENTIRE dump is aborted.\n"); 61124181Smckusick } 6121425Sroot Exit(X_ABORT); 6131425Sroot } 6141425Sroot 61546585Storek void 6161425Sroot Exit(status) 61746239Storek int status; 6181425Sroot { 61950504Smckusick 6201425Sroot #ifdef TDEBUG 6211425Sroot msg("pid = %d exits with status %d\n", getpid(), status); 6221425Sroot #endif TDEBUG 62354047Smckusick (void) exit(status); 6241425Sroot } 62518012Smckusick 62624181Smckusick /* 62750504Smckusick * proceed - handler for SIGUSR2, used to synchronize IO between the slaves. 62824181Smckusick */ 62946585Storek void 63050504Smckusick proceed() 63124181Smckusick { 63218012Smckusick 63350504Smckusick if (ready) 63450504Smckusick longjmp(jmpbuf, 1); 63550504Smckusick caught++; 63624181Smckusick } 63724181Smckusick 63846585Storek void 63918012Smckusick enslave() 64018012Smckusick { 64150504Smckusick int cmd[2]; 64224181Smckusick register int i, j; 64318012Smckusick 64418012Smckusick master = getpid(); 64550504Smckusick 64650504Smckusick signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ 64725219Smckusick signal(SIGPIPE, sigpipe); 64825219Smckusick signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ 64950504Smckusick signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */ 65050504Smckusick 65124181Smckusick for (i = 0; i < SLAVES; i++) { 65250504Smckusick if (i == slp - &slaves[0]) { 65350504Smckusick caught = 1; 65424181Smckusick } else { 65550504Smckusick caught = 0; 65624181Smckusick } 65750504Smckusick 65850504Smckusick if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 || 65950504Smckusick (slaves[i].pid = fork()) < 0) 66046585Storek quit("too many slaves, %d (recompile smaller): %s\n", 66146585Storek i, strerror(errno)); 66250504Smckusick 66350504Smckusick slaves[i].fd = cmd[1]; 66450504Smckusick slaves[i].sent = 0; 66550504Smckusick if (slaves[i].pid == 0) { /* Slave starts up here */ 66618012Smckusick for (j = 0; j <= i; j++) 66754047Smckusick (void) close(slaves[j].fd); 66825219Smckusick signal(SIGINT, SIG_IGN); /* Master handles this */ 66950504Smckusick doslave(cmd[0], i); 67018012Smckusick Exit(X_FINOK); 67118012Smckusick } 67218012Smckusick } 67350504Smckusick 67450504Smckusick for (i = 0; i < SLAVES; i++) 67554047Smckusick (void) atomic(write, slaves[i].fd, 67654047Smckusick (char *) &slaves[(i + 1) % SLAVES].pid, 67754047Smckusick sizeof slaves[0].pid); 67850504Smckusick 67950504Smckusick master = 0; 68018012Smckusick } 68118012Smckusick 68246585Storek void 68324181Smckusick killall() 68418012Smckusick { 68524181Smckusick register int i; 68619982Smckusick 68724181Smckusick for (i = 0; i < SLAVES; i++) 68850504Smckusick if (slaves[i].pid > 0) 68954047Smckusick (void) kill(slaves[i].pid, SIGKILL); 69018012Smckusick } 69118012Smckusick 69224181Smckusick /* 69324181Smckusick * Synchronization - each process has a lockfile, and shares file 69424181Smckusick * descriptors to the following process's lockfile. When our write 69524181Smckusick * completes, we release our lock on the following process's lock- 69624181Smckusick * file, allowing the following process to lock it and proceed. We 69724181Smckusick * get the lock back for the next cycle by swapping descriptors. 69824181Smckusick */ 69946585Storek void 70050504Smckusick doslave(cmd, slave_number) 70150504Smckusick register int cmd; 70250504Smckusick int slave_number; 70319982Smckusick { 70450504Smckusick register int nread; 70550504Smckusick int nextslave, size, wrote, eot_count; 70646795Sbostic #ifndef __STDC__ 70746795Sbostic int read(); 70846795Sbostic #endif 70919982Smckusick 71046789Smckusick /* 71146789Smckusick * Need our own seek pointer. 71246789Smckusick */ 71354047Smckusick (void) close(diskfd); 71446789Smckusick if ((diskfd = open(disk, O_RDONLY)) < 0) 71546585Storek quit("slave couldn't reopen disk: %s\n", strerror(errno)); 71650504Smckusick 71724181Smckusick /* 71850504Smckusick * Need the pid of the next slave in the loop... 71950504Smckusick */ 72054047Smckusick if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave)) 72150504Smckusick != sizeof nextslave) { 72250504Smckusick quit("master/slave protocol botched - didn't get pid of next slave.\n"); 72350504Smckusick } 72450504Smckusick 72550504Smckusick /* 72625219Smckusick * Get list of blocks to dump, read the blocks into tape buffer 72724181Smckusick */ 72854047Smckusick while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) { 72950504Smckusick register struct req *p = slp->req; 73050504Smckusick 73150504Smckusick for (trecno = 0; trecno < ntrec; 73250504Smckusick trecno += p->count, p += p->count) { 73318012Smckusick if (p->dblk) { 73450504Smckusick bread(p->dblk, slp->tblock[trecno], 73525219Smckusick p->count * TP_BSIZE); 73618012Smckusick } else { 73725219Smckusick if (p->count != 1 || atomic(read, cmd, 73854047Smckusick (char *)slp->tblock[trecno], 73954047Smckusick TP_BSIZE) != TP_BSIZE) 74054047Smckusick quit("master/slave protocol botched.\n"); 74118012Smckusick } 74218012Smckusick } 74350504Smckusick if (setjmp(jmpbuf) == 0) { 74450504Smckusick ready = 1; 74550504Smckusick if (!caught) 74654047Smckusick (void) pause(); 74750504Smckusick } 74850504Smckusick ready = 0; 74950504Smckusick caught = 0; 75025219Smckusick 75150504Smckusick /* Try to write the data... */ 75250504Smckusick eot_count = 0; 75350504Smckusick size = 0; 75450504Smckusick 75550504Smckusick while (eot_count < 10 && size < writesize) { 75618012Smckusick #ifdef RDUMP 75750504Smckusick if (host) 75850504Smckusick wrote = rmtwrite(slp->tblock[0]+size, 75950504Smckusick writesize-size); 76048621Skarels else 76150504Smckusick #endif 76250504Smckusick wrote = write(tapefd, slp->tblock[0]+size, 76350504Smckusick writesize-size); 76450504Smckusick #ifdef WRITEDEBUG 76550504Smckusick printf("slave %d wrote %d\n", slave_number, wrote); 76650504Smckusick #endif 76750504Smckusick if (wrote < 0) 76850504Smckusick break; 76950504Smckusick if (wrote == 0) 77050504Smckusick eot_count++; 77150504Smckusick size += wrote; 77250504Smckusick } 77350504Smckusick 77450504Smckusick #ifdef WRITEDEBUG 77550504Smckusick if (size != writesize) 77650504Smckusick printf("slave %d only wrote %d out of %d bytes and gave up.\n", 77750504Smckusick slave_number, size, writesize); 77850504Smckusick #endif 77950504Smckusick 78050504Smckusick if (eot_count > 0) 78150504Smckusick size = 0; 78250504Smckusick 78350504Smckusick /* 78450504Smckusick * fixme: Pyramids running OSx return ENOSPC 78550504Smckusick * at EOT on 1/2 inch drives. 78650504Smckusick */ 78750504Smckusick if (size < 0) { 78854047Smckusick (void) kill(master, SIGUSR1); 78925219Smckusick for (;;) 79054047Smckusick (void) sigpause(0); 79150504Smckusick } else { 79250504Smckusick /* 79350504Smckusick * pass size of write back to master 79450504Smckusick * (for EOT handling) 79550504Smckusick */ 79654047Smckusick (void) atomic(write, cmd, (char *)&size, sizeof size); 79750504Smckusick } 79850504Smckusick 79950504Smckusick /* 80050504Smckusick * If partial write, don't want next slave to go. 80150504Smckusick * Also jolts him awake. 80250504Smckusick */ 80354047Smckusick (void) kill(nextslave, SIGUSR2); 80450504Smckusick } 80546585Storek if (nread != 0) 80646585Storek quit("error reading command pipe: %s\n", strerror(errno)); 80718012Smckusick } 80819947Smckusick 80919947Smckusick /* 81025219Smckusick * Since a read from a pipe may not return all we asked for, 81125219Smckusick * or a write may not write all we ask if we get a signal, 81225219Smckusick * loop until the count is satisfied (or error). 81319947Smckusick */ 81446585Storek int 81525219Smckusick atomic(func, fd, buf, count) 81625219Smckusick int (*func)(), fd, count; 81719947Smckusick char *buf; 81819947Smckusick { 81925219Smckusick int got, need = count; 82019947Smckusick 82125219Smckusick while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) 82219947Smckusick buf += got; 82325219Smckusick return (got < 0 ? got : count - need); 82419947Smckusick } 825