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