]> git.wh0rd.org - dump.git/blob - dump/tape.c
Initial revision
[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, 1995, 1996
5 *
6 */
7
8 /*-
9 * Copyright (c) 1980, 1991, 1993
10 * The Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/wait.h>
49 #ifdef __linux__
50 #include <linux/ext2_fs.h>
51 #include <bsdcompat.h>
52 #else /* __linux__ */
53 #ifdef sunos
54 #include <sys/vnode.h>
55
56 #include <ufs/fs.h>
57 #include <ufs/inode.h>
58 #else
59 #include <ufs/ufs/dinode.h>
60 #include <ufs/ffs/fs.h>
61 #endif
62 #endif /* __linux__ */
63
64 #include <protocols/dumprestore.h>
65
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <setjmp.h>
69 #include <signal.h>
70 #include <stdio.h>
71 #ifdef __STDC__
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #else
76 int write(), read();
77 #endif
78
79 #ifdef __linux__
80 #include <ext2fs/ext2fs.h>
81 #endif
82 #include "dump.h"
83 #include "pathnames.h"
84
85 int writesize; /* size of malloc()ed buffer for tape */
86 long lastspclrec = -1; /* tape block number of last written header */
87 int trecno = 0; /* next record to write in current block */
88 extern long blocksperfile; /* number of blocks per output file */
89 long blocksthisvol; /* number of blocks on current output file */
90 extern int ntrec; /* blocking factor on tape */
91 extern int cartridge;
92 extern char *host;
93 char *nexttape;
94
95 static int atomic __P((int (*)(), int, char *, int));
96 static void doslave __P((int, int));
97 static void enslave __P((void));
98 static void flushtape __P((void));
99 static void killall __P((void));
100 static void rollforward __P((void));
101
102 /*
103 * Concurrent dump mods (Caltech) - disk block reading and tape writing
104 * are exported to several slave processes. While one slave writes the
105 * tape, the others read disk blocks; they pass control of the tape in
106 * a ring via signals. The parent process traverses the filesystem and
107 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
108 * The following structure defines the instruction packets sent to slaves.
109 */
110 struct req {
111 daddr_t dblk;
112 int count;
113 };
114 int reqsiz;
115
116 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
117 struct slave {
118 int tapea; /* header number at start of this chunk */
119 int count; /* count to next header (used for TS_TAPE */
120 /* after EOT) */
121 int inode; /* inode that we are currently dealing with */
122 int fd; /* FD for this slave */
123 int pid; /* PID for this slave */
124 int sent; /* 1 == we've sent this slave requests */
125 int firstrec; /* record number of this block */
126 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
127 struct req *req; /* buffer for requests */
128 } slaves[SLAVES+1];
129 struct slave *slp;
130
131 char (*nextblock)[TP_BSIZE];
132
133 int master; /* pid of master, for sending error signals */
134 int tenths; /* length of tape used per block written */
135 static int caught; /* have we caught the signal to proceed? */
136 static int ready; /* have we reached the lock point without having */
137 /* received the SIGUSR2 signal from the prev slave? */
138 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
139 /* SIGUSR2 arrives from the previous slave */
140
141 int
142 alloctape()
143 {
144 int pgoff = getpagesize() - 1;
145 char *buf;
146 int i;
147
148 writesize = ntrec * TP_BSIZE;
149 reqsiz = (ntrec + 1) * sizeof(struct req);
150 /*
151 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
152 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
153 * repositioning after stopping, i.e, streaming mode, where the gap is
154 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
155 */
156 if (blocksperfile == 0)
157 tenths = writesize / density +
158 (cartridge ? 16 : density == 625 ? 5 : 8);
159 /*
160 * Allocate tape buffer contiguous with the array of instruction
161 * packets, so flushtape() can write them together with one write().
162 * Align tape buffer on page boundary to speed up tape write().
163 */
164 for (i = 0; i <= SLAVES; i++) {
165 buf = (char *)
166 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
167 if (buf == NULL)
168 return(0);
169 slaves[i].tblock = (char (*)[TP_BSIZE])
170 #ifdef __linux__
171 (((long)&buf[reqsiz] + pgoff) &~ pgoff);
172 #else
173 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
174 #endif
175 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
176 }
177 slp = &slaves[0];
178 slp->count = 1;
179 slp->tapea = 0;
180 slp->firstrec = 0;
181 nextblock = slp->tblock;
182 return(1);
183 }
184
185 void
186 writerec(dp, isspcl)
187 char *dp;
188 int isspcl;
189 {
190
191 slp->req[trecno].dblk = (daddr_t)0;
192 slp->req[trecno].count = 1;
193 *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
194 if (isspcl)
195 lastspclrec = spcl.c_tapea;
196 trecno++;
197 spcl.c_tapea++;
198 if (trecno >= ntrec)
199 flushtape();
200 }
201
202 void
203 dumpblock(blkno, size)
204 daddr_t blkno;
205 int size;
206 {
207 int avail, tpblks, dblkno;
208
209 dblkno = fsbtodb(sblock, blkno);
210 tpblks = size >> tp_bshift;
211 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
212 slp->req[trecno].dblk = dblkno;
213 slp->req[trecno].count = avail;
214 trecno += avail;
215 spcl.c_tapea += avail;
216 if (trecno >= ntrec)
217 flushtape();
218 dblkno += avail << (tp_bshift - dev_bshift);
219 tpblks -= avail;
220 }
221 }
222
223 int nogripe = 0;
224
225 void
226 tperror(signo)
227 int signo;
228 {
229
230 if (pipeout) {
231 msg("write error on %s\n", tape);
232 quit("Cannot recover\n");
233 /* NOTREACHED */
234 }
235 msg("write error %d blocks into volume %d\n", blocksthisvol, tapeno);
236 broadcast("DUMP WRITE ERROR!\n");
237 if (!query("Do you want to restart?"))
238 dumpabort(0);
239 msg("Closing this volume. Prepare to restart with new media;\n");
240 msg("this dump volume will be rewritten.\n");
241 killall();
242 nogripe = 1;
243 close_rewind();
244 Exit(X_REWRITE);
245 }
246
247 void
248 sigpipe(signo)
249 int signo;
250 {
251
252 quit("Broken pipe\n");
253 }
254
255 static void
256 flushtape()
257 {
258 int i, blks, got;
259 long lastfirstrec;
260
261 int siz = (char *)nextblock - (char *)slp->req;
262
263 slp->req[trecno].count = 0; /* Sentinel */
264
265 if (atomic(write, slp->fd, (char *)slp->req, siz) != siz)
266 quit("error writing command pipe: %s\n", strerror(errno));
267 slp->sent = 1; /* we sent a request, read the response later */
268
269 lastfirstrec = slp->firstrec;
270
271 if (++slp >= &slaves[SLAVES])
272 slp = &slaves[0];
273
274 /* Read results back from next slave */
275 if (slp->sent) {
276 if (atomic(read, slp->fd, (char *)&got, sizeof got)
277 != sizeof got) {
278 perror(" DUMP: error reading command pipe in master");
279 dumpabort(0);
280 }
281 slp->sent = 0;
282
283 /* Check for end of tape */
284 if (got < writesize) {
285 msg("End of tape detected\n");
286
287 /*
288 * Drain the results, don't care what the values were.
289 * If we read them here then trewind won't...
290 */
291 for (i = 0; i < SLAVES; i++) {
292 if (slaves[i].sent) {
293 if (atomic(read, slaves[i].fd,
294 (char *)&got, sizeof got)
295 != sizeof got) {
296 perror(" DUMP: error reading command pipe in master");
297 dumpabort(0);
298 }
299 slaves[i].sent = 0;
300 }
301 }
302
303 close_rewind();
304 rollforward();
305 return;
306 }
307 }
308
309 blks = 0;
310 if (spcl.c_type != TS_END) {
311 for (i = 0; i < spcl.c_count; i++)
312 if (spcl.c_addr[i] != 0)
313 blks++;
314 }
315 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
316 slp->tapea = spcl.c_tapea;
317 slp->firstrec = lastfirstrec + ntrec;
318 slp->inode = curino;
319 nextblock = slp->tblock;
320 trecno = 0;
321 asize += tenths;
322 blockswritten += ntrec;
323 blocksthisvol += ntrec;
324 if (!pipeout && (blocksperfile ?
325 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
326 close_rewind();
327 startnewtape(0);
328 }
329 timeest();
330 }
331
332 void
333 trewind()
334 {
335 int f;
336 int got;
337
338 for (f = 0; f < SLAVES; f++) {
339 /*
340 * Drain the results, but unlike EOT we DO (or should) care
341 * what the return values were, since if we detect EOT after
342 * we think we've written the last blocks to the tape anyway,
343 * we have to replay those blocks with rollforward.
344 *
345 * fixme: punt for now.
346 */
347 if (slaves[f].sent) {
348 if (atomic(read, slaves[f].fd, (char *)&got, sizeof got)
349 != sizeof got) {
350 perror(" DUMP: error reading command pipe in master");
351 dumpabort(0);
352 }
353 slaves[f].sent = 0;
354 if (got != writesize) {
355 msg("EOT detected in last 2 tape records!\n");
356 msg("Use a longer tape, decrease the size estimate\n");
357 quit("or use no size estimate at all.\n");
358 }
359 }
360 (void) close(slaves[f].fd);
361 }
362 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
363 /* void */;
364
365 if (pipeout)
366 return;
367
368 msg("Closing %s\n", tape);
369
370 #ifdef RDUMP
371 if (host) {
372 rmtclose();
373 while (rmtopen(tape, 0) < 0)
374 sleep(10);
375 rmtclose();
376 return;
377 }
378 #endif
379 (void) close(tapefd);
380 while ((f = open(tape, 0)) < 0)
381 sleep (10);
382 (void) close(f);
383 }
384
385 void
386 close_rewind()
387 {
388 trewind();
389 if (nexttape)
390 return;
391 if (!nogripe) {
392 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
393 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
394 }
395 while (!query("Is the new volume mounted and ready to go?"))
396 if (query("Do you want to abort?")) {
397 dumpabort(0);
398 /*NOTREACHED*/
399 }
400 }
401
402 void
403 rollforward()
404 {
405 register struct req *p, *q, *prev;
406 register struct slave *tslp;
407 int i, size, savedtapea, got;
408 union u_spcl *ntb, *otb;
409 tslp = &slaves[SLAVES];
410 ntb = (union u_spcl *)tslp->tblock[1];
411
412 /*
413 * Each of the N slaves should have requests that need to
414 * be replayed on the next tape. Use the extra slave buffers
415 * (slaves[SLAVES]) to construct request lists to be sent to
416 * each slave in turn.
417 */
418 for (i = 0; i < SLAVES; i++) {
419 q = &tslp->req[1];
420 otb = (union u_spcl *)slp->tblock;
421
422 /*
423 * For each request in the current slave, copy it to tslp.
424 */
425
426 prev = NULL;
427 for (p = slp->req; p->count > 0; p += p->count) {
428 *q = *p;
429 if (p->dblk == 0)
430 *ntb++ = *otb++; /* copy the datablock also */
431 prev = q;
432 q += q->count;
433 }
434 if (prev == NULL)
435 quit("rollforward: protocol botch");
436 if (prev->dblk != 0)
437 prev->count -= 1;
438 else
439 ntb--;
440 q -= 1;
441 q->count = 0;
442 q = &tslp->req[0];
443 if (i == 0) {
444 q->dblk = 0;
445 q->count = 1;
446 trecno = 0;
447 nextblock = tslp->tblock;
448 savedtapea = spcl.c_tapea;
449 spcl.c_tapea = slp->tapea;
450 startnewtape(0);
451 spcl.c_tapea = savedtapea;
452 lastspclrec = savedtapea - 1;
453 }
454 size = (char *)ntb - (char *)q;
455 if (atomic(write, slp->fd, (char *)q, size) != size) {
456 perror(" DUMP: error writing command pipe");
457 dumpabort(0);
458 }
459 slp->sent = 1;
460 if (++slp >= &slaves[SLAVES])
461 slp = &slaves[0];
462
463 q->count = 1;
464
465 if (prev->dblk != 0) {
466 /*
467 * If the last one was a disk block, make the
468 * first of this one be the last bit of that disk
469 * block...
470 */
471 q->dblk = prev->dblk +
472 prev->count * (TP_BSIZE / DEV_BSIZE);
473 ntb = (union u_spcl *)tslp->tblock;
474 } else {
475 /*
476 * It wasn't a disk block. Copy the data to its
477 * new location in the buffer.
478 */
479 q->dblk = 0;
480 *((union u_spcl *)tslp->tblock) = *ntb;
481 ntb = (union u_spcl *)tslp->tblock[1];
482 }
483 }
484 slp->req[0] = *q;
485 nextblock = slp->tblock;
486 if (q->dblk == 0)
487 nextblock++;
488 trecno = 1;
489
490 /*
491 * Clear the first slaves' response. One hopes that it
492 * worked ok, otherwise the tape is much too short!
493 */
494 if (slp->sent) {
495 if (atomic(read, slp->fd, (char *)&got, sizeof got)
496 != sizeof got) {
497 perror(" DUMP: error reading command pipe in master");
498 dumpabort(0);
499 }
500 slp->sent = 0;
501
502 if (got != writesize) {
503 quit("EOT detected at start of the tape!\n");
504 }
505 }
506 }
507
508 /*
509 * We implement taking and restoring checkpoints on the tape level.
510 * When each tape is opened, a new process is created by forking; this
511 * saves all of the necessary context in the parent. The child
512 * continues the dump; the parent waits around, saving the context.
513 * If the child returns X_REWRITE, then it had problems writing that tape;
514 * this causes the parent to fork again, duplicating the context, and
515 * everything continues as if nothing had happened.
516 */
517 void
518 startnewtape(top)
519 int top;
520 {
521 int parentpid;
522 int childpid;
523 int status;
524 int waitpid;
525 char *p;
526 #ifdef __linux__
527 void (*interrupt_save)();
528 #else /* __linux__ */
529 #ifdef sunos
530 void (*interrupt_save)();
531 #else
532 sig_t interrupt_save;
533 #endif
534 #endif /* __linux__ */
535
536 interrupt_save = signal(SIGINT, SIG_IGN);
537 parentpid = getpid();
538
539 restore_check_point:
540 (void)signal(SIGINT, interrupt_save);
541 /*
542 * All signals are inherited...
543 */
544 childpid = fork();
545 if (childpid < 0) {
546 msg("Context save fork fails in parent %d\n", parentpid);
547 Exit(X_ABORT);
548 }
549 if (childpid != 0) {
550 /*
551 * PARENT:
552 * save the context by waiting
553 * until the child doing all of the work returns.
554 * don't catch the interrupt
555 */
556 signal(SIGINT, SIG_IGN);
557 #ifdef TDEBUG
558 msg("Tape: %d; parent process: %d child process %d\n",
559 tapeno+1, parentpid, childpid);
560 #endif /* TDEBUG */
561 while ((waitpid = wait(&status)) != childpid)
562 msg("Parent %d waiting for child %d has another child %d return\n",
563 parentpid, childpid, waitpid);
564 if (status & 0xFF) {
565 msg("Child %d returns LOB status %o\n",
566 childpid, status&0xFF);
567 }
568 status = (status >> 8) & 0xFF;
569 #ifdef TDEBUG
570 switch(status) {
571 case X_FINOK:
572 msg("Child %d finishes X_FINOK\n", childpid);
573 break;
574 case X_ABORT:
575 msg("Child %d finishes X_ABORT\n", childpid);
576 break;
577 case X_REWRITE:
578 msg("Child %d finishes X_REWRITE\n", childpid);
579 break;
580 default:
581 msg("Child %d finishes unknown %d\n",
582 childpid, status);
583 break;
584 }
585 #endif /* TDEBUG */
586 switch(status) {
587 case X_FINOK:
588 Exit(X_FINOK);
589 case X_ABORT:
590 Exit(X_ABORT);
591 case X_REWRITE:
592 goto restore_check_point;
593 default:
594 msg("Bad return code from dump: %d\n", status);
595 Exit(X_ABORT);
596 }
597 /*NOTREACHED*/
598 } else { /* we are the child; just continue */
599 #ifdef TDEBUG
600 sleep(4); /* allow time for parent's message to get out */
601 msg("Child on Tape %d has parent %d, my pid = %d\n",
602 tapeno+1, parentpid, getpid());
603 #endif /* TDEBUG */
604 /*
605 * If we have a name like "/dev/rmt0,/dev/rmt1",
606 * use the name before the comma first, and save
607 * the remaining names for subsequent volumes.
608 */
609 tapeno++; /* current tape sequence */
610 if (nexttape || strchr(tape, ',')) {
611 if (nexttape && *nexttape)
612 tape = nexttape;
613 if ((p = strchr(tape, ',')) != NULL) {
614 *p = '\0';
615 nexttape = p + 1;
616 } else
617 nexttape = NULL;
618 msg("Dumping volume %d on %s\n", tapeno, tape);
619 }
620 #ifdef RDUMP
621 while ((tapefd = (host ? rmtopen(tape, 2) :
622 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
623 #else
624 while ((tapefd = (pipeout ? 1 :
625 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
626 #endif
627 {
628 msg("Cannot open output \"%s\".\n", tape);
629 if (!query("Do you want to retry the open?"))
630 dumpabort(0);
631 }
632
633 enslave(); /* Share open tape file descriptor with slaves */
634
635 asize = 0;
636 blocksthisvol = 0;
637 if (top)
638 newtape++; /* new tape signal */
639 spcl.c_count = slp->count;
640 /*
641 * measure firstrec in TP_BSIZE units since restore doesn't
642 * know the correct ntrec value...
643 */
644 spcl.c_firstrec = slp->firstrec;
645 spcl.c_volume++;
646 spcl.c_type = TS_TAPE;
647 spcl.c_flags |= DR_NEWHEADER;
648 writeheader((ino_t)slp->inode);
649 spcl.c_flags &=~ DR_NEWHEADER;
650 if (tapeno > 1)
651 msg("Volume %d begins with blocks from inode %d\n",
652 tapeno, slp->inode);
653 }
654 }
655
656 void
657 dumpabort(signo)
658 int signo;
659 {
660
661 if (master != 0 && master != getpid())
662 /* Signals master to call dumpabort */
663 (void) kill(master, SIGTERM);
664 else {
665 killall();
666 msg("The ENTIRE dump is aborted.\n");
667 }
668 #ifdef RDUMP
669 rmtclose();
670 #endif
671 Exit(X_ABORT);
672 }
673
674 __dead void
675 Exit(status)
676 int status;
677 {
678
679 #ifdef TDEBUG
680 msg("pid = %d exits with status %d\n", getpid(), status);
681 #endif /* TDEBUG */
682 exit(status);
683 }
684
685 /*
686 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
687 */
688 void
689 proceed(signo)
690 int signo;
691 {
692
693 if (ready)
694 longjmp(jmpbuf, 1);
695 caught++;
696 }
697
698 void
699 enslave()
700 {
701 int cmd[2];
702 #ifdef LINUX_FORK_BUG
703 int i, j;
704 #else
705 register int i, j;
706 #endif
707
708 master = getpid();
709
710 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
711 signal(SIGPIPE, sigpipe);
712 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
713 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
714
715 for (i = 0; i < SLAVES; i++) {
716 if (i == slp - &slaves[0]) {
717 caught = 1;
718 } else {
719 caught = 0;
720 }
721
722 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
723 (slaves[i].pid = fork()) < 0)
724 quit("too many slaves, %d (recompile smaller): %s\n",
725 i, strerror(errno));
726
727 slaves[i].fd = cmd[1];
728 slaves[i].sent = 0;
729 if (slaves[i].pid == 0) { /* Slave starts up here */
730 for (j = 0; j <= i; j++)
731 (void) close(slaves[j].fd);
732 signal(SIGINT, SIG_IGN); /* Master handles this */
733 #ifdef LINUX_FORK_BUG
734 if (atomic(write, cmd[0], (char *) &i, sizeof i)
735 != sizeof i)
736 quit("master/slave protocol botched 3\n");
737 #endif
738 doslave(cmd[0], i);
739 Exit(X_FINOK);
740 }
741 }
742
743 #ifdef LINUX_FORK_BUG
744 /*
745 * Wait for all slaves to _actually_ start to circumvent a bug in
746 * Linux kernels >= 2.1.3 where a signal sent to a child that hasn't
747 * returned from fork() causes a SEGV in the child process
748 */
749 for (i = 0; i < SLAVES; i++)
750 if (atomic(read, slaves[i].fd, (char *) &j, sizeof j) != sizeof j)
751 quit("master/slave protocol botched 4\n");
752 #endif
753
754 /* write pid of next slave to each slave */
755 for (i = 0; i < SLAVES; i++)
756 (void) atomic(write, slaves[i].fd,
757 (char *) &slaves[(i + 1) % SLAVES].pid,
758 sizeof slaves[0].pid);
759
760 master = 0;
761 }
762
763 void
764 killall()
765 {
766 register int i;
767
768 for (i = 0; i < SLAVES; i++)
769 if (slaves[i].pid > 0)
770 (void) kill(slaves[i].pid, SIGKILL);
771 }
772
773 /*
774 * Synchronization - each process has a lockfile, and shares file
775 * descriptors to the following process's lockfile. When our write
776 * completes, we release our lock on the following process's lock-
777 * file, allowing the following process to lock it and proceed. We
778 * get the lock back for the next cycle by swapping descriptors.
779 */
780 static void
781 doslave(cmd, slave_number)
782 register int cmd;
783 int slave_number;
784 {
785 register int nread;
786 int nextslave, size, wrote, eot_count;
787 #ifdef __linux__
788 errcode_t retval;
789 #endif
790
791 #ifdef TDEBUG
792 msg("slave %d, pid %d\n", slave_number, getpid());
793 #endif
794 /*
795 * Need our own seek pointer.
796 */
797 (void) close(diskfd);
798 if ((diskfd = open(disk, O_RDONLY)) < 0)
799 quit("slave couldn't reopen disk: %s\n", strerror(errno));
800 #ifdef __linux__
801 ext2fs_close(fs);
802 retval = ext2fs_open(disk, 0, 0, 0, unix_io_manager, &fs);
803 if (retval)
804 quit("slave couldn't reopen disk: %s\n", strerror(errno));
805 #endif /* __linux__ */
806
807 /*
808 * Need the pid of the next slave in the loop...
809 */
810 if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave))
811 != sizeof nextslave) {
812 quit("master/slave protocol botched - didn't get pid of next slave.\n");
813 }
814
815 /*
816 * Get list of blocks to dump, read the blocks into tape buffer
817 */
818 while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) {
819 register struct req *p = slp->req;
820
821 for (trecno = 0; trecno < ntrec;
822 trecno += p->count, p += p->count) {
823 if (p->dblk) {
824 bread(p->dblk, slp->tblock[trecno],
825 p->count * TP_BSIZE);
826 } else {
827 if (p->count != 1 || atomic(read, cmd,
828 (char *)slp->tblock[trecno],
829 TP_BSIZE) != TP_BSIZE)
830 quit("master/slave protocol botched.\n");
831 }
832 }
833 if (setjmp(jmpbuf) == 0) {
834 ready = 1;
835 if (!caught)
836 (void) pause();
837 }
838 ready = 0;
839 caught = 0;
840
841 /* Try to write the data... */
842 eot_count = 0;
843 size = 0;
844
845 while (eot_count < 10 && size < writesize) {
846 #ifdef RDUMP
847 if (host)
848 wrote = rmtwrite(slp->tblock[0]+size,
849 writesize-size);
850 else
851 #endif
852 wrote = write(tapefd, slp->tblock[0]+size,
853 writesize-size);
854 #ifdef WRITEDEBUG
855 msg("slave %d wrote %d\n", slave_number, wrote);
856 #endif
857 if (wrote < 0)
858 break;
859 if (wrote == 0)
860 eot_count++;
861 size += wrote;
862 }
863
864 #ifdef WRITEDEBUG
865 if (size != writesize)
866 msg("slave %d only wrote %d out of %d bytes and gave up.\n",
867 slave_number, size, writesize);
868 #endif
869
870 if (eot_count > 0)
871 size = 0;
872
873 /*
874 * fixme: Pyramids running OSx return ENOSPC
875 * at EOT on 1/2 inch drives.
876 */
877 if (size < 0) {
878 (void) kill(master, SIGUSR1);
879 for (;;)
880 (void) sigpause(0);
881 } else {
882 /*
883 * pass size of write back to master
884 * (for EOT handling)
885 */
886 (void) atomic(write, cmd, (char *)&size, sizeof size);
887 }
888
889 /*
890 * If partial write, don't want next slave to go.
891 * Also jolts him awake.
892 */
893 (void) kill(nextslave, SIGUSR2);
894 }
895 if (nread != 0)
896 quit("error reading command pipe: %s\n", strerror(errno));
897 }
898
899 /*
900 * Since a read from a pipe may not return all we asked for,
901 * or a write may not write all we ask if we get a signal,
902 * loop until the count is satisfied (or error).
903 */
904 static int
905 atomic(func, fd, buf, count)
906 int (*func)(), fd;
907 char *buf;
908 int count;
909 {
910 int got, need = count;
911
912 while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
913 buf += got;
914 return (got < 0 ? got : count - need);
915 }