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