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