]> git.wh0rd.org - dump.git/blob - dump/tape.c
Cleanups for new gcc/glibc.
[dump.git] / dump / tape.c
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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid[] =
44 "$Id: tape.c,v 1.43 2001/04/12 16:03:29 stelian Exp $";
45 #endif /* not lint */
46
47 #include <config.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <setjmp.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <compaterr.h>
54 #ifdef __STDC__
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #else
59 int write(), read();
60 #endif
61
62 #ifdef __linux__
63 #include <sys/types.h>
64 #include <time.h>
65 #include <linux/types.h>
66 #endif
67 #include <sys/param.h>
68 #include <sys/socket.h>
69 #include <sys/time.h>
70 #include <sys/wait.h>
71 #include <sys/mtio.h>
72 #ifdef __linux__
73 #include <linux/ext2_fs.h>
74 #include <ext2fs/ext2fs.h>
75 #include <bsdcompat.h>
76 #elif defined sunos
77 #include <sys/vnode.h>
78
79 #include <ufs/fs.h>
80 #include <ufs/inode.h>
81 #else
82 #include <ufs/ufs/dinode.h>
83 #include <ufs/ffs/fs.h>
84 #endif /* __linux__ */
85
86 #include <protocols/dumprestore.h>
87
88 #ifdef HAVE_ZLIB
89 #include <zlib.h>
90 #endif /* HAVE_ZLIB */
91
92 #include "dump.h"
93
94 int writesize; /* size of malloc()ed buffer for tape */
95 long lastspclrec = -1; /* tape block number of last written header */
96 int trecno = 0; /* next record to write in current block */
97 extern long blocksperfile; /* number of blocks per output file */
98 long blocksthisvol; /* number of blocks on current output file */
99 extern int ntrec; /* blocking factor on tape */
100 extern int cartridge;
101 extern char *host;
102 char *nexttape;
103 extern pid_t rshpid;
104 int eot_code = 1;
105 long long tapea_bytes = 0; /* bytes_written at start of current volume */
106
107 static ssize_t atomic_read __P((int, void *, size_t));
108 static ssize_t atomic_write __P((int, const void *, size_t));
109 static void doslave __P((int, int));
110 static void enslave __P((void));
111 static void flushtape __P((void));
112 static void killall __P((void));
113 static void rollforward __P((void));
114 static int system_command __P((const char *, const char *, int));
115
116 /*
117 * Concurrent dump mods (Caltech) - disk block reading and tape writing
118 * are exported to several slave processes. While one slave writes the
119 * tape, the others read disk blocks; they pass control of the tape in
120 * a ring via signals. The parent process traverses the filesystem and
121 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
122 * The following structure defines the instruction packets sent to slaves.
123 */
124 struct req {
125 daddr_t dblk;
126 int count;
127 };
128 int reqsiz;
129
130 struct slave_results {
131 ssize_t unclen; /* uncompressed length */
132 ssize_t clen; /* compressed length */
133 };
134
135 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
136 struct slave {
137 int tapea; /* header number at start of this chunk */
138 int count; /* count to next header (used for TS_TAPE */
139 /* after EOT) */
140 int inode; /* inode that we are currently dealing with */
141 int fd; /* FD for this slave */
142 int pid; /* PID for this slave */
143 int sent; /* 1 == we've sent this slave requests */
144 int firstrec; /* record number of this block */
145 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
146 struct req *req; /* buffer for requests */
147 } slaves[SLAVES+1];
148 struct slave *slp;
149
150 char (*nextblock)[TP_BSIZE];
151
152 static time_t tstart_volume; /* time of volume start */
153 static int tapea_volume; /* value of spcl.c_tapea at volume start */
154
155 int master; /* pid of master, for sending error signals */
156 int tenths; /* length of tape overhead per block written */
157 static int caught; /* have we caught the signal to proceed? */
158 static int ready; /* have we reached the lock point without having */
159 /* received the SIGUSR2 signal from the prev slave? */
160 static sigjmp_buf jmpbuf; /* where to jump to if we are ready when the */
161 /* SIGUSR2 arrives from the previous slave */
162 #ifdef USE_QFA
163 static int gtperr = 0;
164 #endif
165
166 int
167 alloctape(void)
168 {
169 int pgoff = getpagesize() - 1;
170 char *buf;
171 int i;
172
173 writesize = ntrec * TP_BSIZE;
174 reqsiz = (ntrec + 1) * sizeof(struct req);
175 /*
176 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
177 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
178 * repositioning after stopping, i.e, streaming mode, where the gap is
179 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
180 */
181 if (blocksperfile == 0 && !unlimited)
182 tenths = (cartridge ? 16 : density == 625 ? 5 : 8);
183 else {
184 tenths = 0;
185 density = 1;
186 }
187 /*
188 * Allocate tape buffer contiguous with the array of instruction
189 * packets, so flushtape() can write them together with one write().
190 * Align tape buffer on page boundary to speed up tape write().
191 */
192 for (i = 0; i <= SLAVES; i++) {
193 buf = (char *)
194 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
195 if (buf == NULL)
196 return(0);
197 slaves[i].tblock = (char (*)[TP_BSIZE])
198 #ifdef __linux__
199 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
200 #else
201 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
202 #endif
203 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
204 }
205 slp = &slaves[0];
206 slp->count = 1;
207 slp->tapea = 0;
208 slp->firstrec = 0;
209 nextblock = slp->tblock;
210 return(1);
211 }
212
213 void
214 writerec(const void *dp, int isspcl)
215 {
216
217 slp->req[trecno].dblk = (daddr_t)0;
218 slp->req[trecno].count = 1;
219 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
220 *(union u_spcl *)(*(nextblock)) = *(union u_spcl *)dp;
221 nextblock++;
222 if (isspcl)
223 lastspclrec = spcl.c_tapea;
224 trecno++;
225 spcl.c_tapea++;
226 if (trecno >= ntrec)
227 flushtape();
228 }
229
230 void
231 dumpblock(daddr_t blkno, int size)
232 {
233 int avail, tpblks, dblkno;
234
235 dblkno = fsbtodb(sblock, blkno);
236 tpblks = size >> tp_bshift;
237 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
238 slp->req[trecno].dblk = dblkno;
239 slp->req[trecno].count = avail;
240 trecno += avail;
241 spcl.c_tapea += avail;
242 if (trecno >= ntrec)
243 flushtape();
244 dblkno += avail << (tp_bshift - dev_bshift);
245 tpblks -= avail;
246 }
247 }
248
249 int nogripe = 0;
250
251 static void
252 tperror(int errnum)
253 {
254
255 if (pipeout) {
256 msg("write error on %s: %s\n", tape, strerror(errnum));
257 quit("Cannot recover\n");
258 /* NOTREACHED */
259 }
260 msg("write error %d blocks into volume %d: %s\n",
261 blocksthisvol, tapeno, strerror(errnum));
262 broadcast("DUMP WRITE ERROR!\n");
263 if (query("Do you want to rewrite this volume?")) {
264 msg("Closing this volume. Prepare to restart with new media;\n");
265 msg("this dump volume will be rewritten.\n");
266 killall();
267 nogripe = 1;
268 close_rewind();
269 Exit(X_REWRITE);
270 }
271 if (query("Do you want to start the next tape?"))
272 return;
273 dumpabort(0);
274 }
275
276 static void
277 sigpipe(int signo)
278 {
279
280 quit("Broken pipe\n");
281 }
282
283 /*
284 * do_stats --
285 * Update xferrate stats
286 */
287 time_t
288 do_stats(void)
289 {
290 time_t tnow, ttaken;
291 int blocks;
292
293 tnow = time(NULL);
294 ttaken = tnow - tstart_volume;
295 blocks = spcl.c_tapea - tapea_volume;
296 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
297 if (! compressed)
298 msg("Volume %d %ld tape blocks (%.2fMB)\n", tapeno,
299 blocks, ((double)blocks * TP_BSIZE / 1048576));
300 if (ttaken > 0) {
301 long volkb = (bytes_written - tapea_bytes) / 1024;
302 long txfrate = volkb / ttaken;
303 msg("Volume %d took %d:%02d:%02d\n", tapeno,
304 ttaken / 3600, (ttaken % 3600) / 60, ttaken % 60);
305 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
306 txfrate);
307 xferrate += txfrate;
308 if (compressed) {
309 double rate = .0005 + (double) blocks / (double) volkb;
310 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
311 " %1.3f:1\n",
312 tapeno, blocks, volkb, rate);
313 }
314 }
315 return(tnow);
316 }
317
318 char *
319 mktimeest(time_t tnow)
320 {
321 static char msgbuf[128];
322 time_t deltat;
323
324 msgbuf[0] = '\0';
325
326 if (blockswritten < 500)
327 return NULL;
328 if (blockswritten > tapesize)
329 tapesize = blockswritten;
330 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
331 / blockswritten * tapesize;
332 if (tnow > tstart_volume)
333 (void)snprintf(msgbuf, sizeof(msgbuf),
334 "%3.2f%% done at %ld kB/s, finished in %d:%02d\n",
335 (blockswritten * 100.0) / tapesize,
336 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume),
337 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
338 else
339 (void)snprintf(msgbuf, sizeof(msgbuf),
340 "%3.2f%% done, finished in %d:%02d\n",
341 (blockswritten * 100.0) / tapesize,
342 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
343
344 return msgbuf;
345 }
346
347 #if defined(SIGINFO)
348 /*
349 * statussig --
350 * information message upon receipt of SIGINFO
351 */
352 void
353 statussig(int notused)
354 {
355 int save_errno = errno;
356 char *buf;
357
358 buf = mktimeest(time(NULL));
359 if (buf)
360 write(STDERR_FILENO, buf, strlen(buf));
361 errno = save_errno;
362 }
363 #endif
364
365 static void
366 flushtape(void)
367 {
368 int i, blks, got;
369 long lastfirstrec;
370 struct slave_results returned;
371
372 int siz = (char *)nextblock - (char *)slp->req;
373
374 slp->req[trecno].count = 0; /* Sentinel */
375
376 if (atomic_write( slp->fd, (char *)slp->req, siz) != siz)
377 quit("error writing command pipe: %s\n", strerror(errno));
378 slp->sent = 1; /* we sent a request, read the response later */
379
380 lastfirstrec = slp->firstrec;
381
382 if (++slp >= &slaves[SLAVES])
383 slp = &slaves[0];
384
385 /* Read results back from next slave */
386 if (slp->sent) {
387 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
388 != sizeof returned) {
389 perror(" DUMP: error reading command pipe in master");
390 dumpabort(0);
391 }
392 got = returned.unclen;
393 bytes_written += returned.clen;
394 if (returned.unclen == returned.clen)
395 uncomprblks++;
396 slp->sent = 0;
397
398 /* Check for errors or end of tape */
399 if (got <= 0) {
400 /* Check for errors */
401 if (got < 0)
402 tperror(-got);
403 else
404 msg("End of tape detected\n");
405
406 /*
407 * Drain the results, don't care what the values were.
408 * If we read them here then trewind won't...
409 */
410 for (i = 0; i < SLAVES; i++) {
411 if (slaves[i].sent) {
412 if (atomic_read( slaves[i].fd,
413 (char *)&returned, sizeof returned)
414 != sizeof returned) {
415 perror(" DUMP: error reading command pipe in master");
416 dumpabort(0);
417 }
418 slaves[i].sent = 0;
419 }
420 }
421
422 close_rewind();
423 rollforward();
424 return;
425 }
426 }
427
428 blks = 0;
429 if (spcl.c_type != TS_END) {
430 for (i = 0; i < spcl.c_count; i++)
431 if (spcl.c_addr[i] != 0)
432 blks++;
433 }
434 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
435 slp->tapea = spcl.c_tapea;
436 slp->firstrec = lastfirstrec + ntrec;
437 slp->inode = curino;
438 nextblock = slp->tblock;
439 trecno = 0;
440 asize += tenths + returned.clen / density;
441 blockswritten += ntrec;
442 blocksthisvol += ntrec;
443 if (!pipeout && !unlimited && (blocksperfile ?
444 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
445 close_rewind();
446 startnewtape(0);
447 }
448 timeest();
449 }
450
451 /*
452 * Executes the command in a shell.
453 * Returns -1 if an error occured, the exit status of
454 * the command on success.
455 */
456 int system_command(const char *command, const char *device, int volnum) {
457 int pid, status;
458 char commandstr[4096];
459
460 pid = fork();
461 if (pid == -1) {
462 perror(" DUMP: unable to fork");
463 return -1;
464 }
465 if (pid == 0) {
466 setuid(getuid());
467 setgid(getgid());
468 #if OLD_STYLE_FSCRIPT
469 snprintf(commandstr, sizeof(commandstr), "%s", command);
470 #else
471 snprintf(commandstr, sizeof(commandstr), "%s %s %d", command, device, volnum);
472 #endif
473 commandstr[sizeof(commandstr) - 1] = '\0';
474 execl("/bin/sh", "sh", "-c", commandstr, NULL);
475 perror(" DUMP: unable to execute shell");
476 exit(-1);
477 }
478 do {
479 if (waitpid(pid, &status, 0) == -1) {
480 if (errno != EINTR) {
481 perror(" DUMP: waitpid error");
482 return -1;
483 }
484 } else {
485 if (WIFEXITED(status))
486 return WEXITSTATUS(status);
487 else
488 return -1;
489 }
490 } while(1);
491 }
492
493 time_t
494 trewind(void)
495 {
496 int f;
497 int got;
498 struct slave_results returned;
499
500 for (f = 0; f < SLAVES; f++) {
501 /*
502 * Drain the results, but unlike EOT we DO (or should) care
503 * what the return values were, since if we detect EOT after
504 * we think we've written the last blocks to the tape anyway,
505 * we have to replay those blocks with rollforward.
506 *
507 * fixme: punt for now.
508 */
509 if (slaves[f].sent) {
510 if (atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
511 != sizeof returned) {
512 perror(" DUMP: error reading command pipe in master");
513 dumpabort(0);
514 }
515 got = returned.unclen;
516 bytes_written += returned.clen;
517 if (returned.unclen == returned.clen)
518 uncomprblks++;
519 slaves[f].sent = 0;
520
521 if (got < 0)
522 tperror(-got);
523
524 if (got == 0) {
525 msg("EOT detected in last 2 tape records!\n");
526 msg("Use a longer tape, decrease the size estimate\n");
527 quit("or use no size estimate at all.\n");
528 }
529 }
530 (void) close(slaves[f].fd);
531 }
532 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
533 /* void */;
534
535 if (!pipeout) {
536
537 msg("Closing %s\n", tape);
538
539 #ifdef RDUMP
540 if (host) {
541 rmtclose();
542 while (rmtopen(tape, 0) < 0)
543 sleep(10);
544 rmtclose();
545 }
546 else
547 #endif
548 {
549 (void) close(tapefd);
550 while ((f = open(tape, 0)) < 0)
551 sleep (10);
552 (void) close(f);
553 }
554 eot_code = 1;
555 if (eot_script && spcl.c_type != TS_END) {
556 msg("Launching %s\n", eot_script);
557 eot_code = system_command(eot_script, tape, tapeno);
558 }
559 if (eot_code != 0 && eot_code != 1) {
560 msg("Dump aborted by the end of tape script\n");
561 dumpabort(0);
562 }
563 }
564 return do_stats();
565 }
566
567
568 void
569 close_rewind(void)
570 {
571 (void)trewind();
572 if (nexttape || Mflag || (eot_code == 0) )
573 return;
574 if (!nogripe) {
575 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
576 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
577 }
578 while (!query("Is the new volume mounted and ready to go?"))
579 if (query("Do you want to abort?")) {
580 dumpabort(0);
581 /*NOTREACHED*/
582 }
583 }
584
585 void
586 rollforward(void)
587 {
588 register struct req *p, *q, *prev;
589 register struct slave *tslp;
590 int i, size, savedtapea, got;
591 union u_spcl *ntb, *otb;
592 struct slave_results returned;
593 #ifdef __linux__
594 int blks;
595 long lastfirstrec;
596 #endif
597 tslp = &slaves[SLAVES];
598 ntb = (union u_spcl *)tslp->tblock[1];
599
600 /*
601 * Each of the N slaves should have requests that need to
602 * be replayed on the next tape. Use the extra slave buffers
603 * (slaves[SLAVES]) to construct request lists to be sent to
604 * each slave in turn.
605 */
606 for (i = 0; i < SLAVES; i++) {
607 q = &tslp->req[1];
608 otb = (union u_spcl *)slp->tblock;
609
610 /*
611 * For each request in the current slave, copy it to tslp.
612 */
613
614 prev = NULL;
615 for (p = slp->req; p->count > 0; p += p->count) {
616 *q = *p;
617 if (p->dblk == 0)
618 *ntb++ = *otb++; /* copy the datablock also */
619 prev = q;
620 q += q->count;
621 }
622 if (prev == NULL)
623 quit("rollforward: protocol botch");
624 if (prev->dblk != 0)
625 prev->count -= 1;
626 else
627 ntb--;
628 q -= 1;
629 q->count = 0;
630 q = &tslp->req[0];
631 if (i == 0) {
632 q->dblk = 0;
633 q->count = 1;
634 trecno = 0;
635 nextblock = tslp->tblock;
636 savedtapea = spcl.c_tapea;
637 spcl.c_tapea = slp->tapea;
638 startnewtape(0);
639 spcl.c_tapea = savedtapea;
640 lastspclrec = savedtapea - 1;
641 }
642 size = (char *)ntb - (char *)q;
643 if (atomic_write( slp->fd, (char *)q, size) != size) {
644 perror(" DUMP: error writing command pipe");
645 dumpabort(0);
646 }
647 slp->sent = 1;
648 #ifdef __linux__
649 lastfirstrec = slp->firstrec;
650 #endif
651 if (++slp >= &slaves[SLAVES])
652 slp = &slaves[0];
653
654 q->count = 1;
655
656 if (prev->dblk != 0) {
657 /*
658 * If the last one was a disk block, make the
659 * first of this one be the last bit of that disk
660 * block...
661 */
662 q->dblk = prev->dblk +
663 prev->count * (TP_BSIZE / DEV_BSIZE);
664 ntb = (union u_spcl *)tslp->tblock;
665 } else {
666 /*
667 * It wasn't a disk block. Copy the data to its
668 * new location in the buffer.
669 */
670 q->dblk = 0;
671 *((union u_spcl *)tslp->tblock) = *ntb;
672 ntb = (union u_spcl *)tslp->tblock[1];
673 }
674 }
675 slp->req[0] = *q;
676 nextblock = slp->tblock;
677 if (q->dblk == 0) {
678 #ifdef __linux__
679 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
680 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
681 #endif
682 nextblock++;
683 }
684 trecno = 1;
685
686 /*
687 * Clear the first slaves' response. One hopes that it
688 * worked ok, otherwise the tape is much too short!
689 */
690 if (slp->sent) {
691 if (atomic_read( slp->fd, (char *)&returned, sizeof returned)
692 != sizeof returned) {
693 perror(" DUMP: error reading command pipe in master");
694 dumpabort(0);
695 }
696 got = returned.unclen;
697 bytes_written += returned.clen;
698 if (returned.clen == returned.unclen)
699 uncomprblks++;
700 slp->sent = 0;
701
702 if (got < 0)
703 tperror(-got);
704
705 if (got == 0) {
706 quit("EOT detected at start of the tape!\n");
707 }
708 }
709
710 #ifdef __linux__
711 blks = 0;
712 if (spcl.c_type != TS_END) {
713 for (i = 0; i < spcl.c_count; i++)
714 if (spcl.c_addr[i] != 0)
715 blks++;
716 }
717
718 slp->firstrec = lastfirstrec + ntrec;
719 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
720 slp->inode = curino;
721 asize += tenths + returned.clen / density;
722 blockswritten += ntrec;
723 blocksthisvol += ntrec;
724 #endif
725 }
726
727 /*
728 * We implement taking and restoring checkpoints on the tape level.
729 * When each tape is opened, a new process is created by forking; this
730 * saves all of the necessary context in the parent. The child
731 * continues the dump; the parent waits around, saving the context.
732 * If the child returns X_REWRITE, then it had problems writing that tape;
733 * this causes the parent to fork again, duplicating the context, and
734 * everything continues as if nothing had happened.
735 */
736 void
737 startnewtape(int top)
738 {
739 int parentpid;
740 int childpid;
741 int status;
742 int waitpid;
743 char *p;
744
745 #ifdef __linux__
746 sigset_t sigs;
747 sigemptyset(&sigs);
748 sigaddset(&sigs, SIGINT);
749 sigprocmask(SIG_BLOCK, &sigs, NULL);
750 #else /* __linux__ */
751 #ifdef sunos
752 void (*interrupt_save)();
753 #else
754 sig_t interrupt_save;
755 #endif
756 interrupt_save = signal(SIGINT, SIG_IGN);
757 #endif /* __linux__ */
758
759 parentpid = getpid();
760 tapea_volume = spcl.c_tapea;
761 tapea_bytes = bytes_written;
762 tstart_volume = time(NULL);
763
764 restore_check_point:
765 #ifdef __linux__
766 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
767 #else
768 (void)signal(SIGINT, interrupt_save);
769 #endif
770 /*
771 * All signals are inherited...
772 */
773 childpid = fork();
774 if (childpid < 0) {
775 msg("Context save fork fails in parent %d\n", parentpid);
776 Exit(X_ABORT);
777 }
778 if (childpid != 0) {
779 /*
780 * PARENT:
781 * save the context by waiting
782 * until the child doing all of the work returns.
783 * don't catch the interrupt
784 */
785 #ifdef __linux__
786 sigprocmask(SIG_BLOCK, &sigs, NULL);
787 #else
788 signal(SIGINT, SIG_IGN);
789 #endif
790 #ifdef TDEBUG
791 msg("Tape: %d; parent process: %d child process %d\n",
792 tapeno+1, parentpid, childpid);
793 #endif /* TDEBUG */
794 while ((waitpid = wait(&status)) != childpid)
795 if (waitpid != rshpid)
796 msg("Parent %d waiting for child %d has another child %d return\n",
797 parentpid, childpid, waitpid);
798 if (status & 0xFF) {
799 msg("Child %d returns LOB status %o\n",
800 childpid, status&0xFF);
801 }
802 status = (status >> 8) & 0xFF;
803 #ifdef TDEBUG
804 switch(status) {
805 case X_FINOK:
806 msg("Child %d finishes X_FINOK\n", childpid);
807 break;
808 case X_ABORT:
809 msg("Child %d finishes X_ABORT\n", childpid);
810 break;
811 case X_REWRITE:
812 msg("Child %d finishes X_REWRITE\n", childpid);
813 break;
814 default:
815 msg("Child %d finishes unknown %d\n",
816 childpid, status);
817 break;
818 }
819 #endif /* TDEBUG */
820 switch(status) {
821 case X_FINOK:
822 Exit(X_FINOK);
823 case X_ABORT:
824 Exit(X_ABORT);
825 case X_REWRITE:
826 goto restore_check_point;
827 default:
828 msg("Bad return code from dump: %d\n", status);
829 Exit(X_ABORT);
830 }
831 /*NOTREACHED*/
832 } else { /* we are the child; just continue */
833 #ifdef TDEBUG
834 sleep(4); /* allow time for parent's message to get out */
835 msg("Child on Tape %d has parent %d, my pid = %d\n",
836 tapeno+1, parentpid, getpid());
837 #endif /* TDEBUG */
838 /*
839 * If we have a name like "/dev/rmt0,/dev/rmt1",
840 * use the name before the comma first, and save
841 * the remaining names for subsequent volumes.
842 */
843 tapeno++; /* current tape sequence */
844 if (Mflag) {
845 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
846 tape[MAXPATHLEN - 1] = '\0';
847 msg("Dumping volume %d on %s\n", tapeno, tape);
848 }
849 else if (nexttape || strchr(tapeprefix, ',')) {
850 if (nexttape && *nexttape)
851 tapeprefix = nexttape;
852 if ((p = strchr(tapeprefix, ',')) != NULL) {
853 *p = '\0';
854 nexttape = p + 1;
855 } else
856 nexttape = NULL;
857 strncpy(tape, tapeprefix, MAXPATHLEN);
858 tape[MAXPATHLEN - 1] = '\0';
859 msg("Dumping volume %d on %s\n", tapeno, tape);
860 }
861 #ifdef RDUMP
862 while ((tapefd = (host ? rmtopen(tape, 2) : pipeout ?
863 fileno(stdout) :
864 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
865 #else
866 while ((tapefd = (pipeout ? fileno(stdout) :
867 open(tape, O_RDWR|O_CREAT, 0666))) < 0)
868 #endif
869 {
870 msg("Cannot open output \"%s\".\n", tape);
871 if (!query("Do you want to retry the open?"))
872 dumpabort(0);
873 }
874
875 enslave(); /* Share open tape file descriptor with slaves */
876
877 asize = 0;
878 blocksthisvol = 0;
879 if (top)
880 newtape++; /* new tape signal */
881 spcl.c_count = slp->count;
882 /*
883 * measure firstrec in TP_BSIZE units since restore doesn't
884 * know the correct ntrec value...
885 */
886 spcl.c_firstrec = slp->firstrec;
887 spcl.c_volume++;
888 spcl.c_type = TS_TAPE;
889 spcl.c_flags |= DR_NEWHEADER;
890 if (compressed)
891 spcl.c_flags |= DR_COMPRESSED;
892 writeheader((dump_ino_t)slp->inode);
893 spcl.c_flags &=~ DR_NEWHEADER;
894 msg("Volume %d started with block %ld at: %s", tapeno,
895 spcl.c_tapea, ctime(&tstart_volume));
896 if (tapeno > 1)
897 msg("Volume %d begins with blocks from inode %d\n",
898 tapeno, slp->inode);
899 }
900 }
901
902 void
903 dumpabort(int signo)
904 {
905
906 if (master != 0 && master != getpid())
907 /* Signals master to call dumpabort */
908 (void) kill(master, SIGTERM);
909 else {
910 killall();
911 msg("The ENTIRE dump is aborted.\n");
912 }
913 #ifdef RDUMP
914 rmtclose();
915 #endif
916 Exit(X_ABORT);
917 }
918
919 void
920 Exit(int status)
921 {
922
923 #ifdef TDEBUG
924 msg("pid = %d exits with status %d\n", getpid(), status);
925 #endif /* TDEBUG */
926 exit(status);
927 }
928
929 /*
930 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
931 */
932 static void
933 proceed(int signo)
934 {
935 if (ready)
936 siglongjmp(jmpbuf, 1);
937 caught++;
938 }
939
940 void
941 enslave(void)
942 {
943 int cmd[2];
944 #ifdef LINUX_FORK_BUG
945 int i, j;
946 #else
947 register int i, j;
948 #endif
949
950 master = getpid();
951
952 { struct sigaction sa;
953 memset(&sa, 0, sizeof sa);
954 sigemptyset(&sa.sa_mask);
955 sa.sa_handler = dumpabort;
956 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
957 sa.sa_handler = sigpipe;
958 sigaction(SIGPIPE, &sa, NULL);
959 sa.sa_handler = proceed;
960 sa.sa_flags = SA_RESTART;
961 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
962 }
963
964 for (i = 0; i < SLAVES; i++) {
965 if (i == slp - &slaves[0]) {
966 caught = 1;
967 } else {
968 caught = 0;
969 }
970
971 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
972 (slaves[i].pid = fork()) < 0)
973 quit("too many slaves, %d (recompile smaller): %s\n",
974 i, strerror(errno));
975
976 slaves[i].fd = cmd[1];
977 slaves[i].sent = 0;
978 if (slaves[i].pid == 0) { /* Slave starts up here */
979 sigset_t sigs;
980 for (j = 0; j <= i; j++)
981 (void) close(slaves[j].fd);
982 sigemptyset(&sigs);
983 sigaddset(&sigs, SIGINT); /* Master handles this */
984 #if defined(SIGINFO)
985 sigaddset(&sigs, SIGINFO);
986 #endif
987 sigprocmask(SIG_BLOCK, &sigs, NULL);
988
989 #ifdef LINUX_FORK_BUG
990 if (atomic_write( cmd[0], (char *) &i, sizeof i)
991 != sizeof i)
992 quit("master/slave protocol botched 3\n");
993 #endif
994 doslave(cmd[0], i);
995 Exit(X_FINOK);
996 }
997 else
998 close(cmd[0]);
999 }
1000
1001 #ifdef LINUX_FORK_BUG
1002 /*
1003 * Wait for all slaves to _actually_ start to circumvent a bug in
1004 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
1005 * returned from fork() causes a SEGV in the child process
1006 */
1007 for (i = 0; i < SLAVES; i++)
1008 if (atomic_read( slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
1009 quit("master/slave protocol botched 4\n");
1010 #endif
1011
1012 for (i = 0; i < SLAVES; i++)
1013 (void) atomic_write( slaves[i].fd,
1014 (char *) &slaves[(i + 1) % SLAVES].pid,
1015 sizeof slaves[0].pid);
1016
1017 master = 0;
1018 }
1019
1020 void
1021 killall(void)
1022 {
1023 register int i;
1024
1025 for (i = 0; i < SLAVES; i++)
1026 if (slaves[i].pid > 0) {
1027 (void) kill(slaves[i].pid, SIGKILL);
1028 slaves[i].sent = 0;
1029 }
1030 }
1031
1032 /*
1033 * Synchronization - each process waits for a SIGUSR2 from the
1034 * previous process before writing to the tape, and sends SIGUSR2
1035 * to the next process when the tape write completes. On tape errors
1036 * a SIGUSR1 is sent to the master which then terminates all of the
1037 * slaves.
1038 */
1039 static void
1040 doslave(int cmd, int slave_number)
1041 {
1042 register int nread;
1043 int nextslave, size, eot_count, bufsize;
1044 volatile int wrote = 0;
1045 char *buffer;
1046 #ifdef HAVE_ZLIB
1047 struct tapebuf *comp_buf = NULL;
1048 int compresult, do_compress = 0;
1049 unsigned long worklen;
1050 #endif /* HAVE_ZLIB */
1051 struct slave_results returns;
1052 #ifdef __linux__
1053 errcode_t retval;
1054 #endif
1055 #ifdef USE_QFA
1056 long curtapepos;
1057 union u_spcl *uspclptr;
1058 struct s_spcl *spclptr;
1059 #endif /* USA_QFA */
1060
1061 /*
1062 * Need our own seek pointer.
1063 */
1064 (void) close(diskfd);
1065 if ((diskfd = open(disk, O_RDONLY)) < 0)
1066 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1067 #ifdef __linux__
1068 ext2fs_close(fs);
1069 retval = dump_fs_open(disk, &fs);
1070 if (retval)
1071 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1072 #endif /* __linux__ */
1073
1074 /*
1075 * Need the pid of the next slave in the loop...
1076 */
1077 if ((nread = atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1078 != sizeof nextslave) {
1079 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1080 }
1081
1082 #ifdef HAVE_ZLIB
1083 /* if we're doing a compressed dump, allocate the compress buffer */
1084 if (compressed) {
1085 comp_buf = malloc(sizeof(struct tapebuf) + TP_BSIZE + writesize);
1086 if (comp_buf == NULL)
1087 quit("couldn't allocate a compress buffer.\n");
1088 comp_buf->flags = 0;
1089 }
1090 #endif /* HAVE_ZLIB */
1091
1092 /*
1093 * Get list of blocks to dump, read the blocks into tape buffer
1094 */
1095 while ((nread = atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1096 register struct req *p = slp->req;
1097
1098 for (trecno = 0; trecno < ntrec;
1099 trecno += p->count, p += p->count) {
1100 if (p->dblk) { /* read a disk block */
1101 bread(p->dblk, slp->tblock[trecno],
1102 p->count * TP_BSIZE);
1103 } else { /* read record from pipe */
1104 if (p->count != 1 || atomic_read( cmd,
1105 (char *)slp->tblock[trecno],
1106 TP_BSIZE) != TP_BSIZE)
1107 quit("master/slave protocol botched.\n");
1108 }
1109 }
1110
1111 #ifdef USE_QFA
1112 if (gTapeposfd >= 0) {
1113 uspclptr = (union u_spcl *)&slp->tblock[0];
1114 spclptr = &uspclptr->s_spcl;
1115 if ((spclptr->c_magic == NFS_MAGIC) &&
1116 (spclptr->c_type == TS_INODE)) {
1117 /* if an error occured previously don't
1118 * try again */
1119 if (gtperr == 0) {
1120 if ((gtperr = GetTapePos(&curtapepos)) == 0) {
1121 #ifdef DEBUG_QFA
1122 msg("inode %ld at tapepos %ld\n", spclptr->c_inumber, curtapepos);
1123 #endif
1124 sprintf(gTps, "%ld\t%d\t%ld\n", (unsigned long)spclptr->c_inumber, tapeno, curtapepos);
1125 if (write(gTapeposfd, gTps, strlen(gTps)) != strlen(gTps)) {
1126 quit("error writing tapepos file.\n");
1127 }
1128 }
1129 }
1130 }
1131 }
1132 #endif /* USE_QFA */
1133
1134 /* Try to write the data... */
1135 wrote = 0;
1136 eot_count = 0;
1137 size = 0;
1138 buffer = (char *) slp->tblock[0]; /* set write pointer */
1139 bufsize = writesize; /* length to write */
1140 returns.clen = returns.unclen = bufsize;
1141
1142 #ifdef HAVE_ZLIB
1143 /*
1144 * When writing a compressed dump, each block is
1145 * written from struct tapebuf with an 4 byte prefix
1146 * followed by the data. This can be less than
1147 * writesize. Restore, on a short read, can compare the
1148 * length read to the compressed length in the header
1149 * to verify that the read was good. Blocks which don't
1150 * compress well are written uncompressed.
1151 * The first block written by each slave is not compressed.
1152 */
1153
1154 if (compressed) {
1155 comp_buf->length = bufsize;
1156 worklen = TP_BSIZE + writesize;
1157 compresult = Z_DATA_ERROR;
1158 if (do_compress)
1159 compresult = compress2(comp_buf->buf, &worklen,
1160 (char *)slp->tblock[0], writesize, compressed);
1161 if (compresult == Z_OK && worklen <= writesize-32) {
1162 /* write the compressed buffer */
1163 comp_buf->length = worklen;
1164 comp_buf->compressed = 1;
1165 buffer = (char *) comp_buf;
1166 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1167 }
1168 else {
1169 /* write the data uncompressed */
1170 comp_buf->length = writesize;
1171 comp_buf->compressed = 0;
1172 buffer = (char *) comp_buf;
1173 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1174 returns.unclen = returns.clen;
1175 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1176 }
1177 }
1178 /* compress the remaining blocks */
1179 do_compress = compressed;
1180 #endif /* HAVE_ZLIB */
1181
1182 if (sigsetjmp(jmpbuf, 1) == 0) {
1183 ready = 1;
1184 if (!caught)
1185 (void) pause();
1186 }
1187 ready = 0;
1188 caught = 0;
1189
1190 while (eot_count < 10 && size < bufsize) {
1191 #ifdef RDUMP
1192 if (host)
1193 wrote = rmtwrite(buffer + size, bufsize - size);
1194 else
1195 #endif
1196 wrote = write(tapefd, buffer + size, bufsize - size);
1197 #ifdef WRITEDEBUG
1198 printf("slave %d wrote %d\n", slave_number, wrote);
1199 #endif
1200 if (wrote < 0)
1201 break;
1202 if (wrote == 0)
1203 eot_count++;
1204 size += wrote;
1205 }
1206
1207 #ifdef WRITEDEBUG
1208 if (size != bufsize)
1209 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
1210 slave_number, size, bufsize);
1211 #endif
1212
1213 /*
1214 * Handle ENOSPC as an EOT condition.
1215 */
1216 if (wrote < 0 && errno == ENOSPC) {
1217 wrote = 0;
1218 eot_count++;
1219 }
1220
1221 if (eot_count > 0)
1222 returns.clen = returns.unclen = 0;
1223
1224 /*
1225 * pass errno back to master for special handling
1226 */
1227 if (wrote < 0)
1228 returns.unclen = -errno;
1229
1230 /*
1231 * pass size of data and size of write back to master
1232 * (for EOT handling)
1233 */
1234 (void) atomic_write( cmd, (char *)&returns, sizeof returns);
1235
1236 /*
1237 * Signal the next slave to go.
1238 */
1239 (void) kill(nextslave, SIGUSR2);
1240 }
1241 if (nread != 0)
1242 quit("error reading command pipe: %s\n", strerror(errno));
1243 }
1244
1245 /*
1246 * Since a read from a pipe may not return all we asked for,
1247 * or a write may not write all we ask if we get a signal,
1248 * loop until the count is satisfied (or error).
1249 */
1250 static ssize_t
1251 atomic_read(int fd, void *buf, size_t count)
1252 {
1253 int got, need = count;
1254
1255 do {
1256 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1257 (char *)buf += got;
1258 } while (got == -1 && errno == EINTR);
1259 return (got < 0 ? got : count - need);
1260 }
1261
1262 /*
1263 * Since a read from a pipe may not return all we asked for,
1264 * or a write may not write all we ask if we get a signal,
1265 * loop until the count is satisfied (or error).
1266 */
1267 static ssize_t
1268 atomic_write(int fd, const void *buf, size_t count)
1269 {
1270 int got, need = count;
1271
1272 do {
1273 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1274 (char *)buf += got;
1275 } while (got == -1 && errno == EINTR);
1276 return (got < 0 ? got : count - need);
1277 }
1278
1279
1280 #ifdef USE_QFA
1281 /*
1282 * read the current tape position
1283 */
1284 int
1285 GetTapePos(long *pos)
1286 {
1287 int err = 0;
1288
1289 *pos = 0;
1290 if (ioctl(tapefd, MTIOCPOS, pos) == -1) {
1291 err = errno;
1292 msg("[%ld] error: %d (getting tapepos: %ld)\n", getpid(),
1293 err, *pos);
1294 return err;
1295 }
1296 return err;
1297 }
1298 #endif /* USE_QFA */