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