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