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