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