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