]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 SP |
4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
5 | * Stelian Pop <pop@cybercable.fr>, 1999 | |
1227625a SP |
6 | * |
7 | */ | |
8 | ||
9 | /*- | |
10 | * Copyright (c) 1980, 1991, 1993 | |
11 | * The Regents of the University of California. All rights reserved. | |
12 | * | |
13 | * Redistribution and use in source and binary forms, with or without | |
14 | * modification, are permitted provided that the following conditions | |
15 | * are met: | |
16 | * 1. Redistributions of source code must retain the above copyright | |
17 | * notice, this list of conditions and the following disclaimer. | |
18 | * 2. Redistributions in binary form must reproduce the above copyright | |
19 | * notice, this list of conditions and the following disclaimer in the | |
20 | * documentation and/or other materials provided with the distribution. | |
21 | * 3. All advertising materials mentioning features or use of this software | |
22 | * must display the following acknowledgement: | |
23 | * This product includes software developed by the University of | |
24 | * California, Berkeley and its contributors. | |
25 | * 4. Neither the name of the University nor the names of its contributors | |
26 | * may be used to endorse or promote products derived from this software | |
27 | * without specific prior written permission. | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
30 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
33 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
35 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
36 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
37 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
38 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
39 | * SUCH DAMAGE. | |
40 | */ | |
41 | ||
42 | #ifndef lint | |
b45f51d6 | 43 | #if 0 |
1227625a | 44 | static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95"; |
b45f51d6 SP |
45 | #endif |
46 | static const char rcsid[] = | |
47 | "$Id: tape.c,v 1.2 1999/10/11 12:53:22 stelian Exp $"; | |
1227625a SP |
48 | #endif /* not lint */ |
49 | ||
b45f51d6 SP |
50 | #ifdef __linux__ |
51 | #include <sys/types.h> | |
52 | #include <linux/types.h> | |
53 | #endif | |
1227625a SP |
54 | #include <sys/param.h> |
55 | #include <sys/socket.h> | |
56 | #include <sys/time.h> | |
57 | #include <sys/wait.h> | |
58 | #ifdef __linux__ | |
59 | #include <linux/ext2_fs.h> | |
60 | #include <bsdcompat.h> | |
61 | #else /* __linux__ */ | |
62 | #ifdef sunos | |
63 | #include <sys/vnode.h> | |
64 | ||
65 | #include <ufs/fs.h> | |
66 | #include <ufs/inode.h> | |
67 | #else | |
68 | #include <ufs/ufs/dinode.h> | |
69 | #include <ufs/ffs/fs.h> | |
70 | #endif | |
71 | #endif /* __linux__ */ | |
72 | ||
73 | #include <protocols/dumprestore.h> | |
74 | ||
75 | #include <errno.h> | |
76 | #include <fcntl.h> | |
77 | #include <setjmp.h> | |
78 | #include <signal.h> | |
79 | #include <stdio.h> | |
80 | #ifdef __STDC__ | |
81 | #include <stdlib.h> | |
82 | #include <string.h> | |
83 | #include <unistd.h> | |
84 | #else | |
85 | int write(), read(); | |
86 | #endif | |
87 | ||
88 | #ifdef __linux__ | |
89 | #include <ext2fs/ext2fs.h> | |
90 | #endif | |
91 | #include "dump.h" | |
1227625a SP |
92 | |
93 | int writesize; /* size of malloc()ed buffer for tape */ | |
94 | long lastspclrec = -1; /* tape block number of last written header */ | |
95 | int trecno = 0; /* next record to write in current block */ | |
96 | extern long blocksperfile; /* number of blocks per output file */ | |
97 | long blocksthisvol; /* number of blocks on current output file */ | |
98 | extern int ntrec; /* blocking factor on tape */ | |
99 | extern int cartridge; | |
100 | extern char *host; | |
101 | char *nexttape; | |
102 | ||
b45f51d6 | 103 | static int atomic __P((ssize_t (*)(), int, char *, int)); |
1227625a SP |
104 | static void doslave __P((int, int)); |
105 | static void enslave __P((void)); | |
106 | static void flushtape __P((void)); | |
107 | static void killall __P((void)); | |
108 | static void rollforward __P((void)); | |
109 | ||
110 | /* | |
111 | * Concurrent dump mods (Caltech) - disk block reading and tape writing | |
112 | * are exported to several slave processes. While one slave writes the | |
113 | * tape, the others read disk blocks; they pass control of the tape in | |
114 | * a ring via signals. The parent process traverses the filesystem and | |
115 | * sends writeheader()'s and lists of daddr's to the slaves via pipes. | |
116 | * The following structure defines the instruction packets sent to slaves. | |
117 | */ | |
118 | struct req { | |
119 | daddr_t dblk; | |
120 | int count; | |
121 | }; | |
122 | int reqsiz; | |
123 | ||
124 | #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ | |
125 | struct slave { | |
126 | int tapea; /* header number at start of this chunk */ | |
127 | int count; /* count to next header (used for TS_TAPE */ | |
128 | /* after EOT) */ | |
129 | int inode; /* inode that we are currently dealing with */ | |
130 | int fd; /* FD for this slave */ | |
131 | int pid; /* PID for this slave */ | |
132 | int sent; /* 1 == we've sent this slave requests */ | |
133 | int firstrec; /* record number of this block */ | |
134 | char (*tblock)[TP_BSIZE]; /* buffer for data blocks */ | |
135 | struct req *req; /* buffer for requests */ | |
136 | } slaves[SLAVES+1]; | |
137 | struct slave *slp; | |
138 | ||
139 | char (*nextblock)[TP_BSIZE]; | |
140 | ||
141 | int master; /* pid of master, for sending error signals */ | |
142 | int tenths; /* length of tape used per block written */ | |
143 | static int caught; /* have we caught the signal to proceed? */ | |
144 | static int ready; /* have we reached the lock point without having */ | |
145 | /* received the SIGUSR2 signal from the prev slave? */ | |
146 | static jmp_buf jmpbuf; /* where to jump to if we are ready when the */ | |
147 | /* SIGUSR2 arrives from the previous slave */ | |
148 | ||
149 | int | |
150 | alloctape() | |
151 | { | |
152 | int pgoff = getpagesize() - 1; | |
153 | char *buf; | |
154 | int i; | |
155 | ||
156 | writesize = ntrec * TP_BSIZE; | |
157 | reqsiz = (ntrec + 1) * sizeof(struct req); | |
158 | /* | |
159 | * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode | |
160 | * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require | |
161 | * repositioning after stopping, i.e, streaming mode, where the gap is | |
162 | * variable, 0.30" to 0.45". The gap is maximal when the tape stops. | |
163 | */ | |
b45f51d6 | 164 | if (blocksperfile == 0 && !unlimited) |
1227625a SP |
165 | tenths = writesize / density + |
166 | (cartridge ? 16 : density == 625 ? 5 : 8); | |
167 | /* | |
168 | * Allocate tape buffer contiguous with the array of instruction | |
169 | * packets, so flushtape() can write them together with one write(). | |
170 | * Align tape buffer on page boundary to speed up tape write(). | |
171 | */ | |
172 | for (i = 0; i <= SLAVES; i++) { | |
173 | buf = (char *) | |
174 | malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE)); | |
175 | if (buf == NULL) | |
176 | return(0); | |
177 | slaves[i].tblock = (char (*)[TP_BSIZE]) | |
178 | #ifdef __linux__ | |
179 | (((long)&buf[reqsiz] + pgoff) &~ pgoff); | |
180 | #else | |
181 | (((long)&buf[ntrec + 1] + pgoff) &~ pgoff); | |
182 | #endif | |
183 | slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1; | |
184 | } | |
185 | slp = &slaves[0]; | |
186 | slp->count = 1; | |
187 | slp->tapea = 0; | |
188 | slp->firstrec = 0; | |
189 | nextblock = slp->tblock; | |
190 | return(1); | |
191 | } | |
192 | ||
193 | void | |
194 | writerec(dp, isspcl) | |
195 | char *dp; | |
196 | int isspcl; | |
197 | { | |
198 | ||
199 | slp->req[trecno].dblk = (daddr_t)0; | |
200 | slp->req[trecno].count = 1; | |
201 | *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp; | |
202 | if (isspcl) | |
203 | lastspclrec = spcl.c_tapea; | |
204 | trecno++; | |
205 | spcl.c_tapea++; | |
206 | if (trecno >= ntrec) | |
207 | flushtape(); | |
208 | } | |
209 | ||
210 | void | |
211 | dumpblock(blkno, size) | |
212 | daddr_t blkno; | |
213 | int size; | |
214 | { | |
215 | int avail, tpblks, dblkno; | |
216 | ||
217 | dblkno = fsbtodb(sblock, blkno); | |
218 | tpblks = size >> tp_bshift; | |
219 | while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { | |
220 | slp->req[trecno].dblk = dblkno; | |
221 | slp->req[trecno].count = avail; | |
222 | trecno += avail; | |
223 | spcl.c_tapea += avail; | |
224 | if (trecno >= ntrec) | |
225 | flushtape(); | |
226 | dblkno += avail << (tp_bshift - dev_bshift); | |
227 | tpblks -= avail; | |
228 | } | |
229 | } | |
230 | ||
231 | int nogripe = 0; | |
232 | ||
233 | void | |
234 | tperror(signo) | |
235 | int signo; | |
236 | { | |
237 | ||
238 | if (pipeout) { | |
239 | msg("write error on %s\n", tape); | |
240 | quit("Cannot recover\n"); | |
241 | /* NOTREACHED */ | |
242 | } | |
243 | msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno); | |
244 | broadcast("DUMP WRITE ERROR!\n"); | |
245 | if (!query("Do you want to restart?")) | |
246 | dumpabort(0); | |
247 | msg("Closing this volume. Prepare to restart with new media;\n"); | |
248 | msg("this dump volume will be rewritten.\n"); | |
249 | killall(); | |
250 | nogripe = 1; | |
251 | close_rewind(); | |
252 | Exit(X_REWRITE); | |
253 | } | |
254 | ||
255 | void | |
256 | sigpipe(signo) | |
257 | int signo; | |
258 | { | |
259 | ||
260 | quit("Broken pipe\n"); | |
261 | } | |
262 | ||
263 | static void | |
264 | flushtape() | |
265 | { | |
266 | int i, blks, got; | |
267 | long lastfirstrec; | |
268 | ||
269 | int siz = (char *)nextblock - (char *)slp->req; | |
270 | ||
271 | slp->req[trecno].count = 0; /* Sentinel */ | |
272 | ||
273 | if (atomic(write, slp->fd, (char *)slp->req, siz) != siz) | |
274 | quit("error writing command pipe: %s\n", strerror(errno)); | |
275 | slp->sent = 1; /* we sent a request, read the response later */ | |
276 | ||
277 | lastfirstrec = slp->firstrec; | |
278 | ||
279 | if (++slp >= &slaves[SLAVES]) | |
280 | slp = &slaves[0]; | |
281 | ||
282 | /* Read results back from next slave */ | |
283 | if (slp->sent) { | |
284 | if (atomic(read, slp->fd, (char *)&got, sizeof got) | |
285 | != sizeof got) { | |
286 | perror(" DUMP: error reading command pipe in master"); | |
287 | dumpabort(0); | |
288 | } | |
289 | slp->sent = 0; | |
290 | ||
291 | /* Check for end of tape */ | |
292 | if (got < writesize) { | |
293 | msg("End of tape detected\n"); | |
294 | ||
295 | /* | |
296 | * Drain the results, don't care what the values were. | |
297 | * If we read them here then trewind won't... | |
298 | */ | |
299 | for (i = 0; i < SLAVES; i++) { | |
300 | if (slaves[i].sent) { | |
301 | if (atomic(read, slaves[i].fd, | |
302 | (char *)&got, sizeof got) | |
303 | != sizeof got) { | |
304 | perror(" DUMP: error reading command pipe in master"); | |
305 | dumpabort(0); | |
306 | } | |
307 | slaves[i].sent = 0; | |
308 | } | |
309 | } | |
310 | ||
311 | close_rewind(); | |
312 | rollforward(); | |
313 | return; | |
314 | } | |
315 | } | |
316 | ||
317 | blks = 0; | |
318 | if (spcl.c_type != TS_END) { | |
319 | for (i = 0; i < spcl.c_count; i++) | |
320 | if (spcl.c_addr[i] != 0) | |
321 | blks++; | |
322 | } | |
323 | slp->count = lastspclrec + blks + 1 - spcl.c_tapea; | |
324 | slp->tapea = spcl.c_tapea; | |
325 | slp->firstrec = lastfirstrec + ntrec; | |
326 | slp->inode = curino; | |
327 | nextblock = slp->tblock; | |
328 | trecno = 0; | |
329 | asize += tenths; | |
330 | blockswritten += ntrec; | |
331 | blocksthisvol += ntrec; | |
b45f51d6 | 332 | if (!pipeout && !unlimited && (blocksperfile ? |
1227625a SP |
333 | (blocksthisvol >= blocksperfile) : (asize > tsize))) { |
334 | close_rewind(); | |
335 | startnewtape(0); | |
336 | } | |
337 | timeest(); | |
338 | } | |
339 | ||
340 | void | |
341 | trewind() | |
342 | { | |
343 | int f; | |
344 | int got; | |
345 | ||
346 | for (f = 0; f < SLAVES; f++) { | |
347 | /* | |
b45f51d6 SP |
348 | * Drain the results, but unlike EOT we DO (or should) care |
349 | * what the return values were, since if we detect EOT after | |
350 | * we think we've written the last blocks to the tape anyway, | |
1227625a SP |
351 | * we have to replay those blocks with rollforward. |
352 | * | |
b45f51d6 | 353 | * fixme: punt for now. |
1227625a SP |
354 | */ |
355 | if (slaves[f].sent) { | |
356 | if (atomic(read, slaves[f].fd, (char *)&got, sizeof got) | |
357 | != sizeof got) { | |
358 | perror(" DUMP: error reading command pipe in master"); | |
359 | dumpabort(0); | |
360 | } | |
361 | slaves[f].sent = 0; | |
362 | if (got != writesize) { | |
363 | msg("EOT detected in last 2 tape records!\n"); | |
364 | msg("Use a longer tape, decrease the size estimate\n"); | |
365 | quit("or use no size estimate at all.\n"); | |
366 | } | |
367 | } | |
368 | (void) close(slaves[f].fd); | |
369 | } | |
370 | while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ | |
371 | /* void */; | |
372 | ||
373 | if (pipeout) | |
374 | return; | |
375 | ||
376 | msg("Closing %s\n", tape); | |
377 | ||
378 | #ifdef RDUMP | |
379 | if (host) { | |
380 | rmtclose(); | |
381 | while (rmtopen(tape, 0) < 0) | |
382 | sleep(10); | |
383 | rmtclose(); | |
384 | return; | |
385 | } | |
386 | #endif | |
387 | (void) close(tapefd); | |
388 | while ((f = open(tape, 0)) < 0) | |
389 | sleep (10); | |
390 | (void) close(f); | |
391 | } | |
392 | ||
393 | void | |
394 | close_rewind() | |
395 | { | |
b45f51d6 SP |
396 | time_t tstart_changevol, tend_changevol; |
397 | ||
1227625a SP |
398 | trewind(); |
399 | if (nexttape) | |
400 | return; | |
b45f51d6 SP |
401 | #ifdef __linux__ |
402 | (void)time4(&(tstart_changevol)); | |
403 | #else | |
404 | (void)time((time_t *)&(tstart_changevol)); | |
405 | #endif | |
1227625a SP |
406 | if (!nogripe) { |
407 | msg("Change Volumes: Mount volume #%d\n", tapeno+1); | |
408 | broadcast("CHANGE DUMP VOLUMES!\7\7\n"); | |
409 | } | |
410 | while (!query("Is the new volume mounted and ready to go?")) | |
411 | if (query("Do you want to abort?")) { | |
412 | dumpabort(0); | |
413 | /*NOTREACHED*/ | |
414 | } | |
b45f51d6 SP |
415 | #ifdef __linux__ |
416 | (void)time4(&(tend_changevol)); | |
417 | #else | |
418 | (void)time((time_t *)&(tend_changevol)); | |
419 | #endif | |
420 | if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1)) | |
421 | tstart_writing += (tend_changevol - tstart_changevol); | |
1227625a SP |
422 | } |
423 | ||
424 | void | |
425 | rollforward() | |
426 | { | |
427 | register struct req *p, *q, *prev; | |
428 | register struct slave *tslp; | |
429 | int i, size, savedtapea, got; | |
430 | union u_spcl *ntb, *otb; | |
b45f51d6 SP |
431 | #ifdef __linux__ |
432 | int blks; | |
433 | long lastfirstrec; | |
434 | #endif | |
1227625a SP |
435 | tslp = &slaves[SLAVES]; |
436 | ntb = (union u_spcl *)tslp->tblock[1]; | |
437 | ||
438 | /* | |
b45f51d6 SP |
439 | * Each of the N slaves should have requests that need to |
440 | * be replayed on the next tape. Use the extra slave buffers | |
441 | * (slaves[SLAVES]) to construct request lists to be sent to | |
1227625a SP |
442 | * each slave in turn. |
443 | */ | |
444 | for (i = 0; i < SLAVES; i++) { | |
445 | q = &tslp->req[1]; | |
446 | otb = (union u_spcl *)slp->tblock; | |
447 | ||
448 | /* | |
b45f51d6 | 449 | * For each request in the current slave, copy it to tslp. |
1227625a SP |
450 | */ |
451 | ||
452 | prev = NULL; | |
453 | for (p = slp->req; p->count > 0; p += p->count) { | |
454 | *q = *p; | |
455 | if (p->dblk == 0) | |
456 | *ntb++ = *otb++; /* copy the datablock also */ | |
457 | prev = q; | |
458 | q += q->count; | |
459 | } | |
460 | if (prev == NULL) | |
461 | quit("rollforward: protocol botch"); | |
462 | if (prev->dblk != 0) | |
463 | prev->count -= 1; | |
464 | else | |
465 | ntb--; | |
466 | q -= 1; | |
467 | q->count = 0; | |
468 | q = &tslp->req[0]; | |
469 | if (i == 0) { | |
470 | q->dblk = 0; | |
471 | q->count = 1; | |
472 | trecno = 0; | |
473 | nextblock = tslp->tblock; | |
474 | savedtapea = spcl.c_tapea; | |
475 | spcl.c_tapea = slp->tapea; | |
476 | startnewtape(0); | |
477 | spcl.c_tapea = savedtapea; | |
478 | lastspclrec = savedtapea - 1; | |
479 | } | |
480 | size = (char *)ntb - (char *)q; | |
481 | if (atomic(write, slp->fd, (char *)q, size) != size) { | |
482 | perror(" DUMP: error writing command pipe"); | |
483 | dumpabort(0); | |
484 | } | |
485 | slp->sent = 1; | |
b45f51d6 SP |
486 | #ifdef __linux__ |
487 | lastfirstrec = slp->firstrec; | |
488 | #endif | |
1227625a SP |
489 | if (++slp >= &slaves[SLAVES]) |
490 | slp = &slaves[0]; | |
491 | ||
492 | q->count = 1; | |
493 | ||
494 | if (prev->dblk != 0) { | |
495 | /* | |
b45f51d6 SP |
496 | * If the last one was a disk block, make the |
497 | * first of this one be the last bit of that disk | |
1227625a SP |
498 | * block... |
499 | */ | |
500 | q->dblk = prev->dblk + | |
501 | prev->count * (TP_BSIZE / DEV_BSIZE); | |
502 | ntb = (union u_spcl *)tslp->tblock; | |
503 | } else { | |
504 | /* | |
b45f51d6 | 505 | * It wasn't a disk block. Copy the data to its |
1227625a SP |
506 | * new location in the buffer. |
507 | */ | |
508 | q->dblk = 0; | |
509 | *((union u_spcl *)tslp->tblock) = *ntb; | |
510 | ntb = (union u_spcl *)tslp->tblock[1]; | |
511 | } | |
512 | } | |
513 | slp->req[0] = *q; | |
514 | nextblock = slp->tblock; | |
515 | if (q->dblk == 0) | |
b45f51d6 SP |
516 | #ifdef __linux__ |
517 | *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)tslp->tblock; | |
518 | #else | |
1227625a | 519 | nextblock++; |
b45f51d6 | 520 | #endif |
1227625a SP |
521 | trecno = 1; |
522 | ||
523 | /* | |
524 | * Clear the first slaves' response. One hopes that it | |
525 | * worked ok, otherwise the tape is much too short! | |
526 | */ | |
527 | if (slp->sent) { | |
528 | if (atomic(read, slp->fd, (char *)&got, sizeof got) | |
529 | != sizeof got) { | |
530 | perror(" DUMP: error reading command pipe in master"); | |
531 | dumpabort(0); | |
532 | } | |
533 | slp->sent = 0; | |
534 | ||
535 | if (got != writesize) { | |
536 | quit("EOT detected at start of the tape!\n"); | |
537 | } | |
538 | } | |
b45f51d6 SP |
539 | |
540 | #ifdef __linux__ | |
541 | blks = 0; | |
542 | if (spcl.c_type != TS_END) { | |
543 | for (i = 0; i < spcl.c_count; i++) | |
544 | if (spcl.c_addr[i] != 0) | |
545 | blks++; | |
546 | } | |
547 | ||
548 | slp->firstrec = lastfirstrec + ntrec; | |
549 | slp->count = lastspclrec + blks + 1 - spcl.c_tapea; | |
550 | slp->inode = curino; | |
551 | asize += tenths; | |
552 | blockswritten += ntrec; | |
553 | blocksthisvol += ntrec; | |
554 | #endif | |
1227625a SP |
555 | } |
556 | ||
557 | /* | |
558 | * We implement taking and restoring checkpoints on the tape level. | |
559 | * When each tape is opened, a new process is created by forking; this | |
560 | * saves all of the necessary context in the parent. The child | |
561 | * continues the dump; the parent waits around, saving the context. | |
562 | * If the child returns X_REWRITE, then it had problems writing that tape; | |
563 | * this causes the parent to fork again, duplicating the context, and | |
564 | * everything continues as if nothing had happened. | |
565 | */ | |
566 | void | |
567 | startnewtape(top) | |
568 | int top; | |
569 | { | |
570 | int parentpid; | |
571 | int childpid; | |
572 | int status; | |
573 | int waitpid; | |
574 | char *p; | |
575 | #ifdef __linux__ | |
576 | void (*interrupt_save)(); | |
577 | #else /* __linux__ */ | |
578 | #ifdef sunos | |
579 | void (*interrupt_save)(); | |
580 | #else | |
581 | sig_t interrupt_save; | |
582 | #endif | |
583 | #endif /* __linux__ */ | |
584 | ||
585 | interrupt_save = signal(SIGINT, SIG_IGN); | |
586 | parentpid = getpid(); | |
587 | ||
588 | restore_check_point: | |
589 | (void)signal(SIGINT, interrupt_save); | |
590 | /* | |
591 | * All signals are inherited... | |
592 | */ | |
593 | childpid = fork(); | |
594 | if (childpid < 0) { | |
595 | msg("Context save fork fails in parent %d\n", parentpid); | |
596 | Exit(X_ABORT); | |
597 | } | |
598 | if (childpid != 0) { | |
599 | /* | |
600 | * PARENT: | |
601 | * save the context by waiting | |
602 | * until the child doing all of the work returns. | |
603 | * don't catch the interrupt | |
604 | */ | |
605 | signal(SIGINT, SIG_IGN); | |
606 | #ifdef TDEBUG | |
607 | msg("Tape: %d; parent process: %d child process %d\n", | |
608 | tapeno+1, parentpid, childpid); | |
609 | #endif /* TDEBUG */ | |
610 | while ((waitpid = wait(&status)) != childpid) | |
611 | msg("Parent %d waiting for child %d has another child %d return\n", | |
612 | parentpid, childpid, waitpid); | |
613 | if (status & 0xFF) { | |
614 | msg("Child %d returns LOB status %o\n", | |
615 | childpid, status&0xFF); | |
616 | } | |
617 | status = (status >> 8) & 0xFF; | |
618 | #ifdef TDEBUG | |
619 | switch(status) { | |
620 | case X_FINOK: | |
621 | msg("Child %d finishes X_FINOK\n", childpid); | |
622 | break; | |
b45f51d6 | 623 | case X_ABORT: |
1227625a SP |
624 | msg("Child %d finishes X_ABORT\n", childpid); |
625 | break; | |
626 | case X_REWRITE: | |
627 | msg("Child %d finishes X_REWRITE\n", childpid); | |
628 | break; | |
629 | default: | |
630 | msg("Child %d finishes unknown %d\n", | |
631 | childpid, status); | |
632 | break; | |
633 | } | |
634 | #endif /* TDEBUG */ | |
635 | switch(status) { | |
636 | case X_FINOK: | |
637 | Exit(X_FINOK); | |
638 | case X_ABORT: | |
639 | Exit(X_ABORT); | |
640 | case X_REWRITE: | |
641 | goto restore_check_point; | |
642 | default: | |
643 | msg("Bad return code from dump: %d\n", status); | |
644 | Exit(X_ABORT); | |
645 | } | |
646 | /*NOTREACHED*/ | |
647 | } else { /* we are the child; just continue */ | |
648 | #ifdef TDEBUG | |
649 | sleep(4); /* allow time for parent's message to get out */ | |
650 | msg("Child on Tape %d has parent %d, my pid = %d\n", | |
651 | tapeno+1, parentpid, getpid()); | |
652 | #endif /* TDEBUG */ | |
653 | /* | |
654 | * If we have a name like "/dev/rmt0,/dev/rmt1", | |
655 | * use the name before the comma first, and save | |
656 | * the remaining names for subsequent volumes. | |
657 | */ | |
658 | tapeno++; /* current tape sequence */ | |
659 | if (nexttape || strchr(tape, ',')) { | |
660 | if (nexttape && *nexttape) | |
661 | tape = nexttape; | |
662 | if ((p = strchr(tape, ',')) != NULL) { | |
663 | *p = '\0'; | |
664 | nexttape = p + 1; | |
665 | } else | |
666 | nexttape = NULL; | |
667 | msg("Dumping volume %d on %s\n", tapeno, tape); | |
668 | } | |
669 | #ifdef RDUMP | |
670 | while ((tapefd = (host ? rmtopen(tape, 2) : | |
671 | pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0) | |
672 | #else | |
b45f51d6 | 673 | while ((tapefd = (pipeout ? 1 : |
1227625a SP |
674 | open(tape, O_WRONLY|O_CREAT, 0666))) < 0) |
675 | #endif | |
676 | { | |
677 | msg("Cannot open output \"%s\".\n", tape); | |
678 | if (!query("Do you want to retry the open?")) | |
679 | dumpabort(0); | |
680 | } | |
681 | ||
682 | enslave(); /* Share open tape file descriptor with slaves */ | |
683 | ||
684 | asize = 0; | |
685 | blocksthisvol = 0; | |
686 | if (top) | |
687 | newtape++; /* new tape signal */ | |
b45f51d6 | 688 | spcl.c_count = slp->count; |
1227625a SP |
689 | /* |
690 | * measure firstrec in TP_BSIZE units since restore doesn't | |
691 | * know the correct ntrec value... | |
692 | */ | |
693 | spcl.c_firstrec = slp->firstrec; | |
694 | spcl.c_volume++; | |
695 | spcl.c_type = TS_TAPE; | |
696 | spcl.c_flags |= DR_NEWHEADER; | |
697 | writeheader((ino_t)slp->inode); | |
698 | spcl.c_flags &=~ DR_NEWHEADER; | |
699 | if (tapeno > 1) | |
700 | msg("Volume %d begins with blocks from inode %d\n", | |
701 | tapeno, slp->inode); | |
702 | } | |
703 | } | |
704 | ||
705 | void | |
706 | dumpabort(signo) | |
707 | int signo; | |
708 | { | |
709 | ||
710 | if (master != 0 && master != getpid()) | |
711 | /* Signals master to call dumpabort */ | |
712 | (void) kill(master, SIGTERM); | |
713 | else { | |
714 | killall(); | |
715 | msg("The ENTIRE dump is aborted.\n"); | |
716 | } | |
717 | #ifdef RDUMP | |
718 | rmtclose(); | |
719 | #endif | |
720 | Exit(X_ABORT); | |
721 | } | |
722 | ||
b45f51d6 | 723 | void |
1227625a SP |
724 | Exit(status) |
725 | int status; | |
726 | { | |
727 | ||
728 | #ifdef TDEBUG | |
729 | msg("pid = %d exits with status %d\n", getpid(), status); | |
730 | #endif /* TDEBUG */ | |
731 | exit(status); | |
732 | } | |
733 | ||
734 | /* | |
735 | * proceed - handler for SIGUSR2, used to synchronize IO between the slaves. | |
736 | */ | |
737 | void | |
738 | proceed(signo) | |
739 | int signo; | |
740 | { | |
741 | ||
742 | if (ready) | |
743 | longjmp(jmpbuf, 1); | |
744 | caught++; | |
745 | } | |
746 | ||
747 | void | |
748 | enslave() | |
749 | { | |
750 | int cmd[2]; | |
751 | #ifdef LINUX_FORK_BUG | |
752 | int i, j; | |
753 | #else | |
754 | register int i, j; | |
755 | #endif | |
756 | ||
757 | master = getpid(); | |
758 | ||
759 | signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ | |
760 | signal(SIGPIPE, sigpipe); | |
761 | signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ | |
762 | signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */ | |
763 | ||
764 | for (i = 0; i < SLAVES; i++) { | |
765 | if (i == slp - &slaves[0]) { | |
766 | caught = 1; | |
767 | } else { | |
768 | caught = 0; | |
769 | } | |
770 | ||
771 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 || | |
772 | (slaves[i].pid = fork()) < 0) | |
773 | quit("too many slaves, %d (recompile smaller): %s\n", | |
774 | i, strerror(errno)); | |
775 | ||
776 | slaves[i].fd = cmd[1]; | |
777 | slaves[i].sent = 0; | |
778 | if (slaves[i].pid == 0) { /* Slave starts up here */ | |
779 | for (j = 0; j <= i; j++) | |
780 | (void) close(slaves[j].fd); | |
781 | signal(SIGINT, SIG_IGN); /* Master handles this */ | |
782 | #ifdef LINUX_FORK_BUG | |
783 | if (atomic(write, cmd[0], (char *) &i, sizeof i) | |
784 | != sizeof i) | |
785 | quit("master/slave protocol botched 3\n"); | |
786 | #endif | |
787 | doslave(cmd[0], i); | |
788 | Exit(X_FINOK); | |
789 | } | |
790 | } | |
791 | ||
792 | #ifdef LINUX_FORK_BUG | |
793 | /* | |
794 | * Wait for all slaves to _actually_ start to circumvent a bug in | |
795 | * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't | |
796 | * returned from fork() causes a SEGV in the child process | |
797 | */ | |
798 | for (i = 0; i < SLAVES; i++) | |
799 | if (atomic(read, slaves[i].fd, (char *) &j, sizeof j) != sizeof j) | |
800 | quit("master/slave protocol botched 4\n"); | |
801 | #endif | |
802 | ||
1227625a SP |
803 | for (i = 0; i < SLAVES; i++) |
804 | (void) atomic(write, slaves[i].fd, | |
805 | (char *) &slaves[(i + 1) % SLAVES].pid, | |
806 | sizeof slaves[0].pid); | |
807 | ||
808 | master = 0; | |
809 | } | |
810 | ||
811 | void | |
812 | killall() | |
813 | { | |
814 | register int i; | |
815 | ||
816 | for (i = 0; i < SLAVES; i++) | |
b45f51d6 | 817 | if (slaves[i].pid > 0) { |
1227625a | 818 | (void) kill(slaves[i].pid, SIGKILL); |
b45f51d6 SP |
819 | slaves[i].sent = 0; |
820 | } | |
1227625a SP |
821 | } |
822 | ||
823 | /* | |
824 | * Synchronization - each process has a lockfile, and shares file | |
825 | * descriptors to the following process's lockfile. When our write | |
826 | * completes, we release our lock on the following process's lock- | |
827 | * file, allowing the following process to lock it and proceed. We | |
828 | * get the lock back for the next cycle by swapping descriptors. | |
829 | */ | |
830 | static void | |
831 | doslave(cmd, slave_number) | |
832 | register int cmd; | |
833 | int slave_number; | |
834 | { | |
835 | register int nread; | |
836 | int nextslave, size, wrote, eot_count; | |
837 | #ifdef __linux__ | |
838 | errcode_t retval; | |
839 | #endif | |
840 | ||
1227625a SP |
841 | /* |
842 | * Need our own seek pointer. | |
843 | */ | |
844 | (void) close(diskfd); | |
845 | if ((diskfd = open(disk, O_RDONLY)) < 0) | |
846 | quit("slave couldn't reopen disk: %s\n", strerror(errno)); | |
847 | #ifdef __linux__ | |
848 | ext2fs_close(fs); | |
849 | retval = ext2fs_open(disk, 0, 0, 0, unix_io_manager, &fs); | |
850 | if (retval) | |
851 | quit("slave couldn't reopen disk: %s\n", strerror(errno)); | |
852 | #endif /* __linux__ */ | |
853 | ||
854 | /* | |
855 | * Need the pid of the next slave in the loop... | |
856 | */ | |
857 | if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave)) | |
858 | != sizeof nextslave) { | |
859 | quit("master/slave protocol botched - didn't get pid of next slave.\n"); | |
860 | } | |
861 | ||
862 | /* | |
863 | * Get list of blocks to dump, read the blocks into tape buffer | |
864 | */ | |
865 | while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) { | |
866 | register struct req *p = slp->req; | |
867 | ||
868 | for (trecno = 0; trecno < ntrec; | |
869 | trecno += p->count, p += p->count) { | |
870 | if (p->dblk) { | |
871 | bread(p->dblk, slp->tblock[trecno], | |
872 | p->count * TP_BSIZE); | |
873 | } else { | |
874 | if (p->count != 1 || atomic(read, cmd, | |
b45f51d6 | 875 | (char *)slp->tblock[trecno], |
1227625a SP |
876 | TP_BSIZE) != TP_BSIZE) |
877 | quit("master/slave protocol botched.\n"); | |
878 | } | |
879 | } | |
880 | if (setjmp(jmpbuf) == 0) { | |
881 | ready = 1; | |
882 | if (!caught) | |
883 | (void) pause(); | |
884 | } | |
885 | ready = 0; | |
886 | caught = 0; | |
887 | ||
888 | /* Try to write the data... */ | |
b45f51d6 | 889 | wrote = 0; |
1227625a SP |
890 | eot_count = 0; |
891 | size = 0; | |
892 | ||
893 | while (eot_count < 10 && size < writesize) { | |
894 | #ifdef RDUMP | |
895 | if (host) | |
896 | wrote = rmtwrite(slp->tblock[0]+size, | |
897 | writesize-size); | |
898 | else | |
899 | #endif | |
900 | wrote = write(tapefd, slp->tblock[0]+size, | |
901 | writesize-size); | |
902 | #ifdef WRITEDEBUG | |
b45f51d6 | 903 | printf("slave %d wrote %d\n", slave_number, wrote); |
1227625a | 904 | #endif |
b45f51d6 | 905 | if (wrote < 0) |
1227625a SP |
906 | break; |
907 | if (wrote == 0) | |
908 | eot_count++; | |
909 | size += wrote; | |
910 | } | |
911 | ||
912 | #ifdef WRITEDEBUG | |
b45f51d6 SP |
913 | if (size != writesize) |
914 | printf("slave %d only wrote %d out of %d bytes and gave up.\n", | |
1227625a SP |
915 | slave_number, size, writesize); |
916 | #endif | |
917 | ||
b45f51d6 SP |
918 | /* |
919 | * Handle ENOSPC as an EOT condition. | |
920 | */ | |
921 | if (wrote < 0 && errno == ENOSPC) { | |
922 | wrote = 0; | |
923 | eot_count++; | |
924 | } | |
925 | ||
1227625a SP |
926 | if (eot_count > 0) |
927 | size = 0; | |
928 | ||
b45f51d6 | 929 | if (wrote < 0) { |
1227625a SP |
930 | (void) kill(master, SIGUSR1); |
931 | for (;;) | |
932 | (void) sigpause(0); | |
933 | } else { | |
934 | /* | |
935 | * pass size of write back to master | |
936 | * (for EOT handling) | |
937 | */ | |
938 | (void) atomic(write, cmd, (char *)&size, sizeof size); | |
b45f51d6 | 939 | } |
1227625a SP |
940 | |
941 | /* | |
942 | * If partial write, don't want next slave to go. | |
943 | * Also jolts him awake. | |
944 | */ | |
945 | (void) kill(nextslave, SIGUSR2); | |
946 | } | |
947 | if (nread != 0) | |
948 | quit("error reading command pipe: %s\n", strerror(errno)); | |
949 | } | |
950 | ||
951 | /* | |
952 | * Since a read from a pipe may not return all we asked for, | |
953 | * or a write may not write all we ask if we get a signal, | |
954 | * loop until the count is satisfied (or error). | |
955 | */ | |
956 | static int | |
957 | atomic(func, fd, buf, count) | |
b45f51d6 SP |
958 | ssize_t (*func)(); |
959 | int fd; | |
1227625a SP |
960 | char *buf; |
961 | int count; | |
962 | { | |
963 | int got, need = count; | |
964 | ||
965 | while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) | |
966 | buf += got; | |
967 | return (got < 0 ? got : count - need); | |
968 | } |