18fae3551SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
48fae3551SRodney W. Grimes * Copyright (c) 1980, 1991, 1993
58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved.
68fae3551SRodney W. Grimes *
78fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
88fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions
98fae3551SRodney W. Grimes * are met:
108fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
118fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
128fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
138fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
148fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
168fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software
178fae3551SRodney W. Grimes * without specific prior written permission.
188fae3551SRodney W. Grimes *
198fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
208fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
218fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
228fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
238fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
248fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
258fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
268fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
278fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
288fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
298fae3551SRodney W. Grimes * SUCH DAMAGE.
308fae3551SRodney W. Grimes */
318fae3551SRodney W. Grimes
328fae3551SRodney W. Grimes #include <sys/param.h>
338fae3551SRodney W. Grimes #include <sys/socket.h>
348fae3551SRodney W. Grimes #include <sys/wait.h>
35ff5e109eSIan Dowse #include <sys/stat.h>
368fae3551SRodney W. Grimes
378fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
38a37c38b8SPeter Wemm #include <ufs/ffs/fs.h>
398fae3551SRodney W. Grimes
408fae3551SRodney W. Grimes #include <protocols/dumprestore.h>
418fae3551SRodney W. Grimes
422d518c65SWarner Losh #include <assert.h>
438fae3551SRodney W. Grimes #include <errno.h>
448fae3551SRodney W. Grimes #include <fcntl.h>
4589fdc4e1SMike Barcroft #include <limits.h>
468fae3551SRodney W. Grimes #include <setjmp.h>
478fae3551SRodney W. Grimes #include <signal.h>
488fae3551SRodney W. Grimes #include <stdio.h>
498fae3551SRodney W. Grimes #include <stdlib.h>
508fae3551SRodney W. Grimes #include <string.h>
517649cb00SKirk McKusick #include <time.h>
528fae3551SRodney W. Grimes #include <unistd.h>
538fae3551SRodney W. Grimes
548fae3551SRodney W. Grimes #include "dump.h"
558fae3551SRodney W. Grimes
5633ceb489SKirk McKusick ino_t curino; /* current inumber; used globally */
5733ceb489SKirk McKusick int newtape; /* new tape flag */
5833ceb489SKirk McKusick union u_spcl u_spcl; /* mapping of variables in a control block */
5933ceb489SKirk McKusick
6033ceb489SKirk McKusick static int tapefd; /* tape file descriptor */
6133ceb489SKirk McKusick static long asize; /* number of 0.1" units written on cur tape */
6233ceb489SKirk McKusick static int writesize; /* size of malloc()ed buffer for tape */
6333ceb489SKirk McKusick static int64_t lastspclrec = -1; /* tape block number of last written header */
6433ceb489SKirk McKusick static int trecno = 0; /* next record to write in current block */
6533ceb489SKirk McKusick static long blocksthisvol; /* number of blocks on current output file */
6633ceb489SKirk McKusick static char *nexttape;
6733ceb489SKirk McKusick static FILE *popenfp = NULL;
688fae3551SRodney W. Grimes
69*e5d0d1c5SKirk McKusick static int atomic_read(int, void *, int);
70*e5d0d1c5SKirk McKusick static int atomic_write(int, const void *, int);
71a74534b1SConrad Meyer static void worker(int, int);
720c577f4fSColin Percival static void create_workers(void);
732db673abSWarner Losh static void flushtape(void);
742db673abSWarner Losh static void killall(void);
752db673abSWarner Losh static void rollforward(void);
768fae3551SRodney W. Grimes
778fae3551SRodney W. Grimes /*
788fae3551SRodney W. Grimes * Concurrent dump mods (Caltech) - disk block reading and tape writing
79a74534b1SConrad Meyer * are exported to several worker processes. While one worker writes the
808fae3551SRodney W. Grimes * tape, the others read disk blocks; they pass control of the tape in
818fae3551SRodney W. Grimes * a ring via signals. The parent process traverses the file system and
82a74534b1SConrad Meyer * sends writeheader()'s and lists of daddr's to the workers via pipes.
83a74534b1SConrad Meyer * The following structure defines the instruction packets sent to workers.
848fae3551SRodney W. Grimes */
858fae3551SRodney W. Grimes struct req {
861c85e6a3SKirk McKusick ufs2_daddr_t dblk;
878fae3551SRodney W. Grimes int count;
888fae3551SRodney W. Grimes };
8933ceb489SKirk McKusick static int reqsiz;
908fae3551SRodney W. Grimes
91a74534b1SConrad Meyer #define WORKERS 3 /* 1 worker writing, 1 reading, 1 for slack */
92a74534b1SConrad Meyer static struct worker {
931c85e6a3SKirk McKusick int64_t tapea; /* header number at start of this chunk */
941c85e6a3SKirk McKusick int64_t firstrec; /* record number of this block */
958fae3551SRodney W. Grimes int count; /* count to next header (used for TS_TAPE */
968fae3551SRodney W. Grimes /* after EOT) */
978fae3551SRodney W. Grimes int inode; /* inode that we are currently dealing with */
98a74534b1SConrad Meyer int fd; /* FD for this worker */
99a74534b1SConrad Meyer int pid; /* PID for this worker */
100a74534b1SConrad Meyer int sent; /* 1 == we've sent this worker requests */
1018fae3551SRodney W. Grimes char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
1028fae3551SRodney W. Grimes struct req *req; /* buffer for requests */
103a74534b1SConrad Meyer } workers[WORKERS+1];
1040c577f4fSColin Percival static struct worker *wp;
1058fae3551SRodney W. Grimes
10633ceb489SKirk McKusick static char (*nextblock)[TP_BSIZE];
1078fae3551SRodney W. Grimes
10833ceb489SKirk McKusick static int master; /* pid of master, for sending error signals */
10933ceb489SKirk McKusick static int tenths; /* length of tape used per block written */
110890005bfSIan Dowse static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
111890005bfSIan Dowse static volatile sig_atomic_t ready; /* reached the lock point without having */
112a74534b1SConrad Meyer /* received the SIGUSR2 signal from the prev worker? */
1138fae3551SRodney W. Grimes static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
114a74534b1SConrad Meyer /* SIGUSR2 arrives from the previous worker */
1158fae3551SRodney W. Grimes
1168fae3551SRodney W. Grimes int
alloctape(void)1172db673abSWarner Losh alloctape(void)
1188fae3551SRodney W. Grimes {
1198fae3551SRodney W. Grimes int pgoff = getpagesize() - 1;
1208fae3551SRodney W. Grimes char *buf;
1218fae3551SRodney W. Grimes int i;
1228fae3551SRodney W. Grimes
1238fae3551SRodney W. Grimes writesize = ntrec * TP_BSIZE;
1248fae3551SRodney W. Grimes reqsiz = (ntrec + 1) * sizeof(struct req);
1258fae3551SRodney W. Grimes /*
1268fae3551SRodney W. Grimes * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
1278fae3551SRodney W. Grimes * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
1288fae3551SRodney W. Grimes * repositioning after stopping, i.e, streaming mode, where the gap is
1298fae3551SRodney W. Grimes * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
1308fae3551SRodney W. Grimes */
131af00957eSJoerg Wunsch if (blocksperfile == 0 && !unlimited)
1328fae3551SRodney W. Grimes tenths = writesize / density +
1338fae3551SRodney W. Grimes (cartridge ? 16 : density == 625 ? 5 : 8);
1348fae3551SRodney W. Grimes /*
1358fae3551SRodney W. Grimes * Allocate tape buffer contiguous with the array of instruction
1368fae3551SRodney W. Grimes * packets, so flushtape() can write them together with one write().
1378fae3551SRodney W. Grimes * Align tape buffer on page boundary to speed up tape write().
1388fae3551SRodney W. Grimes */
139a74534b1SConrad Meyer for (i = 0; i <= WORKERS; i++) {
1408fae3551SRodney W. Grimes buf = (char *)
1418fae3551SRodney W. Grimes malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
1428fae3551SRodney W. Grimes if (buf == NULL)
1438fae3551SRodney W. Grimes return(0);
144a74534b1SConrad Meyer workers[i].tblock = (char (*)[TP_BSIZE])
1458fae3551SRodney W. Grimes (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
146a74534b1SConrad Meyer workers[i].req = (struct req *)workers[i].tblock - ntrec - 1;
1478fae3551SRodney W. Grimes }
1480c577f4fSColin Percival wp = &workers[0];
1490c577f4fSColin Percival wp->count = 1;
1500c577f4fSColin Percival wp->tapea = 0;
1510c577f4fSColin Percival wp->firstrec = 0;
1520c577f4fSColin Percival nextblock = wp->tblock;
1538fae3551SRodney W. Grimes return(1);
1548fae3551SRodney W. Grimes }
1558fae3551SRodney W. Grimes
1568fae3551SRodney W. Grimes void
writerec(char * dp,int isspcl)1572db673abSWarner Losh writerec(char *dp, int isspcl)
1588fae3551SRodney W. Grimes {
1598fae3551SRodney W. Grimes
1600c577f4fSColin Percival wp->req[trecno].dblk = (ufs2_daddr_t)0;
1610c577f4fSColin Percival wp->req[trecno].count = 1;
1622db673abSWarner Losh /* Can't do a structure assignment due to alignment problems */
1639b979919SMatt Jacob bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
1648fae3551SRodney W. Grimes if (isspcl)
1658fae3551SRodney W. Grimes lastspclrec = spcl.c_tapea;
1668fae3551SRodney W. Grimes trecno++;
1678fae3551SRodney W. Grimes spcl.c_tapea++;
1688fae3551SRodney W. Grimes if (trecno >= ntrec)
1698fae3551SRodney W. Grimes flushtape();
1708fae3551SRodney W. Grimes }
1718fae3551SRodney W. Grimes
1728fae3551SRodney W. Grimes void
dumpblock(ufs2_daddr_t blkno,int size)1731c85e6a3SKirk McKusick dumpblock(ufs2_daddr_t blkno, int size)
1748fae3551SRodney W. Grimes {
1751c85e6a3SKirk McKusick int avail, tpblks;
1761c85e6a3SKirk McKusick ufs2_daddr_t dblkno;
1778fae3551SRodney W. Grimes
1788fae3551SRodney W. Grimes dblkno = fsbtodb(sblock, blkno);
1798fae3551SRodney W. Grimes tpblks = size >> tp_bshift;
1808fae3551SRodney W. Grimes while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
1810c577f4fSColin Percival wp->req[trecno].dblk = dblkno;
1820c577f4fSColin Percival wp->req[trecno].count = avail;
1838fae3551SRodney W. Grimes trecno += avail;
1848fae3551SRodney W. Grimes spcl.c_tapea += avail;
1858fae3551SRodney W. Grimes if (trecno >= ntrec)
1868fae3551SRodney W. Grimes flushtape();
1878fae3551SRodney W. Grimes dblkno += avail << (tp_bshift - dev_bshift);
1888fae3551SRodney W. Grimes tpblks -= avail;
1898fae3551SRodney W. Grimes }
1908fae3551SRodney W. Grimes }
1918fae3551SRodney W. Grimes
1928fae3551SRodney W. Grimes int nogripe = 0;
1938fae3551SRodney W. Grimes
1948fae3551SRodney W. Grimes void
tperror(int signo __unused)1952db673abSWarner Losh tperror(int signo __unused)
1968fae3551SRodney W. Grimes {
1978fae3551SRodney W. Grimes
1988fae3551SRodney W. Grimes if (pipeout) {
1998fae3551SRodney W. Grimes msg("write error on %s\n", tape);
2008fae3551SRodney W. Grimes quit("Cannot recover\n");
2018fae3551SRodney W. Grimes /* NOTREACHED */
2028fae3551SRodney W. Grimes }
203325167c3SIan Dowse msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
2048fae3551SRodney W. Grimes broadcast("DUMP WRITE ERROR!\n");
2058fae3551SRodney W. Grimes if (!query("Do you want to restart?"))
2068fae3551SRodney W. Grimes dumpabort(0);
2078fae3551SRodney W. Grimes msg("Closing this volume. Prepare to restart with new media;\n");
2088fae3551SRodney W. Grimes msg("this dump volume will be rewritten.\n");
2098fae3551SRodney W. Grimes killall();
2108fae3551SRodney W. Grimes nogripe = 1;
2118fae3551SRodney W. Grimes close_rewind();
2128fae3551SRodney W. Grimes Exit(X_REWRITE);
2138fae3551SRodney W. Grimes }
2148fae3551SRodney W. Grimes
2158fae3551SRodney W. Grimes void
sigpipe(int signo __unused)2162db673abSWarner Losh sigpipe(int signo __unused)
2178fae3551SRodney W. Grimes {
2188fae3551SRodney W. Grimes
2198fae3551SRodney W. Grimes quit("Broken pipe\n");
2208fae3551SRodney W. Grimes }
2218fae3551SRodney W. Grimes
2228fae3551SRodney W. Grimes static void
flushtape(void)2232db673abSWarner Losh flushtape(void)
2248fae3551SRodney W. Grimes {
2258fae3551SRodney W. Grimes int i, blks, got;
2261c85e6a3SKirk McKusick int64_t lastfirstrec;
2278fae3551SRodney W. Grimes
2280c577f4fSColin Percival int siz = (char *)nextblock - (char *)wp->req;
2298fae3551SRodney W. Grimes
2300c577f4fSColin Percival wp->req[trecno].count = 0; /* Sentinel */
2318fae3551SRodney W. Grimes
232*e5d0d1c5SKirk McKusick if (atomic_write(wp->fd, (const void *)wp->req, siz) != siz)
2338fae3551SRodney W. Grimes quit("error writing command pipe: %s\n", strerror(errno));
2340c577f4fSColin Percival wp->sent = 1; /* we sent a request, read the response later */
2358fae3551SRodney W. Grimes
2360c577f4fSColin Percival lastfirstrec = wp->firstrec;
2378fae3551SRodney W. Grimes
2380c577f4fSColin Percival if (++wp >= &workers[WORKERS])
2390c577f4fSColin Percival wp = &workers[0];
2408fae3551SRodney W. Grimes
241a74534b1SConrad Meyer /* Read results back from next worker */
2420c577f4fSColin Percival if (wp->sent) {
243*e5d0d1c5SKirk McKusick if (atomic_read(wp->fd, (void *)&got, sizeof got)
2448fae3551SRodney W. Grimes != sizeof got) {
2458fae3551SRodney W. Grimes perror(" DUMP: error reading command pipe in master");
2468fae3551SRodney W. Grimes dumpabort(0);
2478fae3551SRodney W. Grimes }
2480c577f4fSColin Percival wp->sent = 0;
2498fae3551SRodney W. Grimes
2508fae3551SRodney W. Grimes /* Check for end of tape */
2518fae3551SRodney W. Grimes if (got < writesize) {
2528fae3551SRodney W. Grimes msg("End of tape detected\n");
2538fae3551SRodney W. Grimes
2548fae3551SRodney W. Grimes /*
2558fae3551SRodney W. Grimes * Drain the results, don't care what the values were.
2568fae3551SRodney W. Grimes * If we read them here then trewind won't...
2578fae3551SRodney W. Grimes */
258a74534b1SConrad Meyer for (i = 0; i < WORKERS; i++) {
259a74534b1SConrad Meyer if (workers[i].sent) {
260*e5d0d1c5SKirk McKusick if (atomic_read(workers[i].fd,
261*e5d0d1c5SKirk McKusick (void *)&got, sizeof got)
2628fae3551SRodney W. Grimes != sizeof got) {
2638fae3551SRodney W. Grimes perror(" DUMP: error reading command pipe in master");
2648fae3551SRodney W. Grimes dumpabort(0);
2658fae3551SRodney W. Grimes }
266a74534b1SConrad Meyer workers[i].sent = 0;
2678fae3551SRodney W. Grimes }
2688fae3551SRodney W. Grimes }
2698fae3551SRodney W. Grimes
2708fae3551SRodney W. Grimes close_rewind();
2718fae3551SRodney W. Grimes rollforward();
2728fae3551SRodney W. Grimes return;
2738fae3551SRodney W. Grimes }
2748fae3551SRodney W. Grimes }
2758fae3551SRodney W. Grimes
2768fae3551SRodney W. Grimes blks = 0;
277b9c7cba5SWarner Losh if (spcl.c_type != TS_END && spcl.c_type != TS_CLRI &&
278b9c7cba5SWarner Losh spcl.c_type != TS_BITS) {
2792d518c65SWarner Losh assert(spcl.c_count <= TP_NINDIR);
2808fae3551SRodney W. Grimes for (i = 0; i < spcl.c_count; i++)
2818fae3551SRodney W. Grimes if (spcl.c_addr[i] != 0)
2828fae3551SRodney W. Grimes blks++;
2838fae3551SRodney W. Grimes }
2840c577f4fSColin Percival wp->count = lastspclrec + blks + 1 - spcl.c_tapea;
2850c577f4fSColin Percival wp->tapea = spcl.c_tapea;
2860c577f4fSColin Percival wp->firstrec = lastfirstrec + ntrec;
2870c577f4fSColin Percival wp->inode = curino;
2880c577f4fSColin Percival nextblock = wp->tblock;
2898fae3551SRodney W. Grimes trecno = 0;
2908fae3551SRodney W. Grimes asize += tenths;
2918fae3551SRodney W. Grimes blockswritten += ntrec;
2928fae3551SRodney W. Grimes blocksthisvol += ntrec;
293af00957eSJoerg Wunsch if (!pipeout && !unlimited && (blocksperfile ?
2948fae3551SRodney W. Grimes (blocksthisvol >= blocksperfile) : (asize > tsize))) {
2958fae3551SRodney W. Grimes close_rewind();
2968fae3551SRodney W. Grimes startnewtape(0);
2978fae3551SRodney W. Grimes }
2988fae3551SRodney W. Grimes timeest();
2998fae3551SRodney W. Grimes }
3008fae3551SRodney W. Grimes
3018fae3551SRodney W. Grimes void
trewind(void)3022db673abSWarner Losh trewind(void)
3038fae3551SRodney W. Grimes {
304ff5e109eSIan Dowse struct stat sb;
3058fae3551SRodney W. Grimes int f;
3068fae3551SRodney W. Grimes int got;
3078fae3551SRodney W. Grimes
308a74534b1SConrad Meyer for (f = 0; f < WORKERS; f++) {
3098fae3551SRodney W. Grimes /*
3108fae3551SRodney W. Grimes * Drain the results, but unlike EOT we DO (or should) care
3118fae3551SRodney W. Grimes * what the return values were, since if we detect EOT after
3128fae3551SRodney W. Grimes * we think we've written the last blocks to the tape anyway,
3138fae3551SRodney W. Grimes * we have to replay those blocks with rollforward.
3148fae3551SRodney W. Grimes *
3158fae3551SRodney W. Grimes * fixme: punt for now.
3168fae3551SRodney W. Grimes */
317a74534b1SConrad Meyer if (workers[f].sent) {
318*e5d0d1c5SKirk McKusick if (atomic_read(workers[f].fd, (void *)&got, sizeof got)
3198fae3551SRodney W. Grimes != sizeof got) {
3208fae3551SRodney W. Grimes perror(" DUMP: error reading command pipe in master");
3218fae3551SRodney W. Grimes dumpabort(0);
3228fae3551SRodney W. Grimes }
323a74534b1SConrad Meyer workers[f].sent = 0;
3248fae3551SRodney W. Grimes if (got != writesize) {
3258fae3551SRodney W. Grimes msg("EOT detected in last 2 tape records!\n");
3268fae3551SRodney W. Grimes msg("Use a longer tape, decrease the size estimate\n");
3278fae3551SRodney W. Grimes quit("or use no size estimate at all.\n");
3288fae3551SRodney W. Grimes }
3298fae3551SRodney W. Grimes }
330a74534b1SConrad Meyer (void) close(workers[f].fd);
3318fae3551SRodney W. Grimes }
332a74534b1SConrad Meyer while (wait((int *)NULL) >= 0) /* wait for any signals from workers */
3338fae3551SRodney W. Grimes /* void */;
3348fae3551SRodney W. Grimes
3358fae3551SRodney W. Grimes if (pipeout)
3368fae3551SRodney W. Grimes return;
3378fae3551SRodney W. Grimes
3388fae3551SRodney W. Grimes msg("Closing %s\n", tape);
3398fae3551SRodney W. Grimes
340c51d70c6SBrian Feldman if (popenout) {
341c51d70c6SBrian Feldman tapefd = -1;
342c51d70c6SBrian Feldman (void)pclose(popenfp);
343c51d70c6SBrian Feldman popenfp = NULL;
344c51d70c6SBrian Feldman return;
345c51d70c6SBrian Feldman }
3468fae3551SRodney W. Grimes #ifdef RDUMP
3478fae3551SRodney W. Grimes if (host) {
3488fae3551SRodney W. Grimes rmtclose();
3498fae3551SRodney W. Grimes while (rmtopen(tape, 0) < 0)
3508fae3551SRodney W. Grimes sleep(10);
3518fae3551SRodney W. Grimes rmtclose();
3528fae3551SRodney W. Grimes return;
3538fae3551SRodney W. Grimes }
3548fae3551SRodney W. Grimes #endif
355ff5e109eSIan Dowse if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
356ff5e109eSIan Dowse (void)close(tapefd);
357ff5e109eSIan Dowse return;
358ff5e109eSIan Dowse }
3598fae3551SRodney W. Grimes (void) close(tapefd);
3608fae3551SRodney W. Grimes while ((f = open(tape, 0)) < 0)
3618fae3551SRodney W. Grimes sleep (10);
3628fae3551SRodney W. Grimes (void) close(f);
3638fae3551SRodney W. Grimes }
3648fae3551SRodney W. Grimes
3658fae3551SRodney W. Grimes void
close_rewind()3668fae3551SRodney W. Grimes close_rewind()
3678fae3551SRodney W. Grimes {
368d6038ed6SJoerg Wunsch time_t tstart_changevol, tend_changevol;
369d6038ed6SJoerg Wunsch
3708fae3551SRodney W. Grimes trewind();
3718fae3551SRodney W. Grimes if (nexttape)
3728fae3551SRodney W. Grimes return;
373d6038ed6SJoerg Wunsch (void)time((time_t *)&(tstart_changevol));
3748fae3551SRodney W. Grimes if (!nogripe) {
3758fae3551SRodney W. Grimes msg("Change Volumes: Mount volume #%d\n", tapeno+1);
376bfa0b298SPoul-Henning Kamp broadcast("CHANGE DUMP VOLUMES!\a\a\n");
3778fae3551SRodney W. Grimes }
3788fae3551SRodney W. Grimes while (!query("Is the new volume mounted and ready to go?"))
3798fae3551SRodney W. Grimes if (query("Do you want to abort?")) {
3808fae3551SRodney W. Grimes dumpabort(0);
3818fae3551SRodney W. Grimes /*NOTREACHED*/
3828fae3551SRodney W. Grimes }
383d6038ed6SJoerg Wunsch (void)time((time_t *)&(tend_changevol));
384d6038ed6SJoerg Wunsch if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
385d6038ed6SJoerg Wunsch tstart_writing += (tend_changevol - tstart_changevol);
3868fae3551SRodney W. Grimes }
3878fae3551SRodney W. Grimes
3888fae3551SRodney W. Grimes void
rollforward(void)3892db673abSWarner Losh rollforward(void)
3908fae3551SRodney W. Grimes {
391d2334e27SIan Dowse struct req *p, *q, *prev;
3920c577f4fSColin Percival struct worker *twp;
3931c85e6a3SKirk McKusick int i, size, got;
3941c85e6a3SKirk McKusick int64_t savedtapea;
3958fae3551SRodney W. Grimes union u_spcl *ntb, *otb;
3960c577f4fSColin Percival twp = &workers[WORKERS];
3970c577f4fSColin Percival ntb = (union u_spcl *)twp->tblock[1];
3988fae3551SRodney W. Grimes
3998fae3551SRodney W. Grimes /*
400a74534b1SConrad Meyer * Each of the N workers should have requests that need to
401a74534b1SConrad Meyer * be replayed on the next tape. Use the extra worker buffers
402a74534b1SConrad Meyer * (workers[WORKERS]) to construct request lists to be sent to
403a74534b1SConrad Meyer * each worker in turn.
4048fae3551SRodney W. Grimes */
405a74534b1SConrad Meyer for (i = 0; i < WORKERS; i++) {
4060c577f4fSColin Percival q = &twp->req[1];
4070c577f4fSColin Percival otb = (union u_spcl *)wp->tblock;
4088fae3551SRodney W. Grimes
4098fae3551SRodney W. Grimes /*
4100c577f4fSColin Percival * For each request in the current worker, copy it to twp.
4118fae3551SRodney W. Grimes */
4128fae3551SRodney W. Grimes
4138fae3551SRodney W. Grimes prev = NULL;
4140c577f4fSColin Percival for (p = wp->req; p->count > 0; p += p->count) {
4158fae3551SRodney W. Grimes *q = *p;
4168fae3551SRodney W. Grimes if (p->dblk == 0)
4178fae3551SRodney W. Grimes *ntb++ = *otb++; /* copy the datablock also */
4188fae3551SRodney W. Grimes prev = q;
4198fae3551SRodney W. Grimes q += q->count;
4208fae3551SRodney W. Grimes }
4218fae3551SRodney W. Grimes if (prev == NULL)
4228fae3551SRodney W. Grimes quit("rollforward: protocol botch");
4238fae3551SRodney W. Grimes if (prev->dblk != 0)
4248fae3551SRodney W. Grimes prev->count -= 1;
4258fae3551SRodney W. Grimes else
4268fae3551SRodney W. Grimes ntb--;
4278fae3551SRodney W. Grimes q -= 1;
4288fae3551SRodney W. Grimes q->count = 0;
4290c577f4fSColin Percival q = &twp->req[0];
4308fae3551SRodney W. Grimes if (i == 0) {
4318fae3551SRodney W. Grimes q->dblk = 0;
4328fae3551SRodney W. Grimes q->count = 1;
4338fae3551SRodney W. Grimes trecno = 0;
4340c577f4fSColin Percival nextblock = twp->tblock;
4358fae3551SRodney W. Grimes savedtapea = spcl.c_tapea;
4360c577f4fSColin Percival spcl.c_tapea = wp->tapea;
4378fae3551SRodney W. Grimes startnewtape(0);
4388fae3551SRodney W. Grimes spcl.c_tapea = savedtapea;
4398fae3551SRodney W. Grimes lastspclrec = savedtapea - 1;
4408fae3551SRodney W. Grimes }
4418fae3551SRodney W. Grimes size = (char *)ntb - (char *)q;
442*e5d0d1c5SKirk McKusick if (atomic_write(wp->fd, (const void *)q, size) != size) {
4438fae3551SRodney W. Grimes perror(" DUMP: error writing command pipe");
4448fae3551SRodney W. Grimes dumpabort(0);
4458fae3551SRodney W. Grimes }
4460c577f4fSColin Percival wp->sent = 1;
4470c577f4fSColin Percival if (++wp >= &workers[WORKERS])
4480c577f4fSColin Percival wp = &workers[0];
4498fae3551SRodney W. Grimes
4508fae3551SRodney W. Grimes q->count = 1;
4518fae3551SRodney W. Grimes
4528fae3551SRodney W. Grimes if (prev->dblk != 0) {
4538fae3551SRodney W. Grimes /*
4548fae3551SRodney W. Grimes * If the last one was a disk block, make the
4558fae3551SRodney W. Grimes * first of this one be the last bit of that disk
4568fae3551SRodney W. Grimes * block...
4578fae3551SRodney W. Grimes */
4588fae3551SRodney W. Grimes q->dblk = prev->dblk +
4598fae3551SRodney W. Grimes prev->count * (TP_BSIZE / DEV_BSIZE);
4600c577f4fSColin Percival ntb = (union u_spcl *)twp->tblock;
4618fae3551SRodney W. Grimes } else {
4628fae3551SRodney W. Grimes /*
4638fae3551SRodney W. Grimes * It wasn't a disk block. Copy the data to its
4648fae3551SRodney W. Grimes * new location in the buffer.
4658fae3551SRodney W. Grimes */
4668fae3551SRodney W. Grimes q->dblk = 0;
4670c577f4fSColin Percival *((union u_spcl *)twp->tblock) = *ntb;
4680c577f4fSColin Percival ntb = (union u_spcl *)twp->tblock[1];
4698fae3551SRodney W. Grimes }
4708fae3551SRodney W. Grimes }
4710c577f4fSColin Percival wp->req[0] = *q;
4720c577f4fSColin Percival nextblock = wp->tblock;
4738fae3551SRodney W. Grimes if (q->dblk == 0)
4748fae3551SRodney W. Grimes nextblock++;
4758fae3551SRodney W. Grimes trecno = 1;
4768fae3551SRodney W. Grimes
4778fae3551SRodney W. Grimes /*
478a74534b1SConrad Meyer * Clear the first workers' response. One hopes that it
4798fae3551SRodney W. Grimes * worked ok, otherwise the tape is much too short!
4808fae3551SRodney W. Grimes */
4810c577f4fSColin Percival if (wp->sent) {
482*e5d0d1c5SKirk McKusick if (atomic_read(wp->fd, (void *)&got, sizeof got)
4838fae3551SRodney W. Grimes != sizeof got) {
4848fae3551SRodney W. Grimes perror(" DUMP: error reading command pipe in master");
4858fae3551SRodney W. Grimes dumpabort(0);
4868fae3551SRodney W. Grimes }
4870c577f4fSColin Percival wp->sent = 0;
4888fae3551SRodney W. Grimes
4898fae3551SRodney W. Grimes if (got != writesize) {
4908fae3551SRodney W. Grimes quit("EOT detected at start of the tape!\n");
4918fae3551SRodney W. Grimes }
4928fae3551SRodney W. Grimes }
4938fae3551SRodney W. Grimes }
4948fae3551SRodney W. Grimes
4958fae3551SRodney W. Grimes /*
4968fae3551SRodney W. Grimes * We implement taking and restoring checkpoints on the tape level.
4978fae3551SRodney W. Grimes * When each tape is opened, a new process is created by forking; this
4988fae3551SRodney W. Grimes * saves all of the necessary context in the parent. The child
4998fae3551SRodney W. Grimes * continues the dump; the parent waits around, saving the context.
5008fae3551SRodney W. Grimes * If the child returns X_REWRITE, then it had problems writing that tape;
5018fae3551SRodney W. Grimes * this causes the parent to fork again, duplicating the context, and
5028fae3551SRodney W. Grimes * everything continues as if nothing had happened.
5038fae3551SRodney W. Grimes */
5048fae3551SRodney W. Grimes void
startnewtape(int top)5052db673abSWarner Losh startnewtape(int top)
5068fae3551SRodney W. Grimes {
5078fae3551SRodney W. Grimes int parentpid;
5088fae3551SRodney W. Grimes int childpid;
5098fae3551SRodney W. Grimes int status;
5108fae3551SRodney W. Grimes char *p;
5118fae3551SRodney W. Grimes sig_t interrupt_save;
5128fae3551SRodney W. Grimes
5138fae3551SRodney W. Grimes interrupt_save = signal(SIGINT, SIG_IGN);
5148fae3551SRodney W. Grimes parentpid = getpid();
5158fae3551SRodney W. Grimes
5168fae3551SRodney W. Grimes restore_check_point:
5178fae3551SRodney W. Grimes (void)signal(SIGINT, interrupt_save);
5188fae3551SRodney W. Grimes /*
5198fae3551SRodney W. Grimes * All signals are inherited...
5208fae3551SRodney W. Grimes */
5212bb823d2SIan Dowse setproctitle(NULL); /* Restore the proctitle. */
5228fae3551SRodney W. Grimes childpid = fork();
5238fae3551SRodney W. Grimes if (childpid < 0) {
5248fae3551SRodney W. Grimes msg("Context save fork fails in parent %d\n", parentpid);
5258fae3551SRodney W. Grimes Exit(X_ABORT);
5268fae3551SRodney W. Grimes }
5278fae3551SRodney W. Grimes if (childpid != 0) {
5288fae3551SRodney W. Grimes /*
5298fae3551SRodney W. Grimes * PARENT:
5308fae3551SRodney W. Grimes * save the context by waiting
5318fae3551SRodney W. Grimes * until the child doing all of the work returns.
5328fae3551SRodney W. Grimes * don't catch the interrupt
5338fae3551SRodney W. Grimes */
5348fae3551SRodney W. Grimes signal(SIGINT, SIG_IGN);
5358fae3551SRodney W. Grimes #ifdef TDEBUG
5368fae3551SRodney W. Grimes msg("Tape: %d; parent process: %d child process %d\n",
5378fae3551SRodney W. Grimes tapeno+1, parentpid, childpid);
5388fae3551SRodney W. Grimes #endif /* TDEBUG */
539c51d70c6SBrian Feldman if (waitpid(childpid, &status, 0) == -1)
540c51d70c6SBrian Feldman msg("Waiting for child %d: %s\n", childpid,
541c51d70c6SBrian Feldman strerror(errno));
5428fae3551SRodney W. Grimes if (status & 0xFF) {
5438fae3551SRodney W. Grimes msg("Child %d returns LOB status %o\n",
5448fae3551SRodney W. Grimes childpid, status&0xFF);
5458fae3551SRodney W. Grimes }
5468fae3551SRodney W. Grimes status = (status >> 8) & 0xFF;
5478fae3551SRodney W. Grimes #ifdef TDEBUG
5488fae3551SRodney W. Grimes switch(status) {
5498fae3551SRodney W. Grimes case X_FINOK:
5508fae3551SRodney W. Grimes msg("Child %d finishes X_FINOK\n", childpid);
5518fae3551SRodney W. Grimes break;
5528fae3551SRodney W. Grimes case X_ABORT:
5538fae3551SRodney W. Grimes msg("Child %d finishes X_ABORT\n", childpid);
5548fae3551SRodney W. Grimes break;
5558fae3551SRodney W. Grimes case X_REWRITE:
5568fae3551SRodney W. Grimes msg("Child %d finishes X_REWRITE\n", childpid);
5578fae3551SRodney W. Grimes break;
5588fae3551SRodney W. Grimes default:
5598fae3551SRodney W. Grimes msg("Child %d finishes unknown %d\n",
5608fae3551SRodney W. Grimes childpid, status);
5618fae3551SRodney W. Grimes break;
5628fae3551SRodney W. Grimes }
5638fae3551SRodney W. Grimes #endif /* TDEBUG */
5648fae3551SRodney W. Grimes switch(status) {
5658fae3551SRodney W. Grimes case X_FINOK:
5668fae3551SRodney W. Grimes Exit(X_FINOK);
5678fae3551SRodney W. Grimes case X_ABORT:
5688fae3551SRodney W. Grimes Exit(X_ABORT);
5698fae3551SRodney W. Grimes case X_REWRITE:
5708fae3551SRodney W. Grimes goto restore_check_point;
5718fae3551SRodney W. Grimes default:
5728fae3551SRodney W. Grimes msg("Bad return code from dump: %d\n", status);
5738fae3551SRodney W. Grimes Exit(X_ABORT);
5748fae3551SRodney W. Grimes }
5758fae3551SRodney W. Grimes /*NOTREACHED*/
5768fae3551SRodney W. Grimes } else { /* we are the child; just continue */
5778fae3551SRodney W. Grimes #ifdef TDEBUG
5788fae3551SRodney W. Grimes sleep(4); /* allow time for parent's message to get out */
5798fae3551SRodney W. Grimes msg("Child on Tape %d has parent %d, my pid = %d\n",
5808fae3551SRodney W. Grimes tapeno+1, parentpid, getpid());
5818fae3551SRodney W. Grimes #endif /* TDEBUG */
5828fae3551SRodney W. Grimes /*
5838fae3551SRodney W. Grimes * If we have a name like "/dev/rmt0,/dev/rmt1",
5848fae3551SRodney W. Grimes * use the name before the comma first, and save
5858fae3551SRodney W. Grimes * the remaining names for subsequent volumes.
5868fae3551SRodney W. Grimes */
5878fae3551SRodney W. Grimes tapeno++; /* current tape sequence */
588a37c38b8SPeter Wemm if (nexttape || strchr(tape, ',')) {
5898fae3551SRodney W. Grimes if (nexttape && *nexttape)
5908fae3551SRodney W. Grimes tape = nexttape;
591a37c38b8SPeter Wemm if ((p = strchr(tape, ',')) != NULL) {
5928fae3551SRodney W. Grimes *p = '\0';
5938fae3551SRodney W. Grimes nexttape = p + 1;
5948fae3551SRodney W. Grimes } else
5958fae3551SRodney W. Grimes nexttape = NULL;
5968fae3551SRodney W. Grimes msg("Dumping volume %d on %s\n", tapeno, tape);
5978fae3551SRodney W. Grimes }
598c51d70c6SBrian Feldman if (pipeout) {
599c51d70c6SBrian Feldman tapefd = STDOUT_FILENO;
600c51d70c6SBrian Feldman } else if (popenout) {
601c51d70c6SBrian Feldman char volno[sizeof("2147483647")];
602c51d70c6SBrian Feldman
603c51d70c6SBrian Feldman (void)sprintf(volno, "%d", spcl.c_volume + 1);
604c51d70c6SBrian Feldman if (setenv("DUMP_VOLUME", volno, 1) == -1) {
605c51d70c6SBrian Feldman msg("Cannot set $DUMP_VOLUME.\n");
606c51d70c6SBrian Feldman dumpabort(0);
607c51d70c6SBrian Feldman }
608c51d70c6SBrian Feldman popenfp = popen(popenout, "w");
609c51d70c6SBrian Feldman if (popenfp == NULL) {
610c51d70c6SBrian Feldman msg("Cannot open output pipeline \"%s\".\n",
611c51d70c6SBrian Feldman popenout);
612c51d70c6SBrian Feldman dumpabort(0);
613c51d70c6SBrian Feldman }
614c51d70c6SBrian Feldman tapefd = fileno(popenfp);
615c51d70c6SBrian Feldman } else {
6168fae3551SRodney W. Grimes #ifdef RDUMP
6178fae3551SRodney W. Grimes while ((tapefd = (host ? rmtopen(tape, 2) :
6188fae3551SRodney W. Grimes open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
619c51d70c6SBrian Feldman #else
620c51d70c6SBrian Feldman while ((tapefd =
621c51d70c6SBrian Feldman open(tape, O_WRONLY|O_CREAT, 0666)) < 0)
6228fae3551SRodney W. Grimes #endif
6238fae3551SRodney W. Grimes {
6248fae3551SRodney W. Grimes msg("Cannot open output \"%s\".\n", tape);
6258fae3551SRodney W. Grimes if (!query("Do you want to retry the open?"))
6268fae3551SRodney W. Grimes dumpabort(0);
6278fae3551SRodney W. Grimes }
628c51d70c6SBrian Feldman }
6298fae3551SRodney W. Grimes
6300c577f4fSColin Percival create_workers(); /* Share open tape file descriptor with workers */
631c51d70c6SBrian Feldman if (popenout)
632c51d70c6SBrian Feldman close(tapefd); /* Give up our copy of it. */
63319f8080eSIan Dowse signal(SIGINFO, infosch);
6348fae3551SRodney W. Grimes
6358fae3551SRodney W. Grimes asize = 0;
6368fae3551SRodney W. Grimes blocksthisvol = 0;
6378fae3551SRodney W. Grimes if (top)
6388fae3551SRodney W. Grimes newtape++; /* new tape signal */
6390c577f4fSColin Percival spcl.c_count = wp->count;
6408fae3551SRodney W. Grimes /*
6418fae3551SRodney W. Grimes * measure firstrec in TP_BSIZE units since restore doesn't
6428fae3551SRodney W. Grimes * know the correct ntrec value...
6438fae3551SRodney W. Grimes */
6440c577f4fSColin Percival spcl.c_firstrec = wp->firstrec;
6458fae3551SRodney W. Grimes spcl.c_volume++;
6468fae3551SRodney W. Grimes spcl.c_type = TS_TAPE;
6470c577f4fSColin Percival writeheader((ino_t)wp->inode);
6488fae3551SRodney W. Grimes if (tapeno > 1)
6498fae3551SRodney W. Grimes msg("Volume %d begins with blocks from inode %d\n",
6500c577f4fSColin Percival tapeno, wp->inode);
6518fae3551SRodney W. Grimes }
6528fae3551SRodney W. Grimes }
6538fae3551SRodney W. Grimes
6548fae3551SRodney W. Grimes void
dumpabort(int signo __unused)6552db673abSWarner Losh dumpabort(int signo __unused)
6568fae3551SRodney W. Grimes {
6578fae3551SRodney W. Grimes
6588fae3551SRodney W. Grimes if (master != 0 && master != getpid())
6598fae3551SRodney W. Grimes /* Signals master to call dumpabort */
6608fae3551SRodney W. Grimes (void) kill(master, SIGTERM);
6618fae3551SRodney W. Grimes else {
6628fae3551SRodney W. Grimes killall();
6638fae3551SRodney W. Grimes msg("The ENTIRE dump is aborted.\n");
6648fae3551SRodney W. Grimes }
6658fae3551SRodney W. Grimes #ifdef RDUMP
6668fae3551SRodney W. Grimes rmtclose();
6678fae3551SRodney W. Grimes #endif
6688fae3551SRodney W. Grimes Exit(X_ABORT);
6698fae3551SRodney W. Grimes }
6708fae3551SRodney W. Grimes
671eaa86f9dSBruce Evans void
Exit(int status)672*e5d0d1c5SKirk McKusick Exit(int status)
6738fae3551SRodney W. Grimes {
6748fae3551SRodney W. Grimes
6758fae3551SRodney W. Grimes #ifdef TDEBUG
6768fae3551SRodney W. Grimes msg("pid = %d exits with status %d\n", getpid(), status);
6778fae3551SRodney W. Grimes #endif /* TDEBUG */
6788fae3551SRodney W. Grimes exit(status);
6798fae3551SRodney W. Grimes }
6808fae3551SRodney W. Grimes
6818fae3551SRodney W. Grimes /*
682a74534b1SConrad Meyer * proceed - handler for SIGUSR2, used to synchronize IO between the workers.
6838fae3551SRodney W. Grimes */
6848fae3551SRodney W. Grimes void
proceed(int signo __unused)6852db673abSWarner Losh proceed(int signo __unused)
6868fae3551SRodney W. Grimes {
6878fae3551SRodney W. Grimes
6888fae3551SRodney W. Grimes if (ready)
6898fae3551SRodney W. Grimes longjmp(jmpbuf, 1);
6908fae3551SRodney W. Grimes caught++;
6918fae3551SRodney W. Grimes }
6928fae3551SRodney W. Grimes
6938fae3551SRodney W. Grimes void
create_workers(void)6940c577f4fSColin Percival create_workers(void)
6958fae3551SRodney W. Grimes {
6968fae3551SRodney W. Grimes int cmd[2];
697d2334e27SIan Dowse int i, j;
6988fae3551SRodney W. Grimes
6998fae3551SRodney W. Grimes master = getpid();
7008fae3551SRodney W. Grimes
701a74534b1SConrad Meyer signal(SIGTERM, dumpabort); /* Worker sends SIGTERM on dumpabort() */
7028fae3551SRodney W. Grimes signal(SIGPIPE, sigpipe);
703a74534b1SConrad Meyer signal(SIGUSR1, tperror); /* Worker sends SIGUSR1 on tape errors */
704a74534b1SConrad Meyer signal(SIGUSR2, proceed); /* Worker sends SIGUSR2 to next worker */
7058fae3551SRodney W. Grimes
706a74534b1SConrad Meyer for (i = 0; i < WORKERS; i++) {
7070c577f4fSColin Percival if (i == wp - &workers[0]) {
7088fae3551SRodney W. Grimes caught = 1;
7098fae3551SRodney W. Grimes } else {
7108fae3551SRodney W. Grimes caught = 0;
7118fae3551SRodney W. Grimes }
7128fae3551SRodney W. Grimes
7138fae3551SRodney W. Grimes if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
714a74534b1SConrad Meyer (workers[i].pid = fork()) < 0)
715a74534b1SConrad Meyer quit("too many workers, %d (recompile smaller): %s\n",
7168fae3551SRodney W. Grimes i, strerror(errno));
7178fae3551SRodney W. Grimes
718a74534b1SConrad Meyer workers[i].fd = cmd[1];
719a74534b1SConrad Meyer workers[i].sent = 0;
720a74534b1SConrad Meyer if (workers[i].pid == 0) { /* Worker starts up here */
7218fae3551SRodney W. Grimes for (j = 0; j <= i; j++)
722a74534b1SConrad Meyer (void) close(workers[j].fd);
7238fae3551SRodney W. Grimes signal(SIGINT, SIG_IGN); /* Master handles this */
724a74534b1SConrad Meyer worker(cmd[0], i);
7258fae3551SRodney W. Grimes Exit(X_FINOK);
7268fae3551SRodney W. Grimes }
7278fae3551SRodney W. Grimes }
7288fae3551SRodney W. Grimes
729a74534b1SConrad Meyer for (i = 0; i < WORKERS; i++)
730*e5d0d1c5SKirk McKusick (void) atomic_write(workers[i].fd,
731*e5d0d1c5SKirk McKusick (const void *) &workers[(i + 1) % WORKERS].pid,
732a74534b1SConrad Meyer sizeof workers[0].pid);
7338fae3551SRodney W. Grimes
7348fae3551SRodney W. Grimes master = 0;
7358fae3551SRodney W. Grimes }
7368fae3551SRodney W. Grimes
7378fae3551SRodney W. Grimes void
killall(void)7382db673abSWarner Losh killall(void)
7398fae3551SRodney W. Grimes {
740d2334e27SIan Dowse int i;
7418fae3551SRodney W. Grimes
742a74534b1SConrad Meyer for (i = 0; i < WORKERS; i++)
743a74534b1SConrad Meyer if (workers[i].pid > 0) {
744a74534b1SConrad Meyer (void) kill(workers[i].pid, SIGKILL);
745a74534b1SConrad Meyer workers[i].sent = 0;
746ee359f48SJoerg Wunsch }
7478fae3551SRodney W. Grimes }
7488fae3551SRodney W. Grimes
7498fae3551SRodney W. Grimes /*
7508fae3551SRodney W. Grimes * Synchronization - each process has a lockfile, and shares file
7518fae3551SRodney W. Grimes * descriptors to the following process's lockfile. When our write
7528fae3551SRodney W. Grimes * completes, we release our lock on the following process's lock-
7538fae3551SRodney W. Grimes * file, allowing the following process to lock it and proceed. We
7548fae3551SRodney W. Grimes * get the lock back for the next cycle by swapping descriptors.
7558fae3551SRodney W. Grimes */
7568fae3551SRodney W. Grimes static void
worker(int cmd,int worker_number)757a74534b1SConrad Meyer worker(int cmd, int worker_number)
7588fae3551SRodney W. Grimes {
759d2334e27SIan Dowse int nread;
760a74534b1SConrad Meyer int nextworker, size, wrote, eot_count;
7618fae3551SRodney W. Grimes
7628fae3551SRodney W. Grimes /*
7638fae3551SRodney W. Grimes * Need our own seek pointer.
7648fae3551SRodney W. Grimes */
7658fae3551SRodney W. Grimes (void) close(diskfd);
7668fae3551SRodney W. Grimes if ((diskfd = open(disk, O_RDONLY)) < 0)
767a74534b1SConrad Meyer quit("worker couldn't reopen disk: %s\n", strerror(errno));
7688fae3551SRodney W. Grimes
7698fae3551SRodney W. Grimes /*
770a74534b1SConrad Meyer * Need the pid of the next worker in the loop...
7718fae3551SRodney W. Grimes */
772*e5d0d1c5SKirk McKusick if ((nread = atomic_read(cmd, (void *)&nextworker, sizeof nextworker))
773a74534b1SConrad Meyer != sizeof nextworker) {
774a74534b1SConrad Meyer quit("master/worker protocol botched - didn't get pid of next worker.\n");
7758fae3551SRodney W. Grimes }
7768fae3551SRodney W. Grimes
7778fae3551SRodney W. Grimes /*
7788fae3551SRodney W. Grimes * Get list of blocks to dump, read the blocks into tape buffer
7798fae3551SRodney W. Grimes */
780*e5d0d1c5SKirk McKusick while ((nread = atomic_read(cmd, (void *)wp->req, reqsiz)) == reqsiz) {
7810c577f4fSColin Percival struct req *p = wp->req;
7828fae3551SRodney W. Grimes
7838fae3551SRodney W. Grimes for (trecno = 0; trecno < ntrec;
7848fae3551SRodney W. Grimes trecno += p->count, p += p->count) {
7858fae3551SRodney W. Grimes if (p->dblk) {
7860c577f4fSColin Percival blkread(p->dblk, wp->tblock[trecno],
7878fae3551SRodney W. Grimes p->count * TP_BSIZE);
7888fae3551SRodney W. Grimes } else {
789*e5d0d1c5SKirk McKusick if (p->count != 1 || atomic_read(cmd,
790*e5d0d1c5SKirk McKusick (void *)wp->tblock[trecno],
7918fae3551SRodney W. Grimes TP_BSIZE) != TP_BSIZE)
792a74534b1SConrad Meyer quit("master/worker protocol botched.\n");
7938fae3551SRodney W. Grimes }
7948fae3551SRodney W. Grimes }
7958fae3551SRodney W. Grimes if (setjmp(jmpbuf) == 0) {
7968fae3551SRodney W. Grimes ready = 1;
7978fae3551SRodney W. Grimes if (!caught)
7988fae3551SRodney W. Grimes (void) pause();
7998fae3551SRodney W. Grimes }
8008fae3551SRodney W. Grimes ready = 0;
8018fae3551SRodney W. Grimes caught = 0;
8028fae3551SRodney W. Grimes
8038fae3551SRodney W. Grimes /* Try to write the data... */
8048fae3551SRodney W. Grimes eot_count = 0;
8058fae3551SRodney W. Grimes size = 0;
8068fae3551SRodney W. Grimes
807617dbd3cSIan Dowse wrote = 0;
8088fae3551SRodney W. Grimes while (eot_count < 10 && size < writesize) {
8098fae3551SRodney W. Grimes #ifdef RDUMP
8108fae3551SRodney W. Grimes if (host)
8110c577f4fSColin Percival wrote = rmtwrite(wp->tblock[0]+size,
8128fae3551SRodney W. Grimes writesize-size);
8138fae3551SRodney W. Grimes else
8148fae3551SRodney W. Grimes #endif
8150c577f4fSColin Percival wrote = write(tapefd, wp->tblock[0]+size,
8168fae3551SRodney W. Grimes writesize-size);
8178fae3551SRodney W. Grimes #ifdef WRITEDEBUG
818a74534b1SConrad Meyer printf("worker %d wrote %d\n", worker_number, wrote);
8198fae3551SRodney W. Grimes #endif
8208fae3551SRodney W. Grimes if (wrote < 0)
8218fae3551SRodney W. Grimes break;
8228fae3551SRodney W. Grimes if (wrote == 0)
8238fae3551SRodney W. Grimes eot_count++;
8248fae3551SRodney W. Grimes size += wrote;
8258fae3551SRodney W. Grimes }
8268fae3551SRodney W. Grimes
8278fae3551SRodney W. Grimes #ifdef WRITEDEBUG
8288fae3551SRodney W. Grimes if (size != writesize)
829a74534b1SConrad Meyer printf("worker %d only wrote %d out of %d bytes and gave up.\n",
830a74534b1SConrad Meyer worker_number, size, writesize);
8318fae3551SRodney W. Grimes #endif
8328fae3551SRodney W. Grimes
83349fccf2aSJustin T. Gibbs /*
83449fccf2aSJustin T. Gibbs * Handle ENOSPC as an EOT condition.
83549fccf2aSJustin T. Gibbs */
83649fccf2aSJustin T. Gibbs if (wrote < 0 && errno == ENOSPC) {
83749fccf2aSJustin T. Gibbs wrote = 0;
83849fccf2aSJustin T. Gibbs eot_count++;
83949fccf2aSJustin T. Gibbs }
84049fccf2aSJustin T. Gibbs
8418fae3551SRodney W. Grimes if (eot_count > 0)
8428fae3551SRodney W. Grimes size = 0;
8438fae3551SRodney W. Grimes
844ee359f48SJoerg Wunsch if (wrote < 0) {
8458fae3551SRodney W. Grimes (void) kill(master, SIGUSR1);
8468fae3551SRodney W. Grimes for (;;)
8478fae3551SRodney W. Grimes (void) sigpause(0);
8488fae3551SRodney W. Grimes } else {
8498fae3551SRodney W. Grimes /*
8508fae3551SRodney W. Grimes * pass size of write back to master
8518fae3551SRodney W. Grimes * (for EOT handling)
8528fae3551SRodney W. Grimes */
853*e5d0d1c5SKirk McKusick (void)atomic_write(cmd, (const void *)&size,
854*e5d0d1c5SKirk McKusick sizeof size);
8558fae3551SRodney W. Grimes }
8568fae3551SRodney W. Grimes
8578fae3551SRodney W. Grimes /*
858a74534b1SConrad Meyer * If partial write, don't want next worker to go.
8598fae3551SRodney W. Grimes * Also jolts him awake.
8608fae3551SRodney W. Grimes */
861a74534b1SConrad Meyer (void) kill(nextworker, SIGUSR2);
8628fae3551SRodney W. Grimes }
8638fae3551SRodney W. Grimes if (nread != 0)
8648fae3551SRodney W. Grimes quit("error reading command pipe: %s\n", strerror(errno));
8658fae3551SRodney W. Grimes }
8668fae3551SRodney W. Grimes
8678fae3551SRodney W. Grimes /*
8688fae3551SRodney W. Grimes * Since a read from a pipe may not return all we asked for,
8698fae3551SRodney W. Grimes * loop until the count is satisfied (or error).
8708fae3551SRodney W. Grimes */
8718fae3551SRodney W. Grimes static int
atomic_read(int fd,void * buf,int count)872*e5d0d1c5SKirk McKusick atomic_read(int fd, void *buf, int count)
8738fae3551SRodney W. Grimes {
8748fae3551SRodney W. Grimes int got, need = count;
8758fae3551SRodney W. Grimes
876*e5d0d1c5SKirk McKusick while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
877*e5d0d1c5SKirk McKusick buf += got;
878*e5d0d1c5SKirk McKusick return (got < 0 ? got : count - need);
879*e5d0d1c5SKirk McKusick }
880*e5d0d1c5SKirk McKusick
881*e5d0d1c5SKirk McKusick /*
882*e5d0d1c5SKirk McKusick * Since a write to a pipe may not write all we ask if we get a signal,
883*e5d0d1c5SKirk McKusick * loop until the count is satisfied (or error).
884*e5d0d1c5SKirk McKusick */
885*e5d0d1c5SKirk McKusick static int
atomic_write(int fd,const void * buf,int count)886*e5d0d1c5SKirk McKusick atomic_write(int fd, const void *buf, int count)
887*e5d0d1c5SKirk McKusick {
888*e5d0d1c5SKirk McKusick int got, need = count;
889*e5d0d1c5SKirk McKusick
890*e5d0d1c5SKirk McKusick while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
8918fae3551SRodney W. Grimes buf += got;
8928fae3551SRodney W. Grimes return (got < 0 ? got : count - need);
8938fae3551SRodney W. Grimes }
894