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