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