tape.c revision 1.41 1 1.41 agc /* $NetBSD: tape.c,v 1.41 2003/08/07 10:04:14 agc Exp $ */
2 1.6 cgd
3 1.1 cgd /*-
4 1.3 mycroft * Copyright (c) 1980, 1991, 1993
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.41 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.12 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.6 cgd #if 0
35 1.14 lukem static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
36 1.6 cgd #else
37 1.41 agc __RCSID("$NetBSD: tape.c,v 1.41 2003/08/07 10:04:14 agc Exp $");
38 1.6 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.3 mycroft #include <sys/param.h>
42 1.3 mycroft #include <sys/socket.h>
43 1.3 mycroft #include <sys/time.h>
44 1.3 mycroft #include <sys/wait.h>
45 1.14 lukem #include <ufs/ufs/dinode.h>
46 1.24 tron #include <sys/ioctl.h>
47 1.24 tron #include <sys/mtio.h>
48 1.3 mycroft
49 1.1 cgd #include <protocols/dumprestore.h>
50 1.3 mycroft
51 1.1 cgd #include <errno.h>
52 1.3 mycroft #include <fcntl.h>
53 1.1 cgd #include <setjmp.h>
54 1.3 mycroft #include <signal.h>
55 1.3 mycroft #include <stdio.h>
56 1.1 cgd #include <stdlib.h>
57 1.1 cgd #include <string.h>
58 1.8 lukem #include <time.h>
59 1.3 mycroft #include <unistd.h>
60 1.3 mycroft
61 1.1 cgd #include "dump.h"
62 1.1 cgd #include "pathnames.h"
63 1.1 cgd
64 1.1 cgd int writesize; /* size of malloc()ed buffer for tape */
65 1.40 fvdl int64_t lastspclrec = -1; /* tape block number of last written header */
66 1.1 cgd int trecno = 0; /* next record to write in current block */
67 1.1 cgd extern long blocksperfile; /* number of blocks per output file */
68 1.1 cgd long blocksthisvol; /* number of blocks on current output file */
69 1.1 cgd extern int ntrec; /* blocking factor on tape */
70 1.1 cgd extern int cartridge;
71 1.1 cgd extern char *host;
72 1.1 cgd char *nexttape;
73 1.3 mycroft
74 1.26 lukem static ssize_t atomic_read(int, char *, int);
75 1.26 lukem static ssize_t atomic_write(int, char *, int);
76 1.26 lukem static void doslave(int, int);
77 1.26 lukem static void enslave(void);
78 1.26 lukem static void flushtape(void);
79 1.26 lukem static void killall(void);
80 1.26 lukem static void proceed(int);
81 1.26 lukem static void rollforward(void);
82 1.26 lukem static void sigpipe(int);
83 1.26 lukem static void tperror(int);
84 1.1 cgd
85 1.1 cgd /*
86 1.1 cgd * Concurrent dump mods (Caltech) - disk block reading and tape writing
87 1.1 cgd * are exported to several slave processes. While one slave writes the
88 1.1 cgd * tape, the others read disk blocks; they pass control of the tape in
89 1.29 lukem * a ring via signals. The parent process traverses the file system and
90 1.1 cgd * sends writeheader()'s and lists of daddr's to the slaves via pipes.
91 1.1 cgd * The following structure defines the instruction packets sent to slaves.
92 1.1 cgd */
93 1.1 cgd struct req {
94 1.1 cgd daddr_t dblk;
95 1.1 cgd int count;
96 1.1 cgd };
97 1.1 cgd int reqsiz;
98 1.1 cgd
99 1.1 cgd #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
100 1.1 cgd struct slave {
101 1.40 fvdl int64_t tapea; /* header number at start of this chunk */
102 1.40 fvdl int64_t firstrec; /* record number of this block */
103 1.1 cgd int count; /* count to next header (used for TS_TAPE */
104 1.1 cgd /* after EOT) */
105 1.1 cgd int inode; /* inode that we are currently dealing with */
106 1.1 cgd int fd; /* FD for this slave */
107 1.1 cgd int pid; /* PID for this slave */
108 1.1 cgd int sent; /* 1 == we've sent this slave requests */
109 1.1 cgd char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
110 1.1 cgd struct req *req; /* buffer for requests */
111 1.1 cgd } slaves[SLAVES+1];
112 1.1 cgd struct slave *slp;
113 1.1 cgd
114 1.1 cgd char (*nextblock)[TP_BSIZE];
115 1.1 cgd
116 1.8 lukem static time_t tstart_volume; /* time of volume start */
117 1.40 fvdl static int64_t tapea_volume; /* value of spcl.c_tapea at volume start */
118 1.8 lukem
119 1.1 cgd int master; /* pid of master, for sending error signals */
120 1.1 cgd int tenths; /* length of tape used per block written */
121 1.1 cgd static int caught; /* have we caught the signal to proceed? */
122 1.1 cgd static int ready; /* have we reached the lock point without having */
123 1.1 cgd /* received the SIGUSR2 signal from the prev slave? */
124 1.1 cgd static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
125 1.1 cgd /* SIGUSR2 arrives from the previous slave */
126 1.1 cgd
127 1.1 cgd int
128 1.26 lukem alloctape(void)
129 1.1 cgd {
130 1.1 cgd int pgoff = getpagesize() - 1;
131 1.1 cgd char *buf;
132 1.1 cgd int i;
133 1.1 cgd
134 1.1 cgd writesize = ntrec * TP_BSIZE;
135 1.1 cgd reqsiz = (ntrec + 1) * sizeof(struct req);
136 1.1 cgd /*
137 1.1 cgd * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
138 1.1 cgd * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
139 1.1 cgd * repositioning after stopping, i.e, streaming mode, where the gap is
140 1.1 cgd * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
141 1.1 cgd */
142 1.36 lukem if (blocksperfile == 0 && !unlimited)
143 1.1 cgd tenths = writesize / density +
144 1.1 cgd (cartridge ? 16 : density == 625 ? 5 : 8);
145 1.1 cgd /*
146 1.1 cgd * Allocate tape buffer contiguous with the array of instruction
147 1.1 cgd * packets, so flushtape() can write them together with one write().
148 1.1 cgd * Align tape buffer on page boundary to speed up tape write().
149 1.1 cgd */
150 1.1 cgd for (i = 0; i <= SLAVES; i++) {
151 1.1 cgd buf = (char *)
152 1.28 lukem xmalloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
153 1.1 cgd slaves[i].tblock = (char (*)[TP_BSIZE])
154 1.1 cgd (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
155 1.1 cgd slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
156 1.1 cgd }
157 1.1 cgd slp = &slaves[0];
158 1.1 cgd slp->count = 1;
159 1.1 cgd slp->tapea = 0;
160 1.1 cgd slp->firstrec = 0;
161 1.1 cgd nextblock = slp->tblock;
162 1.1 cgd return(1);
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd void
166 1.26 lukem writerec(char *dp, int isspcl)
167 1.1 cgd {
168 1.1 cgd
169 1.1 cgd slp->req[trecno].dblk = (daddr_t)0;
170 1.1 cgd slp->req[trecno].count = 1;
171 1.1 cgd *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
172 1.1 cgd if (isspcl)
173 1.40 fvdl lastspclrec = iswap64(spcl.c_tapea);
174 1.1 cgd trecno++;
175 1.40 fvdl spcl.c_tapea = iswap64(iswap64(spcl.c_tapea) +1);
176 1.1 cgd if (trecno >= ntrec)
177 1.1 cgd flushtape();
178 1.1 cgd }
179 1.1 cgd
180 1.1 cgd void
181 1.26 lukem dumpblock(daddr_t blkno, int size)
182 1.1 cgd {
183 1.40 fvdl int avail, tpblks;
184 1.40 fvdl daddr_t dblkno;
185 1.1 cgd
186 1.21 perseant dblkno = fsatoda(ufsib, blkno);
187 1.1 cgd tpblks = size >> tp_bshift;
188 1.1 cgd while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
189 1.1 cgd slp->req[trecno].dblk = dblkno;
190 1.1 cgd slp->req[trecno].count = avail;
191 1.1 cgd trecno += avail;
192 1.40 fvdl spcl.c_tapea = iswap64(iswap64(spcl.c_tapea) + avail);
193 1.1 cgd if (trecno >= ntrec)
194 1.1 cgd flushtape();
195 1.1 cgd dblkno += avail << (tp_bshift - dev_bshift);
196 1.1 cgd tpblks -= avail;
197 1.1 cgd }
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd int nogripe = 0;
201 1.1 cgd
202 1.12 lukem static void
203 1.26 lukem tperror(int signo)
204 1.1 cgd {
205 1.1 cgd
206 1.1 cgd if (pipeout) {
207 1.1 cgd msg("write error on %s\n", tape);
208 1.1 cgd quit("Cannot recover\n");
209 1.1 cgd /* NOTREACHED */
210 1.1 cgd }
211 1.22 briggs msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
212 1.1 cgd broadcast("DUMP WRITE ERROR!\n");
213 1.1 cgd if (!query("Do you want to restart?"))
214 1.1 cgd dumpabort(0);
215 1.1 cgd msg("Closing this volume. Prepare to restart with new media;\n");
216 1.1 cgd msg("this dump volume will be rewritten.\n");
217 1.1 cgd killall();
218 1.1 cgd nogripe = 1;
219 1.1 cgd close_rewind();
220 1.1 cgd Exit(X_REWRITE);
221 1.1 cgd }
222 1.1 cgd
223 1.12 lukem static void
224 1.26 lukem sigpipe(int signo)
225 1.1 cgd {
226 1.1 cgd
227 1.1 cgd quit("Broken pipe\n");
228 1.1 cgd }
229 1.1 cgd
230 1.8 lukem /*
231 1.8 lukem * do_stats --
232 1.8 lukem * Update xferrate stats
233 1.8 lukem */
234 1.8 lukem time_t
235 1.26 lukem do_stats(void)
236 1.8 lukem {
237 1.8 lukem time_t tnow, ttaken;
238 1.40 fvdl int64_t blocks;
239 1.8 lukem
240 1.8 lukem (void)time(&tnow);
241 1.8 lukem ttaken = tnow - tstart_volume;
242 1.40 fvdl blocks = iswap64(spcl.c_tapea) - tapea_volume;
243 1.8 lukem msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
244 1.8 lukem if (ttaken > 0) {
245 1.23 he msg("Volume %d took %d:%02d:%02d\n", tapeno,
246 1.23 he (int) (ttaken / 3600), (int) ((ttaken % 3600) / 60),
247 1.33 lukem (int) (ttaken % 60));
248 1.23 he msg("Volume %d transfer rate: %d KB/s\n", tapeno,
249 1.23 he (int) (blocks / ttaken));
250 1.8 lukem xferrate += blocks / ttaken;
251 1.8 lukem }
252 1.8 lukem return(tnow);
253 1.8 lukem }
254 1.8 lukem
255 1.11 lukem /*
256 1.11 lukem * statussig --
257 1.11 lukem * information message upon receipt of SIGINFO
258 1.11 lukem * (derived from optr.c::timeest())
259 1.11 lukem */
260 1.11 lukem void
261 1.26 lukem statussig(int notused)
262 1.11 lukem {
263 1.11 lukem time_t tnow, deltat;
264 1.11 lukem char msgbuf[128];
265 1.38 yamt int errno_save;
266 1.11 lukem
267 1.11 lukem if (blockswritten < 500)
268 1.33 lukem return;
269 1.38 yamt errno_save = errno;
270 1.11 lukem (void) time((time_t *) &tnow);
271 1.35 lukem if (tnow <= tstart_volume)
272 1.35 lukem return;
273 1.35 lukem deltat = tstart_writing - tnow +
274 1.35 lukem (1.0 * (tnow - tstart_writing)) / blockswritten * tapesize;
275 1.11 lukem (void)snprintf(msgbuf, sizeof(msgbuf),
276 1.11 lukem "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
277 1.11 lukem (blockswritten * 100.0) / tapesize,
278 1.40 fvdl (long)((iswap64(spcl.c_tapea) - tapea_volume) /
279 1.35 lukem (tnow - tstart_volume)),
280 1.11 lukem (int)(deltat / 3600), (int)((deltat % 3600) / 60));
281 1.11 lukem write(STDERR_FILENO, msgbuf, strlen(msgbuf));
282 1.38 yamt errno = errno_save;
283 1.11 lukem }
284 1.11 lukem
285 1.3 mycroft static void
286 1.26 lukem flushtape(void)
287 1.1 cgd {
288 1.1 cgd int i, blks, got;
289 1.40 fvdl int64_t lastfirstrec;
290 1.1 cgd
291 1.1 cgd int siz = (char *)nextblock - (char *)slp->req;
292 1.1 cgd
293 1.1 cgd slp->req[trecno].count = 0; /* Sentinel */
294 1.1 cgd
295 1.12 lukem if (atomic_write(slp->fd, (char *)slp->req, siz) != siz)
296 1.1 cgd quit("error writing command pipe: %s\n", strerror(errno));
297 1.1 cgd slp->sent = 1; /* we sent a request, read the response later */
298 1.1 cgd
299 1.1 cgd lastfirstrec = slp->firstrec;
300 1.1 cgd
301 1.1 cgd if (++slp >= &slaves[SLAVES])
302 1.1 cgd slp = &slaves[0];
303 1.1 cgd
304 1.1 cgd /* Read results back from next slave */
305 1.1 cgd if (slp->sent) {
306 1.12 lukem if (atomic_read(slp->fd, (char *)&got, sizeof got)
307 1.1 cgd != sizeof got) {
308 1.1 cgd perror(" DUMP: error reading command pipe in master");
309 1.1 cgd dumpabort(0);
310 1.1 cgd }
311 1.1 cgd slp->sent = 0;
312 1.1 cgd
313 1.1 cgd /* Check for end of tape */
314 1.1 cgd if (got < writesize) {
315 1.1 cgd msg("End of tape detected\n");
316 1.1 cgd
317 1.1 cgd /*
318 1.1 cgd * Drain the results, don't care what the values were.
319 1.1 cgd * If we read them here then trewind won't...
320 1.1 cgd */
321 1.1 cgd for (i = 0; i < SLAVES; i++) {
322 1.1 cgd if (slaves[i].sent) {
323 1.12 lukem if (atomic_read(slaves[i].fd,
324 1.1 cgd (char *)&got, sizeof got)
325 1.1 cgd != sizeof got) {
326 1.1 cgd perror(" DUMP: error reading command pipe in master");
327 1.1 cgd dumpabort(0);
328 1.1 cgd }
329 1.1 cgd slaves[i].sent = 0;
330 1.1 cgd }
331 1.1 cgd }
332 1.1 cgd
333 1.1 cgd close_rewind();
334 1.1 cgd rollforward();
335 1.1 cgd return;
336 1.1 cgd }
337 1.1 cgd }
338 1.1 cgd
339 1.1 cgd blks = 0;
340 1.16 bouyer if (iswap32(spcl.c_type) != TS_END) {
341 1.16 bouyer for (i = 0; i < iswap32(spcl.c_count); i++)
342 1.1 cgd if (spcl.c_addr[i] != 0)
343 1.1 cgd blks++;
344 1.1 cgd }
345 1.16 bouyer slp->count = lastspclrec + blks + 1 - iswap32(spcl.c_tapea);
346 1.40 fvdl slp->tapea = iswap64(spcl.c_tapea);
347 1.1 cgd slp->firstrec = lastfirstrec + ntrec;
348 1.1 cgd slp->inode = curino;
349 1.1 cgd nextblock = slp->tblock;
350 1.1 cgd trecno = 0;
351 1.1 cgd asize += tenths;
352 1.1 cgd blockswritten += ntrec;
353 1.1 cgd blocksthisvol += ntrec;
354 1.36 lukem if (!pipeout && !unlimited && (blocksperfile ?
355 1.1 cgd (blocksthisvol >= blocksperfile) : (asize > tsize))) {
356 1.1 cgd close_rewind();
357 1.1 cgd startnewtape(0);
358 1.1 cgd }
359 1.1 cgd timeest();
360 1.1 cgd }
361 1.1 cgd
362 1.1 cgd void
363 1.25 tron trewind(int eject)
364 1.1 cgd {
365 1.1 cgd int f;
366 1.1 cgd int got;
367 1.1 cgd
368 1.1 cgd for (f = 0; f < SLAVES; f++) {
369 1.1 cgd /*
370 1.33 lukem * Drain the results, but unlike EOT we DO (or should) care
371 1.33 lukem * what the return values were, since if we detect EOT after
372 1.33 lukem * we think we've written the last blocks to the tape anyway,
373 1.1 cgd * we have to replay those blocks with rollforward.
374 1.1 cgd *
375 1.33 lukem * fixme: punt for now.
376 1.1 cgd */
377 1.1 cgd if (slaves[f].sent) {
378 1.12 lukem if (atomic_read(slaves[f].fd, (char *)&got, sizeof got)
379 1.1 cgd != sizeof got) {
380 1.1 cgd perror(" DUMP: error reading command pipe in master");
381 1.1 cgd dumpabort(0);
382 1.1 cgd }
383 1.1 cgd slaves[f].sent = 0;
384 1.1 cgd if (got != writesize) {
385 1.1 cgd msg("EOT detected in last 2 tape records!\n");
386 1.1 cgd msg("Use a longer tape, decrease the size estimate\n");
387 1.1 cgd quit("or use no size estimate at all.\n");
388 1.1 cgd }
389 1.1 cgd }
390 1.1 cgd (void) close(slaves[f].fd);
391 1.1 cgd }
392 1.1 cgd while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
393 1.1 cgd /* void */;
394 1.1 cgd
395 1.1 cgd if (pipeout)
396 1.1 cgd return;
397 1.1 cgd
398 1.1 cgd msg("Closing %s\n", tape);
399 1.1 cgd
400 1.1 cgd #ifdef RDUMP
401 1.1 cgd if (host) {
402 1.1 cgd rmtclose();
403 1.31 bouyer while (rmtopen(tape, 0, 0) < 0)
404 1.1 cgd sleep(10);
405 1.31 bouyer if (eflag && eject) {
406 1.31 bouyer msg("Ejecting %s\n", tape);
407 1.31 bouyer (void) rmtioctl(MTOFFL, 0);
408 1.31 bouyer }
409 1.1 cgd rmtclose();
410 1.1 cgd return;
411 1.1 cgd }
412 1.1 cgd #endif
413 1.1 cgd (void) close(tapefd);
414 1.1 cgd while ((f = open(tape, 0)) < 0)
415 1.1 cgd sleep (10);
416 1.25 tron if (eflag && eject) {
417 1.24 tron struct mtop offl;
418 1.24 tron
419 1.24 tron msg("Ejecting %s\n", tape);
420 1.24 tron offl.mt_op = MTOFFL;
421 1.24 tron offl.mt_count = 0;
422 1.24 tron (void) ioctl(f, MTIOCTOP, &offl);
423 1.24 tron }
424 1.1 cgd (void) close(f);
425 1.1 cgd }
426 1.1 cgd
427 1.1 cgd void
428 1.26 lukem close_rewind(void)
429 1.1 cgd {
430 1.31 bouyer int i, f;
431 1.31 bouyer
432 1.25 tron trewind(1);
433 1.8 lukem (void)do_stats();
434 1.1 cgd if (nexttape)
435 1.1 cgd return;
436 1.1 cgd if (!nogripe) {
437 1.1 cgd msg("Change Volumes: Mount volume #%d\n", tapeno+1);
438 1.35 lukem broadcast("CHANGE DUMP VOLUMES!\a\a\n");
439 1.1 cgd }
440 1.31 bouyer if (lflag) {
441 1.37 bouyer for (i = 0; i < lflag / 10; i++) { /* wait lflag seconds */
442 1.31 bouyer if (host) {
443 1.31 bouyer if (rmtopen(tape, 0, 0) >= 0) {
444 1.31 bouyer rmtclose();
445 1.31 bouyer return;
446 1.31 bouyer }
447 1.31 bouyer } else {
448 1.31 bouyer if ((f = open(tape, 0)) >= 0) {
449 1.31 bouyer close(f);
450 1.31 bouyer return;
451 1.31 bouyer }
452 1.31 bouyer }
453 1.31 bouyer sleep (10);
454 1.31 bouyer }
455 1.31 bouyer }
456 1.33 lukem
457 1.1 cgd while (!query("Is the new volume mounted and ready to go?"))
458 1.1 cgd if (query("Do you want to abort?")) {
459 1.1 cgd dumpabort(0);
460 1.1 cgd /*NOTREACHED*/
461 1.1 cgd }
462 1.1 cgd }
463 1.1 cgd
464 1.1 cgd void
465 1.26 lukem rollforward(void)
466 1.1 cgd {
467 1.9 lukem struct req *p, *q, *prev;
468 1.9 lukem struct slave *tslp;
469 1.1 cgd int i, size, savedtapea, got;
470 1.1 cgd union u_spcl *ntb, *otb;
471 1.1 cgd tslp = &slaves[SLAVES];
472 1.1 cgd ntb = (union u_spcl *)tslp->tblock[1];
473 1.1 cgd
474 1.1 cgd /*
475 1.33 lukem * Each of the N slaves should have requests that need to
476 1.33 lukem * be replayed on the next tape. Use the extra slave buffers
477 1.33 lukem * (slaves[SLAVES]) to construct request lists to be sent to
478 1.1 cgd * each slave in turn.
479 1.1 cgd */
480 1.1 cgd for (i = 0; i < SLAVES; i++) {
481 1.1 cgd q = &tslp->req[1];
482 1.1 cgd otb = (union u_spcl *)slp->tblock;
483 1.1 cgd
484 1.1 cgd /*
485 1.33 lukem * For each request in the current slave, copy it to tslp.
486 1.1 cgd */
487 1.1 cgd
488 1.3 mycroft prev = NULL;
489 1.1 cgd for (p = slp->req; p->count > 0; p += p->count) {
490 1.1 cgd *q = *p;
491 1.1 cgd if (p->dblk == 0)
492 1.1 cgd *ntb++ = *otb++; /* copy the datablock also */
493 1.1 cgd prev = q;
494 1.1 cgd q += q->count;
495 1.1 cgd }
496 1.3 mycroft if (prev == NULL)
497 1.3 mycroft quit("rollforward: protocol botch");
498 1.1 cgd if (prev->dblk != 0)
499 1.1 cgd prev->count -= 1;
500 1.1 cgd else
501 1.1 cgd ntb--;
502 1.1 cgd q -= 1;
503 1.1 cgd q->count = 0;
504 1.1 cgd q = &tslp->req[0];
505 1.1 cgd if (i == 0) {
506 1.1 cgd q->dblk = 0;
507 1.1 cgd q->count = 1;
508 1.1 cgd trecno = 0;
509 1.1 cgd nextblock = tslp->tblock;
510 1.16 bouyer savedtapea = iswap32(spcl.c_tapea);
511 1.16 bouyer spcl.c_tapea = iswap32(slp->tapea);
512 1.1 cgd startnewtape(0);
513 1.16 bouyer spcl.c_tapea = iswap32(savedtapea);
514 1.1 cgd lastspclrec = savedtapea - 1;
515 1.1 cgd }
516 1.1 cgd size = (char *)ntb - (char *)q;
517 1.12 lukem if (atomic_write(slp->fd, (char *)q, size) != size) {
518 1.1 cgd perror(" DUMP: error writing command pipe");
519 1.1 cgd dumpabort(0);
520 1.1 cgd }
521 1.1 cgd slp->sent = 1;
522 1.1 cgd if (++slp >= &slaves[SLAVES])
523 1.1 cgd slp = &slaves[0];
524 1.1 cgd
525 1.1 cgd q->count = 1;
526 1.1 cgd
527 1.1 cgd if (prev->dblk != 0) {
528 1.1 cgd /*
529 1.33 lukem * If the last one was a disk block, make the
530 1.33 lukem * first of this one be the last bit of that disk
531 1.1 cgd * block...
532 1.1 cgd */
533 1.1 cgd q->dblk = prev->dblk +
534 1.1 cgd prev->count * (TP_BSIZE / DEV_BSIZE);
535 1.1 cgd ntb = (union u_spcl *)tslp->tblock;
536 1.1 cgd } else {
537 1.1 cgd /*
538 1.33 lukem * It wasn't a disk block. Copy the data to its
539 1.1 cgd * new location in the buffer.
540 1.1 cgd */
541 1.1 cgd q->dblk = 0;
542 1.1 cgd *((union u_spcl *)tslp->tblock) = *ntb;
543 1.1 cgd ntb = (union u_spcl *)tslp->tblock[1];
544 1.1 cgd }
545 1.1 cgd }
546 1.1 cgd slp->req[0] = *q;
547 1.1 cgd nextblock = slp->tblock;
548 1.1 cgd if (q->dblk == 0)
549 1.1 cgd nextblock++;
550 1.1 cgd trecno = 1;
551 1.1 cgd
552 1.1 cgd /*
553 1.1 cgd * Clear the first slaves' response. One hopes that it
554 1.1 cgd * worked ok, otherwise the tape is much too short!
555 1.1 cgd */
556 1.1 cgd if (slp->sent) {
557 1.12 lukem if (atomic_read(slp->fd, (char *)&got, sizeof got)
558 1.1 cgd != sizeof got) {
559 1.1 cgd perror(" DUMP: error reading command pipe in master");
560 1.1 cgd dumpabort(0);
561 1.1 cgd }
562 1.1 cgd slp->sent = 0;
563 1.1 cgd
564 1.1 cgd if (got != writesize) {
565 1.1 cgd quit("EOT detected at start of the tape!\n");
566 1.1 cgd }
567 1.1 cgd }
568 1.1 cgd }
569 1.1 cgd
570 1.1 cgd /*
571 1.1 cgd * We implement taking and restoring checkpoints on the tape level.
572 1.1 cgd * When each tape is opened, a new process is created by forking; this
573 1.1 cgd * saves all of the necessary context in the parent. The child
574 1.1 cgd * continues the dump; the parent waits around, saving the context.
575 1.1 cgd * If the child returns X_REWRITE, then it had problems writing that tape;
576 1.1 cgd * this causes the parent to fork again, duplicating the context, and
577 1.1 cgd * everything continues as if nothing had happened.
578 1.1 cgd */
579 1.1 cgd void
580 1.26 lukem startnewtape(int top)
581 1.1 cgd {
582 1.1 cgd int parentpid;
583 1.1 cgd int childpid;
584 1.1 cgd int status;
585 1.30 lukem int waitforpid;
586 1.1 cgd char *p;
587 1.1 cgd sig_t interrupt_save;
588 1.1 cgd
589 1.1 cgd interrupt_save = signal(SIGINT, SIG_IGN);
590 1.1 cgd parentpid = getpid();
591 1.16 bouyer tapea_volume = iswap32(spcl.c_tapea);
592 1.8 lukem (void)time(&tstart_volume);
593 1.1 cgd
594 1.3 mycroft restore_check_point:
595 1.1 cgd (void)signal(SIGINT, interrupt_save);
596 1.1 cgd /*
597 1.1 cgd * All signals are inherited...
598 1.1 cgd */
599 1.1 cgd childpid = fork();
600 1.1 cgd if (childpid < 0) {
601 1.1 cgd msg("Context save fork fails in parent %d\n", parentpid);
602 1.1 cgd Exit(X_ABORT);
603 1.1 cgd }
604 1.1 cgd if (childpid != 0) {
605 1.1 cgd /*
606 1.1 cgd * PARENT:
607 1.1 cgd * save the context by waiting
608 1.1 cgd * until the child doing all of the work returns.
609 1.1 cgd * don't catch the interrupt
610 1.1 cgd */
611 1.1 cgd signal(SIGINT, SIG_IGN);
612 1.35 lukem signal(SIGINFO, SIG_IGN); /* only want child's stats */
613 1.1 cgd #ifdef TDEBUG
614 1.1 cgd msg("Tape: %d; parent process: %d child process %d\n",
615 1.1 cgd tapeno+1, parentpid, childpid);
616 1.3 mycroft #endif /* TDEBUG */
617 1.30 lukem while ((waitforpid = wait(&status)) != childpid)
618 1.1 cgd msg("Parent %d waiting for child %d has another child %d return\n",
619 1.30 lukem parentpid, childpid, waitforpid);
620 1.1 cgd if (status & 0xFF) {
621 1.1 cgd msg("Child %d returns LOB status %o\n",
622 1.1 cgd childpid, status&0xFF);
623 1.1 cgd }
624 1.1 cgd status = (status >> 8) & 0xFF;
625 1.1 cgd #ifdef TDEBUG
626 1.1 cgd switch(status) {
627 1.1 cgd case X_FINOK:
628 1.1 cgd msg("Child %d finishes X_FINOK\n", childpid);
629 1.1 cgd break;
630 1.33 lukem case X_ABORT:
631 1.1 cgd msg("Child %d finishes X_ABORT\n", childpid);
632 1.1 cgd break;
633 1.1 cgd case X_REWRITE:
634 1.1 cgd msg("Child %d finishes X_REWRITE\n", childpid);
635 1.1 cgd break;
636 1.1 cgd default:
637 1.1 cgd msg("Child %d finishes unknown %d\n",
638 1.1 cgd childpid, status);
639 1.1 cgd break;
640 1.1 cgd }
641 1.3 mycroft #endif /* TDEBUG */
642 1.1 cgd switch(status) {
643 1.1 cgd case X_FINOK:
644 1.1 cgd Exit(X_FINOK);
645 1.1 cgd case X_ABORT:
646 1.1 cgd Exit(X_ABORT);
647 1.1 cgd case X_REWRITE:
648 1.1 cgd goto restore_check_point;
649 1.1 cgd default:
650 1.1 cgd msg("Bad return code from dump: %d\n", status);
651 1.1 cgd Exit(X_ABORT);
652 1.1 cgd }
653 1.1 cgd /*NOTREACHED*/
654 1.1 cgd } else { /* we are the child; just continue */
655 1.35 lukem signal(SIGINFO, statussig); /* now want child's stats */
656 1.1 cgd #ifdef TDEBUG
657 1.1 cgd sleep(4); /* allow time for parent's message to get out */
658 1.1 cgd msg("Child on Tape %d has parent %d, my pid = %d\n",
659 1.1 cgd tapeno+1, parentpid, getpid());
660 1.3 mycroft #endif /* TDEBUG */
661 1.1 cgd /*
662 1.10 lukem * If we have a name like "/dev/rst0,/dev/rst1",
663 1.1 cgd * use the name before the comma first, and save
664 1.1 cgd * the remaining names for subsequent volumes.
665 1.1 cgd */
666 1.32 lukem tapeno++; /* current tape sequence */
667 1.4 mycroft if (nexttape || strchr(tape, ',')) {
668 1.1 cgd if (nexttape && *nexttape)
669 1.1 cgd tape = nexttape;
670 1.4 mycroft if ((p = strchr(tape, ',')) != NULL) {
671 1.1 cgd *p = '\0';
672 1.1 cgd nexttape = p + 1;
673 1.1 cgd } else
674 1.1 cgd nexttape = NULL;
675 1.1 cgd msg("Dumping volume %d on %s\n", tapeno, tape);
676 1.1 cgd }
677 1.1 cgd #ifdef RDUMP
678 1.31 bouyer while ((tapefd = (host ? rmtopen(tape, 2, 1) :
679 1.1 cgd pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
680 1.1 cgd #else
681 1.33 lukem while ((tapefd = (pipeout ? 1 :
682 1.1 cgd open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
683 1.1 cgd #endif
684 1.1 cgd {
685 1.1 cgd msg("Cannot open output \"%s\".\n", tape);
686 1.1 cgd if (!query("Do you want to retry the open?"))
687 1.1 cgd dumpabort(0);
688 1.1 cgd }
689 1.1 cgd
690 1.1 cgd enslave(); /* Share open tape file descriptor with slaves */
691 1.1 cgd
692 1.1 cgd asize = 0;
693 1.1 cgd blocksthisvol = 0;
694 1.1 cgd if (top)
695 1.1 cgd newtape++; /* new tape signal */
696 1.16 bouyer spcl.c_count = iswap32(slp->count);
697 1.1 cgd /*
698 1.1 cgd * measure firstrec in TP_BSIZE units since restore doesn't
699 1.1 cgd * know the correct ntrec value...
700 1.1 cgd */
701 1.16 bouyer spcl.c_firstrec = iswap32(slp->firstrec);
702 1.16 bouyer spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
703 1.16 bouyer spcl.c_type = iswap32(TS_TAPE);
704 1.40 fvdl if (!is_ufs2)
705 1.40 fvdl spcl.c_flags = iswap32(iswap32(spcl.c_flags)
706 1.40 fvdl | DR_NEWHEADER);
707 1.1 cgd writeheader((ino_t)slp->inode);
708 1.40 fvdl if (!is_ufs2)
709 1.40 fvdl spcl.c_flags = iswap32(iswap32(spcl.c_flags) &
710 1.40 fvdl ~ DR_NEWHEADER);
711 1.8 lukem msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
712 1.1 cgd if (tapeno > 1)
713 1.1 cgd msg("Volume %d begins with blocks from inode %d\n",
714 1.1 cgd tapeno, slp->inode);
715 1.1 cgd }
716 1.1 cgd }
717 1.1 cgd
718 1.1 cgd void
719 1.26 lukem dumpabort(int signo)
720 1.1 cgd {
721 1.1 cgd
722 1.1 cgd if (master != 0 && master != getpid())
723 1.1 cgd /* Signals master to call dumpabort */
724 1.1 cgd (void) kill(master, SIGTERM);
725 1.1 cgd else {
726 1.1 cgd killall();
727 1.1 cgd msg("The ENTIRE dump is aborted.\n");
728 1.1 cgd }
729 1.2 cgd #ifdef RDUMP
730 1.2 cgd rmtclose();
731 1.2 cgd #endif
732 1.1 cgd Exit(X_ABORT);
733 1.1 cgd }
734 1.1 cgd
735 1.13 lukem void
736 1.26 lukem Exit(int status)
737 1.1 cgd {
738 1.1 cgd
739 1.1 cgd #ifdef TDEBUG
740 1.1 cgd msg("pid = %d exits with status %d\n", getpid(), status);
741 1.3 mycroft #endif /* TDEBUG */
742 1.3 mycroft exit(status);
743 1.1 cgd }
744 1.1 cgd
745 1.1 cgd /*
746 1.1 cgd * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
747 1.1 cgd */
748 1.12 lukem static void
749 1.26 lukem proceed(int signo)
750 1.1 cgd {
751 1.1 cgd
752 1.1 cgd if (ready)
753 1.1 cgd longjmp(jmpbuf, 1);
754 1.1 cgd caught++;
755 1.1 cgd }
756 1.1 cgd
757 1.1 cgd void
758 1.26 lukem enslave(void)
759 1.1 cgd {
760 1.1 cgd int cmd[2];
761 1.9 lukem int i, j;
762 1.1 cgd
763 1.1 cgd master = getpid();
764 1.1 cgd
765 1.1 cgd signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
766 1.1 cgd signal(SIGPIPE, sigpipe);
767 1.1 cgd signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
768 1.1 cgd signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
769 1.1 cgd
770 1.1 cgd for (i = 0; i < SLAVES; i++) {
771 1.1 cgd if (i == slp - &slaves[0]) {
772 1.1 cgd caught = 1;
773 1.1 cgd } else {
774 1.1 cgd caught = 0;
775 1.1 cgd }
776 1.1 cgd
777 1.17 lukem if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
778 1.1 cgd (slaves[i].pid = fork()) < 0)
779 1.1 cgd quit("too many slaves, %d (recompile smaller): %s\n",
780 1.1 cgd i, strerror(errno));
781 1.1 cgd
782 1.1 cgd slaves[i].fd = cmd[1];
783 1.1 cgd slaves[i].sent = 0;
784 1.1 cgd if (slaves[i].pid == 0) { /* Slave starts up here */
785 1.1 cgd for (j = 0; j <= i; j++)
786 1.32 lukem (void) close(slaves[j].fd);
787 1.1 cgd signal(SIGINT, SIG_IGN); /* Master handles this */
788 1.11 lukem signal(SIGINFO, SIG_IGN);
789 1.1 cgd doslave(cmd[0], i);
790 1.1 cgd Exit(X_FINOK);
791 1.1 cgd }
792 1.1 cgd }
793 1.33 lukem
794 1.1 cgd for (i = 0; i < SLAVES; i++)
795 1.33 lukem (void) atomic_write(slaves[i].fd,
796 1.33 lukem (char *) &slaves[(i + 1) % SLAVES].pid,
797 1.32 lukem sizeof slaves[0].pid);
798 1.33 lukem
799 1.33 lukem master = 0;
800 1.1 cgd }
801 1.1 cgd
802 1.1 cgd void
803 1.26 lukem killall(void)
804 1.1 cgd {
805 1.9 lukem int i;
806 1.1 cgd
807 1.1 cgd for (i = 0; i < SLAVES; i++)
808 1.35 lukem if (slaves[i].pid > 0) {
809 1.1 cgd (void) kill(slaves[i].pid, SIGKILL);
810 1.35 lukem slaves[i].sent = 0;
811 1.35 lukem }
812 1.1 cgd }
813 1.1 cgd
814 1.1 cgd /*
815 1.1 cgd * Synchronization - each process has a lockfile, and shares file
816 1.1 cgd * descriptors to the following process's lockfile. When our write
817 1.1 cgd * completes, we release our lock on the following process's lock-
818 1.1 cgd * file, allowing the following process to lock it and proceed. We
819 1.1 cgd * get the lock back for the next cycle by swapping descriptors.
820 1.1 cgd */
821 1.3 mycroft static void
822 1.26 lukem doslave(int cmd, int slave_number)
823 1.1 cgd {
824 1.35 lukem int nread, nextslave, size, wrote, eot_count, werror;
825 1.39 kleink sigset_t nsigset;
826 1.1 cgd
827 1.1 cgd /*
828 1.1 cgd * Need our own seek pointer.
829 1.1 cgd */
830 1.1 cgd (void) close(diskfd);
831 1.1 cgd if ((diskfd = open(disk, O_RDONLY)) < 0)
832 1.1 cgd quit("slave couldn't reopen disk: %s\n", strerror(errno));
833 1.1 cgd
834 1.1 cgd /*
835 1.1 cgd * Need the pid of the next slave in the loop...
836 1.1 cgd */
837 1.12 lukem if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
838 1.1 cgd != sizeof nextslave) {
839 1.1 cgd quit("master/slave protocol botched - didn't get pid of next slave.\n");
840 1.1 cgd }
841 1.1 cgd
842 1.1 cgd /*
843 1.1 cgd * Get list of blocks to dump, read the blocks into tape buffer
844 1.1 cgd */
845 1.12 lukem while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
846 1.9 lukem struct req *p = slp->req;
847 1.1 cgd
848 1.1 cgd for (trecno = 0; trecno < ntrec;
849 1.1 cgd trecno += p->count, p += p->count) {
850 1.1 cgd if (p->dblk) {
851 1.1 cgd bread(p->dblk, slp->tblock[trecno],
852 1.1 cgd p->count * TP_BSIZE);
853 1.1 cgd } else {
854 1.12 lukem if (p->count != 1 || atomic_read(cmd,
855 1.33 lukem (char *)slp->tblock[trecno],
856 1.1 cgd TP_BSIZE) != TP_BSIZE)
857 1.1 cgd quit("master/slave protocol botched.\n");
858 1.1 cgd }
859 1.1 cgd }
860 1.1 cgd if (setjmp(jmpbuf) == 0) {
861 1.1 cgd ready = 1;
862 1.1 cgd if (!caught)
863 1.1 cgd (void) pause();
864 1.1 cgd }
865 1.1 cgd ready = 0;
866 1.1 cgd caught = 0;
867 1.1 cgd
868 1.1 cgd /* Try to write the data... */
869 1.1 cgd eot_count = 0;
870 1.1 cgd size = 0;
871 1.35 lukem werror = 0;
872 1.1 cgd
873 1.1 cgd while (eot_count < 10 && size < writesize) {
874 1.1 cgd #ifdef RDUMP
875 1.1 cgd if (host)
876 1.1 cgd wrote = rmtwrite(slp->tblock[0]+size,
877 1.1 cgd writesize-size);
878 1.1 cgd else
879 1.1 cgd #endif
880 1.1 cgd wrote = write(tapefd, slp->tblock[0]+size,
881 1.1 cgd writesize-size);
882 1.35 lukem werror = errno;
883 1.1 cgd #ifdef WRITEDEBUG
884 1.35 lukem fprintf(stderr, "slave %d wrote %d werror %d\n",
885 1.35 lukem slave_number, wrote, werror);
886 1.1 cgd #endif
887 1.33 lukem if (wrote < 0)
888 1.1 cgd break;
889 1.1 cgd if (wrote == 0)
890 1.1 cgd eot_count++;
891 1.1 cgd size += wrote;
892 1.1 cgd }
893 1.1 cgd
894 1.1 cgd #ifdef WRITEDEBUG
895 1.33 lukem if (size != writesize)
896 1.32 lukem fprintf(stderr,
897 1.32 lukem "slave %d only wrote %d out of %d bytes and gave up.\n",
898 1.32 lukem slave_number, size, writesize);
899 1.1 cgd #endif
900 1.1 cgd
901 1.35 lukem /*
902 1.35 lukem * Handle ENOSPC as an EOT condition.
903 1.35 lukem */
904 1.35 lukem if (wrote < 0 && werror == ENOSPC) {
905 1.35 lukem wrote = 0;
906 1.35 lukem eot_count++;
907 1.35 lukem }
908 1.35 lukem
909 1.1 cgd if (eot_count > 0)
910 1.1 cgd size = 0;
911 1.1 cgd
912 1.35 lukem if (wrote < 0) {
913 1.1 cgd (void) kill(master, SIGUSR1);
914 1.39 kleink sigemptyset(&nsigset);
915 1.1 cgd for (;;)
916 1.39 kleink sigsuspend(&nsigset);
917 1.1 cgd } else {
918 1.1 cgd /*
919 1.1 cgd * pass size of write back to master
920 1.1 cgd * (for EOT handling)
921 1.1 cgd */
922 1.12 lukem (void) atomic_write(cmd, (char *)&size, sizeof size);
923 1.33 lukem }
924 1.1 cgd
925 1.1 cgd /*
926 1.1 cgd * If partial write, don't want next slave to go.
927 1.1 cgd * Also jolts him awake.
928 1.1 cgd */
929 1.1 cgd (void) kill(nextslave, SIGUSR2);
930 1.1 cgd }
931 1.18 bouyer printcachestats();
932 1.1 cgd if (nread != 0)
933 1.1 cgd quit("error reading command pipe: %s\n", strerror(errno));
934 1.1 cgd }
935 1.1 cgd
936 1.1 cgd /*
937 1.1 cgd * Since a read from a pipe may not return all we asked for,
938 1.1 cgd * loop until the count is satisfied (or error).
939 1.1 cgd */
940 1.5 cgd static ssize_t
941 1.26 lukem atomic_read(int fd, char *buf, int count)
942 1.12 lukem {
943 1.12 lukem ssize_t got, need = count;
944 1.12 lukem
945 1.12 lukem while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
946 1.12 lukem buf += got;
947 1.12 lukem return (got < 0 ? got : count - need);
948 1.12 lukem }
949 1.12 lukem
950 1.12 lukem /*
951 1.12 lukem * Since a write may not write all we ask if we get a signal,
952 1.12 lukem * loop until the count is satisfied (or error).
953 1.12 lukem */
954 1.12 lukem static ssize_t
955 1.26 lukem atomic_write(int fd, char *buf, int count)
956 1.1 cgd {
957 1.5 cgd ssize_t got, need = count;
958 1.1 cgd
959 1.12 lukem while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
960 1.1 cgd buf += got;
961 1.1 cgd return (got < 0 ? got : count - need);
962 1.1 cgd }
963