Fix copyright
[unix-history] / usr / src / sbin / dump / tape.c
CommitLineData
76797561
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7a65e725 7#ifndef lint
76797561
DF
8static char sccsid[] = "@(#)tape.c 5.1 (Berkeley) %G%";
9#endif not lint
7a65e725 10
ae4b153c 11#include "dump.h"
23b4aba9 12#include <signal.h>
ae4b153c 13
1ddebffe
SL
14char (*tblock)[TP_BSIZE]; /* Pointer to malloc()ed buffer for tape */
15int writesize; /* Size of malloc()ed buffer for tape */
f5bba473 16int trecno = 0;
23b4aba9 17extern int ntrec; /* blocking factor on tape */
1ddebffe
SL
18
19/*
23b4aba9
KM
20 * Streaming dump mods (Caltech) - disk block reading and tape writing
21 * are exported to several slave processes. While one slave writes the
22 * tape, the others read disk blocks; they pass control of the tape in
23 * a ring via pipes. The parent process traverses the filesystem and
24 * sends daddr's, inode records, etc, through pipes to each slave.
25 * Speed from Eagle to TU77 on VAX/780 is about 140 Kbytes/second.
26 * #ifdef RDUMP version is CPU-limited to about 40 Kbytes/second.
27 */
28struct req { /* instruction packets sent to slaves */
29 daddr_t dblk;
30 int count;
31} *req;
32int reqsiz;
33
34#define SLAVES 3 /* 2 slaves read disk while 3rd writes tape */
35#define LAG 2 /* Write behind by LAG tape blocks (rdump) */
36int slavefd[SLAVES]; /* Pipes from master to each slave */
37int rotor; /* Current slave number */
38int master; /* Pid of master, for sending error signals */
39int trace = 0; /* Protocol trace; easily patchable with adb */
40#define tmsg if (trace) msg
41
42#ifdef RDUMP
43extern int rmtape;
44#endif
45
46/*
47 * Allocate tape buffer contiguous with the array of instruction packets,
48 * so they can be written with a single write call in flusht().
1ddebffe
SL
49 */
50alloctape()
51{
52
53 writesize = ntrec * TP_BSIZE;
23b4aba9
KM
54 reqsiz = ntrec * sizeof(struct req);
55 req = (struct req *)malloc(reqsiz+writesize); /* array of packets */
56 tblock = (char (*)[TP_BSIZE]) &req[ntrec]; /* Tape buffer */
57 return (req != NULL);
1ddebffe
SL
58}
59
23b4aba9
KM
60/*
61 * Send special record to be put on tape
62 */
ae4b153c 63taprec(dp)
b6407c9d 64 char *dp;
ae4b153c 65{
ae4b153c 66
23b4aba9
KM
67 tmsg("taprec %d\n", trecno);
68 req[trecno].dblk = (daddr_t)0;
69 req[trecno].count = 1;
70 *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp;
ae4b153c 71 spcl.c_tapea++;
23b4aba9 72 if (++trecno >= ntrec)
ae4b153c
BJ
73 flusht();
74}
75
f5bba473
KM
76dmpblk(blkno, size)
77 daddr_t blkno;
78 int size;
ae4b153c 79{
23b4aba9
KM
80 int tpblks, dblkno;
81 register int avail;
f5bba473 82
b6407c9d 83 if (size % TP_BSIZE != 0)
f5bba473 84 msg("bad size to dmpblk: %d\n", size);
b6407c9d 85 dblkno = fsbtodb(sblock, blkno);
23b4aba9
KM
86 tpblks = size / TP_BSIZE;
87 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
88 tmsg("dmpblk %d\n", avail);
89 req[trecno].dblk = dblkno;
90 req[trecno].count = avail;
f5bba473
KM
91 trecno += avail;
92 spcl.c_tapea += avail;
23b4aba9
KM
93 if (trecno >= ntrec)
94 flusht();
b6407c9d
KM
95 dblkno += avail * (TP_BSIZE / DEV_BSIZE);
96 tpblks -= avail;
f5bba473 97 }
ae4b153c
BJ
98}
99
100int nogripe = 0;
101
23b4aba9
KM
102tperror() {
103 if (pipeout) {
104 msg("Tape write error on %s\n", tape);
105 msg("Cannot recover\n");
106 dumpabort();
107 /* NOTREACHED */
108 }
109 msg("Tape write error on tape %d\n", tapeno);
110 broadcast("TAPE ERROR!\n");
111 if (!query("Do you want to restart?"))
112 dumpabort();
113 msg("This tape will rewind. After it is rewound,\n");
114 msg("replace the faulty tape with a new one;\n");
115 msg("this dump volume will be rewritten.\n");
116 nogripe = 1;
117 close_rewind();
118 Exit(X_REWRITE);
119}
120
121senderr()
122{
123
87801efd 124 perror(" DUMP: pipe error in command to slave");
23b4aba9
KM
125 dumpabort();
126}
127
128#ifdef RDUMP
129tflush(cnt)
130 int cnt;
131{
132 int i;
133
134 for (i = 0; i < ntrec; i++)
135 spclrec();
136}
137#endif RDUMP
138
ae4b153c
BJ
139flusht()
140{
23b4aba9 141 int sig, siz = (char *)tblock - (char *)req;
ae4b153c 142
23b4aba9
KM
143 tmsg("flusht %d\n", siz);
144 sig = sigblock(1<<SIGINT-1 | 1<<SIGIOT-1); /* Don't interrupt write */
145 if (write(slavefd[rotor], req, siz) != siz)
146 senderr();
147 sigsetmask(sig);
148 if (++rotor >= SLAVES) rotor = 0;
149 tblock = (char (*)[TP_BSIZE]) &req[ntrec];
ae4b153c 150 trecno = 0;
1ddebffe 151 asize += writesize/density;
23b4aba9 152 asize += 7; /* inter-record gap (why fixed?) */
1ddebffe 153 blockswritten += ntrec;
a47b7e40 154 if (!pipeout && asize > tsize) {
ae4b153c
BJ
155 close_rewind();
156 otape();
157 }
158 timeest();
159}
160
161rewind()
162{
23b4aba9 163 register int f;
a47b7e40
KM
164
165 if (pipeout)
166 return;
23b4aba9
KM
167 for (f = 0; f < SLAVES; f++)
168 close(slavefd[f]);
169 while (wait(NULL) >= 0) ; /* wait for any signals from slaves */
170 msg("Tape rewinding\n");
171#ifdef RDUMP
172 rmtclose();
173 while (rmtopen(tape, 0) < 0)
174 sleep(10);
175 rmtclose();
ae4b153c 176#else
be3f486f
BJ
177 close(to);
178 while ((f = open(tape, 0)) < 0)
179 sleep (10);
180 close(f);
ae4b153c
BJ
181#endif
182}
183
184close_rewind()
185{
23b4aba9
KM
186 rewind();
187 if (!nogripe) {
ae4b153c
BJ
188 msg("Change Tapes: Mount tape #%d\n", tapeno+1);
189 broadcast("CHANGE TAPES!\7\7\n");
190 }
23b4aba9
KM
191 while (!query("Is the new tape mounted and ready to go?"))
192 if (query("Do you want to abort?"))
ae4b153c 193 dumpabort();
ae4b153c
BJ
194}
195
196/*
23b4aba9 197 * We implement taking and restoring checkpoints on the tape level.
ae4b153c
BJ
198 * When each tape is opened, a new process is created by forking; this
199 * saves all of the necessary context in the parent. The child
200 * continues the dump; the parent waits around, saving the context.
201 * If the child returns X_REWRITE, then it had problems writing that tape;
202 * this causes the parent to fork again, duplicating the context, and
203 * everything continues as if nothing had happened.
204 */
205
206otape()
207{
208 int parentpid;
209 int childpid;
210 int status;
211 int waitpid;
ae4b153c
BJ
212 int interrupt();
213
ae4b153c
BJ
214 parentpid = getpid();
215
216 restore_check_point:
217 signal(SIGINT, interrupt);
218 /*
219 * All signals are inherited...
220 */
221 childpid = fork();
23b4aba9 222 if (childpid < 0) {
ae4b153c
BJ
223 msg("Context save fork fails in parent %d\n", parentpid);
224 Exit(X_ABORT);
225 }
23b4aba9 226 if (childpid != 0) {
ae4b153c
BJ
227 /*
228 * PARENT:
229 * save the context by waiting
230 * until the child doing all of the work returns.
23b4aba9 231 * don't catch the interrupt
ae4b153c
BJ
232 */
233 signal(SIGINT, SIG_IGN);
234#ifdef TDEBUG
235 msg("Tape: %d; parent process: %d child process %d\n",
236 tapeno+1, parentpid, childpid);
237#endif TDEBUG
23b4aba9
KM
238 while ((waitpid = wait(&status)) != childpid)
239 msg("Parent %d waiting for child %d has another child %d return\n",
240 parentpid, childpid, waitpid);
241 if (status & 0xFF) {
ae4b153c
BJ
242 msg("Child %d returns LOB status %o\n",
243 childpid, status&0xFF);
244 }
245 status = (status >> 8) & 0xFF;
246#ifdef TDEBUG
23b4aba9 247 switch(status) {
ae4b153c
BJ
248 case X_FINOK:
249 msg("Child %d finishes X_FINOK\n", childpid);
250 break;
251 case X_ABORT:
252 msg("Child %d finishes X_ABORT\n", childpid);
253 break;
254 case X_REWRITE:
255 msg("Child %d finishes X_REWRITE\n", childpid);
256 break;
257 default:
23b4aba9
KM
258 msg("Child %d finishes unknown %d\n",
259 childpid, status);
ae4b153c
BJ
260 break;
261 }
262#endif TDEBUG
23b4aba9 263 switch(status) {
ae4b153c
BJ
264 case X_FINOK:
265 Exit(X_FINOK);
266 case X_ABORT:
267 Exit(X_ABORT);
268 case X_REWRITE:
269 goto restore_check_point;
270 default:
271 msg("Bad return code from dump: %d\n", status);
272 Exit(X_ABORT);
273 }
274 /*NOTREACHED*/
275 } else { /* we are the child; just continue */
276#ifdef TDEBUG
277 sleep(4); /* allow time for parent's message to get out */
278 msg("Child on Tape %d has parent %d, my pid = %d\n",
279 tapeno+1, parentpid, getpid());
280#endif
23b4aba9
KM
281#ifdef RDUMP
282 while ((to = rmtopen(tape, 2)) < 0)
283#else
284 while ((to = pipeout ? 1 : creat(tape, 0666)) < 0)
285#endif
286 if (!query("Cannot open tape. Do you want to retry the open?"))
287 dumpabort();
288
289 enslave(); /* Share open tape file descriptor with slaves */
ae4b153c
BJ
290
291 asize = 0;
292 tapeno++; /* current tape sequence */
293 newtape++; /* new tape signal */
294 spcl.c_volume++;
295 spcl.c_type = TS_TAPE;
296 spclrec();
297 if (tapeno > 1)
298 msg("Tape %d begins with blocks from ino %d\n",
299 tapeno, ino);
300 }
301}
302
ae4b153c
BJ
303dumpabort()
304{
23b4aba9
KM
305 if (master != 0 && master != getpid())
306 kill(master, SIGIOT);
ed7c701e 307 msg("The ENTIRE dump is aborted.\n");
ae4b153c
BJ
308 Exit(X_ABORT);
309}
310
311Exit(status)
312{
313#ifdef TDEBUG
314 msg("pid = %d exits with status %d\n", getpid(), status);
315#endif TDEBUG
ed7c701e 316 exit(status);
ae4b153c 317}
23b4aba9
KM
318
319#define OK 020
320char tok = OK;
321
322enslave()
323{
324 int prev[2], next[2], cmd[2]; /* file descriptors for pipes */
87801efd 325 int i, j, ret, slavepid;
23b4aba9
KM
326
327 master = getpid();
328 signal(SIGPIPE, dumpabort);
329 signal(SIGIOT, tperror); /* SIGIOT asks for restart from checkpoint */
330 pipe(prev);
331 for (i = rotor = 0; i < SLAVES; ++i) {
332 if ((i < SLAVES - 1 && pipe(next) < 0) || pipe(cmd) < 0
333 || (slavepid = fork()) < 0) {
334 perror(" DUMP: too many slaves");
335 dumpabort();
336 }
337 if (i >= SLAVES - 1)
338 next[1] = prev[1]; /* Last slave loops back */
339 slavefd[i] = cmd[1];
340 if (slavepid == 0) { /* Slave starts up here */
341 for (j = 0; j <= i; j++)
342 close(slavefd[j]);
343 if (i < SLAVES - 1) {
344 close(prev[1]);
345 close(next[0]);
346 } else { /* Insert initial token */
87801efd
KM
347 if ((ret = write(next[1], &tok, 1)) != 1)
348 ringerr(ret, "cannot start token");
23b4aba9
KM
349 }
350 doslave(i, cmd[0], prev[0], next[1]);
351 close(next[1]);
352 j = read(prev[0], &tok, 1); /* Eat the final token */
353#ifdef RDUMP /* Read remaining acknowledges */
354 for (; j > 0 && (tok &~ OK) > 0; tok--) {
355 if (rmtwrite2() != writesize && (tok & OK)) {
356 kill(master, SIGIOT);
357 tok &= ~OK;
358 }
359 }
360#endif
361 Exit(X_FINOK);
362 }
363 close(cmd[0]);
364 close(next[1]);
365 close(prev[0]);
366 prev[0] = next[0];
367 }
368 master = 0;
369}
370
371/*
372 * Somebody must have died, should never happen
373 */
87801efd
KM
374ringerr(code, msg, a1, a2)
375 int code;
376 char *msg;
377 int a1, a2;
23b4aba9 378{
87801efd
KM
379 char buf[BUFSIZ];
380
381 fprintf(stderr, " DUMP: ");
382 sprintf(buf, msg, a1, a2);
383 if (code < 0)
384 perror(msg);
385 else if (code == 0)
386 fprintf(stderr, "%s: unexpected EOF\n", buf);
387 else
388 fprintf(stderr, "%s: code %d\n", buf, code);
23b4aba9
KM
389 kill(master, SIGPIPE);
390 Exit(X_ABORT);
391}
392
87801efd
KM
393int childnum;
394sigpipe()
395{
396
397 ringerr(childnum, "SIGPIPE raised");
398}
399
23b4aba9
KM
400doslave(num, cmd, prev, next)
401 int num, cmd, prev, next;
402{
87801efd
KM
403 int ret;
404
23b4aba9
KM
405 tmsg("slave %d\n", num);
406 signal(SIGINT, SIG_IGN); /* Master handles it */
407 signal(SIGTERM, SIG_IGN);
87801efd
KM
408 signal(SIGPIPE, sigpipe);
409 childnum = num;
23b4aba9
KM
410 close(fi);
411 if ((fi = open(disk, 0)) < 0) { /* Need our own seek pointer */
412 perror(" DUMP: can't reopen disk");
413 kill(master, SIGPIPE);
414 Exit(X_ABORT);
415 }
87801efd 416 while ((ret = readpipe(cmd, req, reqsiz)) == reqsiz) {
23b4aba9
KM
417 register struct req *p = req;
418 for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) {
419 if (p->dblk) {
420 tmsg("%d READS %d\n", num, p->count);
421 bread(p->dblk, tblock[trecno],
422 p->count * TP_BSIZE);
423 } else {
424 tmsg("%d PIPEIN %d\n", num, p->count);
425 if (p->count != 1)
87801efd
KM
426 ringerr(11, "%d PIPEIN %d", num,
427 p->count);
ca485693 428 if (readpipe(cmd, tblock[trecno], TP_BSIZE) != TP_BSIZE)
23b4aba9
KM
429 senderr();
430 }
431 }
87801efd
KM
432 if ((ret = read(prev, &tok, 1)) != 1)
433 ringerr(ret, "read token"); /* Wait your turn */
23b4aba9
KM
434 tmsg("%d WRITE\n", num);
435#ifdef RDUMP
436 if (tok & OK) {
437 rmtwrite0(writesize);
438 rmtwrite1(tblock[0], writesize);
439 tok++; /* Number of writes in progress */
440 }
441 if (tok > (LAG|OK) && (--tok, rmtwrite2() != writesize)) {
442#else
443 if ((tok & OK) &&
444 write(to, tblock[0], writesize) != writesize) {
445 perror(tape);
446#endif
447 kill(master, SIGIOT); /* restart from checkpoint */
448 tok &= ~OK;
449 }
87801efd
KM
450 if ((ret = write(next, &tok, 1)) != 1)
451 ringerr(ret, "write token"); /* Next slave's turn */
23b4aba9 452 }
87801efd
KM
453 if (ret != 0)
454 ringerr(ret, "partial record?");
23b4aba9
KM
455 tmsg("%d CLOSE\n", num);
456}
ca485693
KM
457
458/*
459 * Since a read from a pipe may not return all we asked for
460 * we must loop until we get all we need
461 */
462readpipe(fd, buf, cnt)
463 int fd;
464 char *buf;
465 int cnt;
466{
467 int rd, got;
468
469 for (rd = cnt; rd > 0; rd -= got) {
470 got = read(fd, buf, rd);
471 if (got < 0)
472 return (got);
473 if (got == 0)
474 return (cnt - rd);
475 buf += got;
476 }
477 return (cnt);
478}