tabstr changed to indentprefix to be Sun compatible
[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
e9a09562 8static char sccsid[] = "@(#)tape.c 5.9 (Berkeley) %G%";
76797561 9#endif not lint
7a65e725 10
09e9de78 11#include <sys/file.h>
a40f6134 12#include "dump.h"
e9a09562 13#include "pathnames.h"
ae4b153c 14
c4c501b5
KM
15char (*tblock)[TP_BSIZE]; /* pointer to malloc()ed buffer for tape */
16int writesize; /* size of malloc()ed buffer for tape */
17long lastspclrec = -1; /* tape block number of last written header */
18int trecno = 0; /* next record to write in current block */
a40f6134
KM
19extern int ntrec; /* blocking factor on tape */
20extern int cartridge;
21extern int read(), write();
22#ifdef RDUMP
23extern char *host;
24#endif RDUMP
1ddebffe
SL
25
26/*
09e9de78 27 * Concurrent dump mods (Caltech) - disk block reading and tape writing
23b4aba9
KM
28 * are exported to several slave processes. While one slave writes the
29 * tape, the others read disk blocks; they pass control of the tape in
09e9de78 30 * a ring via flock(). The parent process traverses the filesystem and
a40f6134 31 * sends spclrec()'s and lists of daddr's to the slaves via pipes.
23b4aba9
KM
32 */
33struct req { /* instruction packets sent to slaves */
34 daddr_t dblk;
35 int count;
36} *req;
37int reqsiz;
38
09e9de78 39#define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
a40f6134
KM
40int slavefd[SLAVES]; /* pipes from master to each slave */
41int slavepid[SLAVES]; /* used by killall() */
42int rotor; /* next slave to be instructed */
43int master; /* pid of master, for sending error signals */
44int tenths; /* length of tape used per block written */
23b4aba9 45
1ddebffe
SL
46alloctape()
47{
09e9de78 48 int pgoff = getpagesize() - 1;
a40f6134 49
1ddebffe 50 writesize = ntrec * TP_BSIZE;
a40f6134 51 reqsiz = ntrec * sizeof(struct req);
09e9de78 52 /*
a40f6134
KM
53 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
54 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
55 * repositioning after stopping, i.e, streaming mode, where the gap is
56 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
57 */
58 tenths = writesize/density + (cartridge ? 16 : density == 625 ? 5 : 8);
59 /*
60 * Allocate tape buffer contiguous with the array of instruction
61 * packets, so flusht() can write them together with one write().
62 * Align tape buffer on page boundary to speed up tape write().
09e9de78 63 */
09e9de78
KM
64 req = (struct req *)malloc(reqsiz + writesize + pgoff);
65 if (req == NULL)
66 return(0);
67 tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff);
a40f6134 68 req = (struct req *)tblock - ntrec;
09e9de78 69 return(1);
1ddebffe
SL
70}
71
a40f6134 72
ae4b153c 73taprec(dp)
b6407c9d 74 char *dp;
ae4b153c 75{
23b4aba9
KM
76 req[trecno].dblk = (daddr_t)0;
77 req[trecno].count = 1;
09e9de78 78 *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */
c4c501b5 79 lastspclrec = spcl.c_tapea;
09e9de78 80 trecno++;
ae4b153c 81 spcl.c_tapea++;
09e9de78 82 if(trecno >= ntrec)
ae4b153c
BJ
83 flusht();
84}
85
f5bba473
KM
86dmpblk(blkno, size)
87 daddr_t blkno;
88 int size;
ae4b153c 89{
a40f6134 90 int avail, tpblks, dblkno;
f5bba473 91
b6407c9d 92 dblkno = fsbtodb(sblock, blkno);
23b4aba9
KM
93 tpblks = size / TP_BSIZE;
94 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
23b4aba9
KM
95 req[trecno].dblk = dblkno;
96 req[trecno].count = avail;
a40f6134 97 trecno += avail;
f5bba473 98 spcl.c_tapea += avail;
a40f6134 99 if (trecno >= ntrec)
23b4aba9 100 flusht();
ff96014a 101 dblkno += avail * (TP_BSIZE / dev_bsize);
b6407c9d 102 tpblks -= avail;
f5bba473 103 }
ae4b153c
BJ
104}
105
106int nogripe = 0;
107
23b4aba9
KM
108tperror() {
109 if (pipeout) {
110 msg("Tape write error on %s\n", tape);
111 msg("Cannot recover\n");
112 dumpabort();
113 /* NOTREACHED */
114 }
a40f6134 115 msg("Tape write error %d feet into tape %d\n", asize/120L, tapeno);
23b4aba9
KM
116 broadcast("TAPE ERROR!\n");
117 if (!query("Do you want to restart?"))
118 dumpabort();
119 msg("This tape will rewind. After it is rewound,\n");
120 msg("replace the faulty tape with a new one;\n");
121 msg("this dump volume will be rewritten.\n");
09e9de78 122 killall();
23b4aba9
KM
123 nogripe = 1;
124 close_rewind();
125 Exit(X_REWRITE);
126}
127
a40f6134 128sigpipe()
23b4aba9 129{
a40f6134
KM
130
131 msg("Broken pipe\n");
132 dumpabort();
133}
134
135#ifdef RDUMP
136/*
137 * compatibility routine
138 */
139tflush(i)
23b4aba9 140 int i;
a40f6134 141{
23b4aba9
KM
142
143 for (i = 0; i < ntrec; i++)
144 spclrec();
145}
146#endif RDUMP
147
ae4b153c
BJ
148flusht()
149{
a40f6134 150 int siz = (char *)tblock - (char *)req;
ae4b153c 151
a40f6134
KM
152 if (atomic(write, slavefd[rotor], req, siz) != siz) {
153 perror(" DUMP: error writing command pipe");
09e9de78
KM
154 dumpabort();
155 }
23b4aba9
KM
156 if (++rotor >= SLAVES) rotor = 0;
157 tblock = (char (*)[TP_BSIZE]) &req[ntrec];
ae4b153c 158 trecno = 0;
09e9de78 159 asize += tenths;
1ddebffe 160 blockswritten += ntrec;
a47b7e40 161 if (!pipeout && asize > tsize) {
ae4b153c
BJ
162 close_rewind();
163 otape();
164 }
165 timeest();
166}
167
168rewind()
169{
09e9de78 170 int f;
a47b7e40
KM
171
172 if (pipeout)
173 return;
23b4aba9
KM
174 for (f = 0; f < SLAVES; f++)
175 close(slavefd[f]);
176 while (wait(NULL) >= 0) ; /* wait for any signals from slaves */
177 msg("Tape rewinding\n");
178#ifdef RDUMP
a40f6134
KM
179 if (host) {
180 rmtclose();
181 while (rmtopen(tape, 0) < 0)
182 sleep(10);
183 rmtclose();
184 return;
185 }
186#endif RDUMP
be3f486f
BJ
187 close(to);
188 while ((f = open(tape, 0)) < 0)
189 sleep (10);
190 close(f);
ae4b153c
BJ
191}
192
193close_rewind()
194{
23b4aba9
KM
195 rewind();
196 if (!nogripe) {
ae4b153c
BJ
197 msg("Change Tapes: Mount tape #%d\n", tapeno+1);
198 broadcast("CHANGE TAPES!\7\7\n");
199 }
23b4aba9 200 while (!query("Is the new tape mounted and ready to go?"))
a40f6134 201 if (query("Do you want to abort?")) {
ae4b153c 202 dumpabort();
a40f6134
KM
203 /*NOTREACHED*/
204 }
ae4b153c
BJ
205}
206
207/*
23b4aba9 208 * We implement taking and restoring checkpoints on the tape level.
ae4b153c
BJ
209 * When each tape is opened, a new process is created by forking; this
210 * saves all of the necessary context in the parent. The child
211 * continues the dump; the parent waits around, saving the context.
212 * If the child returns X_REWRITE, then it had problems writing that tape;
213 * this causes the parent to fork again, duplicating the context, and
214 * everything continues as if nothing had happened.
215 */
216
217otape()
218{
219 int parentpid;
220 int childpid;
221 int status;
222 int waitpid;
a40f6134 223 int (*interrupt)() = signal(SIGINT, SIG_IGN);
c4c501b5 224 int blks, i;
ae4b153c 225
ae4b153c
BJ
226 parentpid = getpid();
227
228 restore_check_point:
a40f6134
KM
229 signal(SIGINT, interrupt);
230 /*
231 * All signals are inherited...
232 */
ae4b153c 233 childpid = fork();
23b4aba9 234 if (childpid < 0) {
ae4b153c
BJ
235 msg("Context save fork fails in parent %d\n", parentpid);
236 Exit(X_ABORT);
237 }
23b4aba9 238 if (childpid != 0) {
ae4b153c
BJ
239 /*
240 * PARENT:
241 * save the context by waiting
242 * until the child doing all of the work returns.
23b4aba9 243 * don't catch the interrupt
ae4b153c 244 */
a40f6134 245 signal(SIGINT, SIG_IGN);
ae4b153c
BJ
246#ifdef TDEBUG
247 msg("Tape: %d; parent process: %d child process %d\n",
248 tapeno+1, parentpid, childpid);
249#endif TDEBUG
23b4aba9
KM
250 while ((waitpid = wait(&status)) != childpid)
251 msg("Parent %d waiting for child %d has another child %d return\n",
252 parentpid, childpid, waitpid);
253 if (status & 0xFF) {
ae4b153c
BJ
254 msg("Child %d returns LOB status %o\n",
255 childpid, status&0xFF);
256 }
257 status = (status >> 8) & 0xFF;
258#ifdef TDEBUG
23b4aba9 259 switch(status) {
ae4b153c
BJ
260 case X_FINOK:
261 msg("Child %d finishes X_FINOK\n", childpid);
262 break;
263 case X_ABORT:
264 msg("Child %d finishes X_ABORT\n", childpid);
265 break;
266 case X_REWRITE:
267 msg("Child %d finishes X_REWRITE\n", childpid);
268 break;
269 default:
23b4aba9 270 msg("Child %d finishes unknown %d\n",
a40f6134 271 childpid, status);
ae4b153c
BJ
272 break;
273 }
274#endif TDEBUG
23b4aba9 275 switch(status) {
ae4b153c
BJ
276 case X_FINOK:
277 Exit(X_FINOK);
278 case X_ABORT:
279 Exit(X_ABORT);
280 case X_REWRITE:
281 goto restore_check_point;
282 default:
283 msg("Bad return code from dump: %d\n", status);
284 Exit(X_ABORT);
285 }
286 /*NOTREACHED*/
287 } else { /* we are the child; just continue */
288#ifdef TDEBUG
289 sleep(4); /* allow time for parent's message to get out */
290 msg("Child on Tape %d has parent %d, my pid = %d\n",
291 tapeno+1, parentpid, getpid());
a40f6134 292#endif TDEBUG
23b4aba9 293#ifdef RDUMP
a40f6134
KM
294 while ((to = (host ? rmtopen(tape, 2) :
295 pipeout ? 1 : creat(tape, 0666))) < 0)
296#else RDUMP
23b4aba9 297 while ((to = pipeout ? 1 : creat(tape, 0666)) < 0)
09e9de78 298#endif RDUMP
e9a09562
KM
299 {
300 msg("Cannot open tape \"%s\".\n", tape);
301 if (!query("Do you want to retry the open?"))
23b4aba9 302 dumpabort();
e9a09562 303 }
23b4aba9
KM
304
305 enslave(); /* Share open tape file descriptor with slaves */
ae4b153c
BJ
306
307 asize = 0;
308 tapeno++; /* current tape sequence */
309 newtape++; /* new tape signal */
c4c501b5
KM
310 blks = 0;
311 if (spcl.c_type != TS_END)
312 for (i = 0; i < spcl.c_count; i++)
313 if (spcl.c_addr[i] != 0)
314 blks++;
315 spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec;
ae4b153c
BJ
316 spcl.c_volume++;
317 spcl.c_type = TS_TAPE;
022f1b15 318 spcl.c_flags |= DR_NEWHEADER;
ae4b153c 319 spclrec();
022f1b15 320 spcl.c_flags &=~ DR_NEWHEADER;
ae4b153c
BJ
321 if (tapeno > 1)
322 msg("Tape %d begins with blocks from ino %d\n",
323 tapeno, ino);
324 }
325}
326
ae4b153c
BJ
327dumpabort()
328{
23b4aba9 329 if (master != 0 && master != getpid())
a40f6134 330 kill(master, SIGTERM); /* Signals master to call dumpabort */
09e9de78
KM
331 else {
332 killall();
333 msg("The ENTIRE dump is aborted.\n");
334 }
ae4b153c
BJ
335 Exit(X_ABORT);
336}
337
338Exit(status)
339{
340#ifdef TDEBUG
341 msg("pid = %d exits with status %d\n", getpid(), status);
342#endif TDEBUG
ed7c701e 343 exit(status);
ae4b153c 344}
23b4aba9 345
09e9de78 346/*
a40f6134 347 * could use pipe() for this if flock() worked on pipes
09e9de78
KM
348 */
349lockfile(fd)
350 int fd[2];
351{
352 char tmpname[20];
353
e9a09562 354 strcpy(tmpname, _PATH_LOCK);
09e9de78 355 mktemp(tmpname);
c2b1998f
KM
356 if ((fd[1] = creat(tmpname, 0400)) < 0) {
357 msg("Could not create lockfile ");
358 perror(tmpname);
359 dumpabort();
360 }
361 if ((fd[0] = open(tmpname, 0)) < 0) {
362 msg("Could not reopen lockfile ");
363 perror(tmpname);
364 dumpabort();
365 }
09e9de78 366 unlink(tmpname);
09e9de78 367}
23b4aba9
KM
368
369enslave()
370{
09e9de78
KM
371 int first[2], prev[2], next[2], cmd[2]; /* file descriptors */
372 register int i, j;
23b4aba9
KM
373
374 master = getpid();
a40f6134
KM
375 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
376 signal(SIGPIPE, sigpipe);
377 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
09e9de78
KM
378 lockfile(first);
379 for (i = 0; i < SLAVES; i++) {
380 if (i == 0) {
381 prev[0] = first[1];
382 prev[1] = first[0];
383 } else {
384 prev[0] = next[0];
385 prev[1] = next[1];
386 flock(prev[1], LOCK_EX);
387 }
c2b1998f
KM
388 if (i < SLAVES - 1) {
389 lockfile(next);
390 } else {
391 next[0] = first[0];
392 next[1] = first[1]; /* Last slave loops back */
393 }
394 if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) {
395 msg("too many slaves, %d (recompile smaller) ", i);
396 perror("");
23b4aba9
KM
397 dumpabort();
398 }
23b4aba9 399 slavefd[i] = cmd[1];
a40f6134 400 if (slavepid[i] == 0) { /* Slave starts up here */
23b4aba9
KM
401 for (j = 0; j <= i; j++)
402 close(slavefd[j]);
a40f6134
KM
403 signal(SIGINT, SIG_IGN); /* Master handles this */
404 doslave(cmd[0], prev, next);
23b4aba9
KM
405 Exit(X_FINOK);
406 }
407 close(cmd[0]);
09e9de78
KM
408 if (i > 0) {
409 close(prev[0]);
410 close(prev[1]);
411 }
23b4aba9 412 }
09e9de78
KM
413 close(first[0]);
414 close(first[1]);
415 master = 0; rotor = 0;
23b4aba9
KM
416}
417
09e9de78 418killall()
87801efd 419{
09e9de78 420 register int i;
87801efd 421
09e9de78
KM
422 for (i = 0; i < SLAVES; i++)
423 if (slavepid[i] > 0)
424 kill(slavepid[i], SIGKILL);
87801efd
KM
425}
426
09e9de78
KM
427/*
428 * Synchronization - each process has a lockfile, and shares file
429 * descriptors to the following process's lockfile. When our write
430 * completes, we release our lock on the following process's lock-
431 * file, allowing the following process to lock it and proceed. We
432 * get the lock back for the next cycle by swapping descriptors.
433 */
a40f6134
KM
434doslave(cmd, prev, next)
435 register int cmd, prev[2], next[2];
23b4aba9 436{
a40f6134 437 register int nread, toggle = 0;
87801efd 438
23b4aba9 439 close(fi);
a40f6134 440 if ((fi = open(disk, 0)) < 0) { /* Need our own seek pointer */
09e9de78 441 perror(" DUMP: slave couldn't reopen disk");
a40f6134 442 dumpabort();
23b4aba9 443 }
09e9de78 444 /*
a40f6134 445 * Get list of blocks to dump, read the blocks into tape buffer
09e9de78 446 */
a40f6134 447 while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) {
23b4aba9
KM
448 register struct req *p = req;
449 for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) {
450 if (p->dblk) {
23b4aba9 451 bread(p->dblk, tblock[trecno],
a40f6134 452 p->count * TP_BSIZE);
23b4aba9 453 } else {
a40f6134
KM
454 if (p->count != 1 || atomic(read, cmd,
455 tblock[trecno], TP_BSIZE) != TP_BSIZE) {
cd928082 456 msg("Master/slave protocol botched.\n");
09e9de78
KM
457 dumpabort();
458 }
23b4aba9
KM
459 }
460 }
09e9de78 461 flock(prev[toggle], LOCK_EX); /* Wait our turn */
a40f6134 462
23b4aba9 463#ifdef RDUMP
a40f6134
KM
464 if ((host ? rmtwrite(tblock[0], writesize)
465 : write(to, tblock[0], writesize)) != writesize) {
466#else RDUMP
467 if (write(to, tblock[0], writesize) != writesize) {
09e9de78 468#endif RDUMP
a40f6134
KM
469 kill(master, SIGUSR1);
470 for (;;)
471 sigpause(0);
23b4aba9 472 }
09e9de78
KM
473 toggle ^= 1;
474 flock(next[toggle], LOCK_UN); /* Next slave's turn */
475 } /* Also jolts him awake */
a40f6134
KM
476 if (nread != 0) {
477 perror(" DUMP: error reading command pipe");
478 dumpabort();
23b4aba9 479 }
23b4aba9 480}
ca485693
KM
481
482/*
a40f6134
KM
483 * Since a read from a pipe may not return all we asked for,
484 * or a write may not write all we ask if we get a signal,
485 * loop until the count is satisfied (or error).
ca485693 486 */
a40f6134
KM
487atomic(func, fd, buf, count)
488 int (*func)(), fd, count;
ca485693 489 char *buf;
ca485693 490{
a40f6134
KM
491 int got, need = count;
492
493 while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
ca485693 494 buf += got;
a40f6134 495 return (got < 0 ? got : count - need);
ca485693 496}