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