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