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