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