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