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