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