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