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