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