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