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