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