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