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