Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1980, 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)dumptape.c 5.18 (Berkeley) 4/24/91"; | |
78ed81a3 | 36 | static char rcsid[] = "$Header: /b/source/CVS/src/sbin/dump/dumptape.c,v 1.3 1993/03/23 00:27:15 cgd Exp $"; |
15637ed4 RG |
37 | #endif /* not lint */ |
38 | ||
39 | #include <sys/param.h> | |
40 | #include <sys/wait.h> | |
41 | #include <ufs/dir.h> | |
42 | #include <ufs/dinode.h> | |
43 | #include <ufs/fs.h> | |
44 | #include <signal.h> | |
45 | #include <fcntl.h> | |
46 | #include <protocols/dumprestore.h> | |
47 | #include <errno.h> | |
48 | #ifdef __STDC__ | |
49 | #include <unistd.h> | |
50 | #include <stdlib.h> | |
51 | #include <string.h> | |
52 | #endif | |
53 | #include "dump.h" | |
54 | #include "pathnames.h" | |
55 | ||
56 | char (*tblock)[TP_BSIZE]; /* pointer to malloc()ed buffer for tape */ | |
57 | int writesize; /* size of malloc()ed buffer for tape */ | |
58 | long lastspclrec = -1; /* tape block number of last written header */ | |
59 | int trecno = 0; /* next record to write in current block */ | |
60 | extern long blocksperfile; /* number of blocks per output file */ | |
61 | long blocksthisvol; /* number of blocks on current output file */ | |
62 | extern int ntrec; /* blocking factor on tape */ | |
63 | extern int cartridge; | |
64 | char *nexttape; | |
65 | #ifdef RDUMP | |
66 | extern char *host; | |
67 | int rmtopen(), rmtwrite(); | |
68 | void rmtclose(); | |
69 | #endif RDUMP | |
70 | ||
71 | int atomic(); | |
72 | void doslave(), enslave(), flushtape(), killall(); | |
73 | ||
74 | /* | |
75 | * Concurrent dump mods (Caltech) - disk block reading and tape writing | |
76 | * are exported to several slave processes. While one slave writes the | |
77 | * tape, the others read disk blocks; they pass control of the tape in | |
78 | * a ring via flock(). The parent process traverses the filesystem and | |
79 | * sends writeheader()'s and lists of daddr's to the slaves via pipes. | |
80 | */ | |
81 | struct req { /* instruction packets sent to slaves */ | |
82 | daddr_t dblk; | |
83 | int count; | |
84 | } *req; | |
85 | int reqsiz; | |
86 | ||
87 | #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ | |
88 | int slavefd[SLAVES]; /* pipes from master to each slave */ | |
89 | int slavepid[SLAVES]; /* used by killall() */ | |
90 | int rotor; /* next slave to be instructed */ | |
91 | int master; /* pid of master, for sending error signals */ | |
92 | int tenths; /* length of tape used per block written */ | |
93 | ||
94 | int | |
95 | alloctape() | |
96 | { | |
97 | int pgoff = getpagesize() - 1; | |
98 | ||
99 | writesize = ntrec * TP_BSIZE; | |
100 | reqsiz = ntrec * sizeof(struct req); | |
101 | /* | |
102 | * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode | |
103 | * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require | |
104 | * repositioning after stopping, i.e, streaming mode, where the gap is | |
105 | * variable, 0.30" to 0.45". The gap is maximal when the tape stops. | |
106 | */ | |
107 | if (blocksperfile == 0) | |
108 | tenths = writesize / density + | |
109 | (cartridge ? 16 : density == 625 ? 5 : 8); | |
110 | /* | |
111 | * Allocate tape buffer contiguous with the array of instruction | |
112 | * packets, so flushtape() can write them together with one write(). | |
113 | * Align tape buffer on page boundary to speed up tape write(). | |
114 | */ | |
115 | req = (struct req *)malloc(reqsiz + writesize + pgoff); | |
116 | if (req == NULL) | |
117 | return(0); | |
118 | tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff); | |
119 | req = (struct req *)tblock - ntrec; | |
120 | return(1); | |
121 | } | |
122 | ||
123 | ||
124 | void | |
125 | writerec(dp) | |
126 | char *dp; | |
127 | { | |
128 | req[trecno].dblk = (daddr_t)0; | |
129 | req[trecno].count = 1; | |
130 | *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */ | |
131 | lastspclrec = spcl.c_tapea; | |
132 | trecno++; | |
133 | spcl.c_tapea++; | |
134 | if (trecno >= ntrec) | |
135 | flushtape(); | |
136 | } | |
137 | ||
138 | void | |
139 | dumpblock(blkno, size) | |
140 | daddr_t blkno; | |
141 | int size; | |
142 | { | |
143 | int avail, tpblks, dblkno; | |
144 | ||
145 | dblkno = fsbtodb(sblock, blkno); | |
146 | tpblks = size >> tp_bshift; | |
147 | while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { | |
148 | req[trecno].dblk = dblkno; | |
149 | req[trecno].count = avail; | |
150 | trecno += avail; | |
151 | spcl.c_tapea += avail; | |
152 | if (trecno >= ntrec) | |
153 | flushtape(); | |
154 | dblkno += avail << (tp_bshift - dev_bshift); | |
155 | tpblks -= avail; | |
156 | } | |
157 | } | |
158 | ||
159 | int nogripe = 0; | |
160 | ||
161 | void | |
162 | tperror() | |
163 | { | |
164 | if (pipeout) { | |
165 | msg("write error on %s\n", tape); | |
166 | quit("Cannot recover\n"); | |
167 | /* NOTREACHED */ | |
168 | } | |
169 | msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno); | |
170 | broadcast("DUMP WRITE ERROR!\n"); | |
171 | if (!query("Do you want to restart?")) | |
172 | dumpabort(); | |
173 | msg("Closing this volume. Prepare to restart with new media;\n"); | |
174 | msg("this dump volume will be rewritten.\n"); | |
175 | killall(); | |
176 | nogripe = 1; | |
177 | close_rewind(); | |
178 | Exit(X_REWRITE); | |
179 | } | |
180 | ||
181 | void | |
182 | sigpipe() | |
183 | { | |
184 | ||
185 | quit("Broken pipe\n"); | |
186 | } | |
187 | ||
188 | void | |
189 | flushtape() | |
190 | { | |
191 | #ifndef __STDC__ | |
192 | int write(); | |
193 | #endif | |
194 | ||
195 | int siz = (char *)tblock - (char *)req; | |
196 | ||
197 | if (atomic(write, slavefd[rotor], req, siz) != siz) | |
198 | quit("error writing command pipe: %s\n", strerror(errno)); | |
199 | if (++rotor >= SLAVES) | |
200 | rotor = 0; | |
201 | tblock = (char (*)[TP_BSIZE]) &req[ntrec]; | |
202 | trecno = 0; | |
203 | asize += tenths; | |
204 | blockswritten += ntrec; | |
205 | blocksthisvol += ntrec; | |
206 | if (!pipeout && (blocksperfile ? | |
207 | (blocksthisvol >= blocksperfile) : (asize > tsize))) { | |
208 | close_rewind(); | |
209 | startnewtape(); | |
210 | } | |
211 | timeest(); | |
212 | } | |
213 | ||
214 | void | |
215 | trewind() | |
216 | { | |
217 | int f; | |
218 | ||
219 | if (pipeout) | |
220 | return; | |
221 | for (f = 0; f < SLAVES; f++) | |
222 | close(slavefd[f]); | |
223 | while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ | |
224 | /* void */; | |
225 | msg("Closing %s\n", tape); | |
226 | #ifdef RDUMP | |
227 | if (host) { | |
228 | rmtclose(); | |
229 | while (rmtopen(tape, 0) < 0) | |
230 | sleep(10); | |
231 | rmtclose(); | |
232 | return; | |
233 | } | |
234 | #endif RDUMP | |
235 | close(tapefd); | |
236 | while ((f = open(tape, 0)) < 0) | |
237 | sleep (10); | |
238 | close(f); | |
239 | } | |
240 | ||
241 | void | |
242 | close_rewind() | |
243 | { | |
244 | trewind(); | |
245 | if (nexttape) | |
246 | return; | |
247 | if (!nogripe) { | |
248 | msg("Change Volumes: Mount volume #%d\n", tapeno+1); | |
249 | broadcast("CHANGE DUMP VOLUMES!\7\7\n"); | |
250 | } | |
251 | while (!query("Is the new volume mounted and ready to go?")) | |
252 | if (query("Do you want to abort?")) { | |
253 | dumpabort(); | |
254 | /*NOTREACHED*/ | |
255 | } | |
256 | } | |
257 | ||
258 | /* | |
259 | * We implement taking and restoring checkpoints on the tape level. | |
260 | * When each tape is opened, a new process is created by forking; this | |
261 | * saves all of the necessary context in the parent. The child | |
262 | * continues the dump; the parent waits around, saving the context. | |
263 | * If the child returns X_REWRITE, then it had problems writing that tape; | |
264 | * this causes the parent to fork again, duplicating the context, and | |
265 | * everything continues as if nothing had happened. | |
266 | */ | |
267 | ||
268 | void | |
269 | startnewtape() | |
270 | { | |
271 | int parentpid; | |
272 | int childpid; | |
273 | int status; | |
274 | int waitpid; | |
275 | sig_t interrupt; | |
276 | int blks, i; | |
277 | char *p; | |
278 | ||
279 | interrupt = signal(SIGINT, SIG_IGN); | |
280 | parentpid = getpid(); | |
281 | ||
282 | restore_check_point: | |
283 | (void)signal(SIGINT, interrupt); | |
284 | /* | |
285 | * All signals are inherited... | |
286 | */ | |
287 | childpid = fork(); | |
288 | if (childpid < 0) { | |
289 | msg("Context save fork fails in parent %d\n", parentpid); | |
290 | Exit(X_ABORT); | |
291 | } | |
292 | if (childpid != 0) { | |
293 | /* | |
294 | * PARENT: | |
295 | * save the context by waiting | |
296 | * until the child doing all of the work returns. | |
297 | * don't catch the interrupt | |
298 | */ | |
299 | signal(SIGINT, SIG_IGN); | |
300 | #ifdef TDEBUG | |
301 | msg("Tape: %d; parent process: %d child process %d\n", | |
302 | tapeno+1, parentpid, childpid); | |
303 | #endif TDEBUG | |
304 | while ((waitpid = wait(&status)) != childpid) | |
305 | msg("Parent %d waiting for child %d has another child %d return\n", | |
306 | parentpid, childpid, waitpid); | |
307 | if (status & 0xFF) { | |
308 | msg("Child %d returns LOB status %o\n", | |
309 | childpid, status&0xFF); | |
310 | } | |
311 | status = (status >> 8) & 0xFF; | |
312 | #ifdef TDEBUG | |
313 | switch(status) { | |
314 | case X_FINOK: | |
315 | msg("Child %d finishes X_FINOK\n", childpid); | |
316 | break; | |
317 | case X_ABORT: | |
318 | msg("Child %d finishes X_ABORT\n", childpid); | |
319 | break; | |
320 | case X_REWRITE: | |
321 | msg("Child %d finishes X_REWRITE\n", childpid); | |
322 | break; | |
323 | default: | |
324 | msg("Child %d finishes unknown %d\n", | |
325 | childpid, status); | |
326 | break; | |
327 | } | |
328 | #endif TDEBUG | |
329 | switch(status) { | |
330 | case X_FINOK: | |
331 | Exit(X_FINOK); | |
332 | case X_ABORT: | |
333 | Exit(X_ABORT); | |
334 | case X_REWRITE: | |
335 | goto restore_check_point; | |
336 | default: | |
337 | msg("Bad return code from dump: %d\n", status); | |
338 | Exit(X_ABORT); | |
339 | } | |
340 | /*NOTREACHED*/ | |
341 | } else { /* we are the child; just continue */ | |
342 | #ifdef TDEBUG | |
343 | sleep(4); /* allow time for parent's message to get out */ | |
344 | msg("Child on Tape %d has parent %d, my pid = %d\n", | |
345 | tapeno+1, parentpid, getpid()); | |
346 | #endif TDEBUG | |
347 | /* | |
348 | * If we have a name like "/dev/rmt0,/dev/rmt1", | |
349 | * use the name before the comma first, and save | |
350 | * the remaining names for subsequent volumes. | |
351 | */ | |
352 | tapeno++; /* current tape sequence */ | |
353 | if (nexttape || index(tape, ',')) { | |
354 | if (nexttape && *nexttape) | |
355 | tape = nexttape; | |
356 | if (p = index(tape, ',')) { | |
357 | *p = '\0'; | |
358 | nexttape = p + 1; | |
359 | } else | |
360 | nexttape = NULL; | |
361 | msg("Dumping volume %d on %s\n", tapeno, tape); | |
362 | } | |
363 | #ifdef RDUMP | |
364 | while ((tapefd = (host ? rmtopen(tape, 2) : | |
365 | pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0) | |
366 | #else RDUMP | |
367 | while ((tapefd = | |
368 | pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666)) < 0) | |
369 | #endif RDUMP | |
370 | { | |
371 | msg("Cannot open output \"%s\".\n", tape); | |
372 | if (!query("Do you want to retry the open?")) | |
373 | dumpabort(); | |
374 | } | |
375 | ||
376 | enslave(); /* Share open tape file descriptor with slaves */ | |
377 | ||
378 | asize = 0; | |
379 | blocksthisvol = 0; | |
380 | newtape++; /* new tape signal */ | |
381 | blks = 0; | |
382 | if (spcl.c_type != TS_END) | |
383 | for (i = 0; i < spcl.c_count; i++) | |
384 | if (spcl.c_addr[i] != 0) | |
385 | blks++; | |
386 | spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec; | |
387 | spcl.c_volume++; | |
388 | spcl.c_type = TS_TAPE; | |
389 | spcl.c_flags |= DR_NEWHEADER; | |
390 | writeheader(curino); | |
391 | spcl.c_flags &=~ DR_NEWHEADER; | |
392 | if (tapeno > 1) | |
393 | msg("Volume %d begins with blocks from inode %d\n", | |
394 | tapeno, curino); | |
395 | } | |
396 | } | |
397 | ||
398 | void | |
399 | dumpabort() | |
400 | { | |
401 | if (master != 0 && master != getpid()) | |
402 | kill(master, SIGTERM); /* Signals master to call dumpabort */ | |
403 | else { | |
404 | killall(); | |
405 | msg("The ENTIRE dump is aborted.\n"); | |
406 | } | |
407 | Exit(X_ABORT); | |
408 | } | |
409 | ||
410 | void | |
411 | Exit(status) | |
412 | int status; | |
413 | { | |
414 | #ifdef TDEBUG | |
415 | msg("pid = %d exits with status %d\n", getpid(), status); | |
416 | #endif TDEBUG | |
417 | exit(status); | |
418 | } | |
419 | ||
420 | /* | |
421 | * could use pipe() for this if flock() worked on pipes | |
422 | */ | |
423 | void | |
424 | lockfile(fd) | |
425 | int fd[2]; | |
426 | { | |
427 | char tmpname[20]; | |
428 | ||
429 | strcpy(tmpname, _PATH_LOCK); | |
430 | mktemp(tmpname); | |
431 | if ((fd[1] = creat(tmpname, 0400)) < 0) | |
432 | quit("cannot create lockfile %s: %s\n", | |
433 | tmpname, strerror(errno)); | |
434 | if ((fd[0] = open(tmpname, 0)) < 0) | |
435 | quit("cannot reopen lockfile %s: %s\n", | |
436 | tmpname, strerror(errno)); | |
437 | (void) unlink(tmpname); | |
438 | } | |
439 | ||
440 | void | |
441 | enslave() | |
442 | { | |
443 | int first[2], prev[2], next[2], cmd[2]; /* file descriptors */ | |
444 | register int i, j; | |
445 | ||
446 | master = getpid(); | |
447 | signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ | |
448 | signal(SIGPIPE, sigpipe); | |
449 | signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ | |
450 | lockfile(first); | |
451 | for (i = 0; i < SLAVES; i++) { | |
452 | if (i == 0) { | |
453 | prev[0] = first[1]; | |
454 | prev[1] = first[0]; | |
455 | } else { | |
456 | prev[0] = next[0]; | |
457 | prev[1] = next[1]; | |
458 | flock(prev[1], LOCK_EX); | |
459 | } | |
460 | if (i < SLAVES - 1) { | |
461 | lockfile(next); | |
462 | } else { | |
463 | next[0] = first[0]; | |
464 | next[1] = first[1]; /* Last slave loops back */ | |
465 | } | |
466 | if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) | |
467 | quit("too many slaves, %d (recompile smaller): %s\n", | |
468 | i, strerror(errno)); | |
469 | slavefd[i] = cmd[1]; | |
470 | if (slavepid[i] == 0) { /* Slave starts up here */ | |
471 | for (j = 0; j <= i; j++) | |
472 | close(slavefd[j]); | |
473 | signal(SIGINT, SIG_IGN); /* Master handles this */ | |
474 | doslave(cmd[0], prev, next); | |
475 | Exit(X_FINOK); | |
476 | } | |
477 | close(cmd[0]); | |
478 | if (i > 0) { | |
479 | close(prev[0]); | |
480 | close(prev[1]); | |
481 | } | |
482 | } | |
483 | close(first[0]); | |
484 | close(first[1]); | |
485 | master = 0; rotor = 0; | |
486 | } | |
487 | ||
488 | void | |
489 | killall() | |
490 | { | |
491 | register int i; | |
492 | ||
493 | for (i = 0; i < SLAVES; i++) | |
494 | if (slavepid[i] > 0) | |
495 | kill(slavepid[i], SIGKILL); | |
496 | } | |
497 | ||
498 | /* | |
499 | * Synchronization - each process has a lockfile, and shares file | |
500 | * descriptors to the following process's lockfile. When our write | |
501 | * completes, we release our lock on the following process's lock- | |
502 | * file, allowing the following process to lock it and proceed. We | |
503 | * get the lock back for the next cycle by swapping descriptors. | |
504 | */ | |
505 | void | |
506 | doslave(cmd, prev, next) | |
507 | register int cmd, prev[2], next[2]; | |
508 | { | |
509 | register int nread, toggle = 0; | |
510 | int nwrite; | |
511 | #ifndef __STDC__ | |
512 | int read(); | |
513 | #endif | |
514 | ||
515 | /* | |
516 | * Need our own seek pointer. | |
517 | */ | |
518 | close(diskfd); | |
519 | if ((diskfd = open(disk, O_RDONLY)) < 0) | |
520 | quit("slave couldn't reopen disk: %s\n", strerror(errno)); | |
521 | /* | |
522 | * Get list of blocks to dump, read the blocks into tape buffer | |
523 | */ | |
524 | while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) { | |
525 | register struct req *p = req; | |
526 | for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) { | |
527 | if (p->dblk) { | |
528 | bread(p->dblk, tblock[trecno], | |
529 | p->count * TP_BSIZE); | |
530 | } else { | |
531 | if (p->count != 1 || atomic(read, cmd, | |
532 | tblock[trecno], TP_BSIZE) != TP_BSIZE) | |
533 | quit("master/slave protocol botched.\n"); | |
534 | } | |
535 | } | |
536 | flock(prev[toggle], LOCK_EX); /* Wait our turn */ | |
537 | ||
538 | #ifdef RDUMP | |
539 | if ((nwrite = (host ? rmtwrite(tblock[0], writesize) | |
540 | : write(tapefd, tblock[0], writesize))) != writesize) { | |
541 | #else RDUMP | |
542 | if ((nwrite = write(tapefd, tblock[0], writesize)) | |
543 | != writesize) { | |
544 | #endif RDUMP | |
545 | if (nwrite == -1) | |
546 | perror("write"); | |
547 | else | |
548 | msg("short write: got %d instead of %d\n", | |
549 | nwrite, writesize); | |
550 | kill(master, SIGUSR1); | |
551 | for (;;) | |
552 | sigpause(0); | |
553 | } | |
554 | toggle ^= 1; | |
555 | flock(next[toggle], LOCK_UN); /* Next slave's turn */ | |
556 | } /* Also jolts him awake */ | |
557 | if (nread != 0) | |
558 | quit("error reading command pipe: %s\n", strerror(errno)); | |
559 | } | |
560 | ||
561 | /* | |
562 | * Since a read from a pipe may not return all we asked for, | |
563 | * or a write may not write all we ask if we get a signal, | |
564 | * loop until the count is satisfied (or error). | |
565 | */ | |
566 | int | |
567 | atomic(func, fd, buf, count) | |
568 | int (*func)(), fd, count; | |
569 | char *buf; | |
570 | { | |
571 | int got, need = count; | |
572 | ||
573 | while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) | |
574 | buf += got; | |
575 | return (got < 0 ? got : count - need); | |
576 | } |