Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / dump / tape.c
blob1c9a64c68bdaee8266ca8a1cbf682d05b3a82f0b
1 /* $NetBSD: tape.c,v 1.48 2006/12/18 20:07:32 christos Exp $ */
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
36 #else
37 __RCSID("$NetBSD: tape.c,v 1.48 2006/12/18 20:07:32 christos Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/param.h>
42 #include <sys/socket.h>
43 #include <sys/time.h>
44 #include <sys/wait.h>
45 #include <ufs/ufs/dinode.h>
46 #include <sys/ioctl.h>
47 #include <sys/mtio.h>
49 #include <protocols/dumprestore.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
60 #include "dump.h"
61 #include "pathnames.h"
63 int writesize; /* size of malloc()ed buffer for tape */
64 int64_t lastspclrec = -1; /* tape block number of last written header */
65 int trecno = 0; /* next record to write in current block */
66 extern long blocksperfile; /* number of blocks per output file */
67 long blocksthisvol; /* number of blocks on current output file */
68 extern int ntrec; /* blocking factor on tape */
69 extern int cartridge;
70 extern const char *host;
71 char *nexttape;
73 static ssize_t atomic_read(int, char *, int);
74 static ssize_t atomic_write(int, char *, int);
75 static void doslave(int, int);
76 static void enslave(void);
77 static void flushtape(void);
78 static void killall(void);
79 static void proceed(int);
80 static void rollforward(void);
81 static void sigpipe(int);
82 static void tperror(int);
85 * Concurrent dump mods (Caltech) - disk block reading and tape writing
86 * are exported to several slave processes. While one slave writes the
87 * tape, the others read disk blocks; they pass control of the tape in
88 * a ring via signals. The parent process traverses the file system and
89 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
90 * The following structure defines the instruction packets sent to slaves.
92 struct req {
93 daddr_t dblk;
94 int count;
96 int reqsiz;
98 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
99 struct slave {
100 int64_t tapea; /* header number at start of this chunk */
101 int64_t firstrec; /* record number of this block */
102 int count; /* count to next header (used for TS_TAPE */
103 /* after EOT) */
104 int inode; /* inode that we are currently dealing with */
105 int fd; /* FD for this slave */
106 int pid; /* PID for this slave */
107 int sent; /* 1 == we've sent this slave requests */
108 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
109 struct req *req; /* buffer for requests */
110 } slaves[SLAVES+1];
111 struct slave *slp;
113 char (*nextblock)[TP_BSIZE];
115 static int64_t tapea_volume; /* value of spcl.c_tapea at volume start */
117 int master; /* pid of master, for sending error signals */
118 int tenths; /* length of tape used per block written */
119 static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
122 alloctape(void)
124 int pgoff = getpagesize() - 1;
125 char *buf;
126 int i;
128 writesize = ntrec * TP_BSIZE;
129 reqsiz = (ntrec + 1) * sizeof(struct req);
131 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
132 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
133 * repositioning after stopping, i.e, streaming mode, where the gap is
134 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
136 if (blocksperfile == 0 && !unlimited)
137 tenths = writesize / density +
138 (cartridge ? 16 : density == 625 ? 5 : 8);
140 * Allocate tape buffer contiguous with the array of instruction
141 * packets, so flushtape() can write them together with one write().
142 * Align tape buffer on page boundary to speed up tape write().
144 for (i = 0; i <= SLAVES; i++) {
145 buf = (char *)
146 xmalloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
147 slaves[i].tblock = (char (*)[TP_BSIZE])
148 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
149 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
151 slp = &slaves[0];
152 slp->count = 1;
153 slp->tapea = 0;
154 slp->firstrec = 0;
155 nextblock = slp->tblock;
156 return(1);
159 void
160 writerec(char *dp, int isspcl)
163 slp->req[trecno].dblk = (daddr_t)0;
164 slp->req[trecno].count = 1;
165 *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp;
166 if (isspcl)
167 lastspclrec = iswap64(spcl.c_tapea);
168 trecno++;
169 spcl.c_tapea = iswap64(iswap64(spcl.c_tapea) +1);
170 if (trecno >= ntrec)
171 flushtape();
174 void
175 dumpblock(daddr_t blkno, int size)
177 int avail, tpblks;
178 daddr_t dblkno;
180 dblkno = fsatoda(ufsib, blkno);
181 tpblks = size >> tp_bshift;
182 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
183 slp->req[trecno].dblk = dblkno;
184 slp->req[trecno].count = avail;
185 trecno += avail;
186 spcl.c_tapea = iswap64(iswap64(spcl.c_tapea) + avail);
187 if (trecno >= ntrec)
188 flushtape();
189 dblkno += avail << (tp_bshift - dev_bshift);
190 tpblks -= avail;
194 int nogripe = 0;
196 static void
197 tperror(int signo __unused)
200 if (pipeout) {
201 msg("write error on %s\n", tape);
202 quit("Cannot recover\n");
203 /* NOTREACHED */
205 msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
206 broadcast("DUMP WRITE ERROR!\n");
207 if (!query("Do you want to restart?"))
208 dumpabort(0);
209 msg("Closing this volume. Prepare to restart with new media;\n");
210 msg("this dump volume will be rewritten.\n");
211 killall();
212 nogripe = 1;
213 close_rewind();
214 Exit(X_REWRITE);
217 static void
218 sigpipe(int signo __unused)
221 quit("Broken pipe\n");
225 * do_stats --
226 * Update xferrate stats
228 time_t
229 do_stats(void)
231 time_t tnow, ttaken;
232 int64_t blocks;
234 (void)time(&tnow);
235 ttaken = tnow - tstart_volume;
236 blocks = iswap64(spcl.c_tapea) - tapea_volume;
237 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
238 if (ttaken > 0) {
239 msg("Volume %d took %d:%02d:%02d\n", tapeno,
240 (int) (ttaken / 3600), (int) ((ttaken % 3600) / 60),
241 (int) (ttaken % 60));
242 msg("Volume %d transfer rate: %d KB/s\n", tapeno,
243 (int) (blocks / ttaken));
244 xferrate += blocks / ttaken;
246 return(tnow);
250 * statussig --
251 * information message upon receipt of SIGINFO
252 * (derived from optr.c::timeest())
254 void
255 statussig(int notused __unused)
257 time_t tnow, deltat;
258 char msgbuf[128];
259 int errno_save;
261 if (blockswritten < 500)
262 return;
263 errno_save = errno;
264 (void) time((time_t *) &tnow);
265 if (tnow <= tstart_volume)
266 return;
267 deltat = tstart_writing - tnow +
268 (1.0 * (tnow - tstart_writing)) / blockswritten * tapesize;
269 (void)snprintf(msgbuf, sizeof(msgbuf),
270 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
271 (blockswritten * 100.0) / tapesize,
272 (long)((iswap64(spcl.c_tapea) - tapea_volume) /
273 (tnow - tstart_volume)),
274 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
275 write(STDERR_FILENO, msgbuf, strlen(msgbuf));
276 errno = errno_save;
279 static void
280 flushtape(void)
282 int i, blks, got;
283 int64_t lastfirstrec;
285 int siz = (char *)nextblock - (char *)slp->req;
287 slp->req[trecno].count = 0; /* Sentinel */
289 if (atomic_write(slp->fd, (char *)slp->req, siz) != siz)
290 quit("error writing command pipe: %s\n", strerror(errno));
291 slp->sent = 1; /* we sent a request, read the response later */
293 lastfirstrec = slp->firstrec;
295 if (++slp >= &slaves[SLAVES])
296 slp = &slaves[0];
298 /* Read results back from next slave */
299 if (slp->sent) {
300 if (atomic_read(slp->fd, (char *)&got, sizeof got)
301 != sizeof got) {
302 perror(" DUMP: error reading command pipe in master");
303 dumpabort(0);
305 slp->sent = 0;
307 /* Check for end of tape */
308 if (got < writesize) {
309 msg("End of tape detected\n");
312 * Drain the results, don't care what the values were.
313 * If we read them here then trewind won't...
315 for (i = 0; i < SLAVES; i++) {
316 if (slaves[i].sent) {
317 if (atomic_read(slaves[i].fd,
318 (char *)&got, sizeof got)
319 != sizeof got) {
320 perror(" DUMP: error reading command pipe in master");
321 dumpabort(0);
323 slaves[i].sent = 0;
327 close_rewind();
328 rollforward();
329 return;
333 blks = 0;
334 if (iswap32(spcl.c_type) != TS_END) {
335 for (i = 0; i < iswap32(spcl.c_count); i++)
336 if (spcl.c_addr[i] != 0)
337 blks++;
339 slp->count = lastspclrec + blks + 1 - iswap32(spcl.c_tapea);
340 slp->tapea = iswap64(spcl.c_tapea);
341 slp->firstrec = lastfirstrec + ntrec;
342 slp->inode = curino;
343 nextblock = slp->tblock;
344 trecno = 0;
345 asize += tenths;
346 blockswritten += ntrec;
347 blocksthisvol += ntrec;
348 if (!pipeout && !unlimited && (blocksperfile ?
349 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
350 close_rewind();
351 startnewtape(0);
353 timeest();
356 void
357 trewind(int eject)
359 int f;
360 int got;
362 for (f = 0; f < SLAVES; f++) {
364 * Drain the results, but unlike EOT we DO (or should) care
365 * what the return values were, since if we detect EOT after
366 * we think we've written the last blocks to the tape anyway,
367 * we have to replay those blocks with rollforward.
369 * fixme: punt for now.
371 if (slaves[f].sent) {
372 if (atomic_read(slaves[f].fd, (char *)&got, sizeof got)
373 != sizeof got) {
374 perror(" DUMP: error reading command pipe in master");
375 dumpabort(0);
377 slaves[f].sent = 0;
378 if (got != writesize) {
379 msg("EOT detected in last 2 tape records!\n");
380 msg("Use a longer tape, decrease the size estimate\n");
381 quit("or use no size estimate at all.\n");
384 (void) close(slaves[f].fd);
386 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
387 /* void */;
389 if (pipeout)
390 return;
392 msg("Closing %s\n", tape);
394 #ifdef RDUMP
395 if (host) {
396 rmtclose();
397 while (rmtopen(tape, 0, 0) < 0)
398 sleep(10);
399 if (eflag && eject) {
400 msg("Ejecting %s\n", tape);
401 (void) rmtioctl(MTOFFL, 0);
403 rmtclose();
404 return;
406 #endif
407 (void) close(tapefd);
408 while ((f = open(tape, 0)) < 0)
409 sleep (10);
410 if (eflag && eject) {
411 struct mtop offl;
413 msg("Ejecting %s\n", tape);
414 offl.mt_op = MTOFFL;
415 offl.mt_count = 0;
416 (void) ioctl(f, MTIOCTOP, &offl);
418 (void) close(f);
421 void
422 close_rewind(void)
424 int i, f;
426 trewind(1);
427 (void)do_stats();
428 if (nexttape)
429 return;
430 if (!nogripe) {
431 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
432 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
434 if (lflag) {
435 for (i = 0; i < lflag / 10; i++) { /* wait lflag seconds */
436 if (host) {
437 if (rmtopen(tape, 0, 0) >= 0) {
438 rmtclose();
439 return;
441 } else {
442 if ((f = open(tape, 0)) >= 0) {
443 close(f);
444 return;
447 sleep (10);
451 while (!query("Is the new volume mounted and ready to go?"))
452 if (query("Do you want to abort?")) {
453 dumpabort(0);
454 /*NOTREACHED*/
458 void
459 rollforward(void)
461 struct req *p, *q, *prev;
462 struct slave *tslp;
463 int i, size, savedtapea, got;
464 union u_spcl *ntb, *otb;
465 tslp = &slaves[SLAVES];
466 ntb = (union u_spcl *)tslp->tblock[1];
469 * Each of the N slaves should have requests that need to
470 * be replayed on the next tape. Use the extra slave buffers
471 * (slaves[SLAVES]) to construct request lists to be sent to
472 * each slave in turn.
474 for (i = 0; i < SLAVES; i++) {
475 q = &tslp->req[1];
476 otb = (union u_spcl *)slp->tblock;
479 * For each request in the current slave, copy it to tslp.
482 prev = NULL;
483 for (p = slp->req; p->count > 0; p += p->count) {
484 *q = *p;
485 if (p->dblk == 0)
486 *ntb++ = *otb++; /* copy the datablock also */
487 prev = q;
488 q += q->count;
490 if (prev == NULL)
491 quit("rollforward: protocol botch");
492 if (prev->dblk != 0)
493 prev->count -= 1;
494 else
495 ntb--;
496 q -= 1;
497 q->count = 0;
498 q = &tslp->req[0];
499 if (i == 0) {
500 q->dblk = 0;
501 q->count = 1;
502 trecno = 0;
503 nextblock = tslp->tblock;
504 savedtapea = iswap32(spcl.c_tapea);
505 spcl.c_tapea = iswap32(slp->tapea);
506 startnewtape(0);
507 spcl.c_tapea = iswap32(savedtapea);
508 lastspclrec = savedtapea - 1;
510 size = (char *)ntb - (char *)q;
511 if (atomic_write(slp->fd, (char *)q, size) != size) {
512 perror(" DUMP: error writing command pipe");
513 dumpabort(0);
515 slp->sent = 1;
516 if (++slp >= &slaves[SLAVES])
517 slp = &slaves[0];
519 q->count = 1;
521 if (prev->dblk != 0) {
523 * If the last one was a disk block, make the
524 * first of this one be the last bit of that disk
525 * block...
527 q->dblk = prev->dblk +
528 prev->count * (TP_BSIZE / DEV_BSIZE);
529 ntb = (union u_spcl *)tslp->tblock;
530 } else {
532 * It wasn't a disk block. Copy the data to its
533 * new location in the buffer.
535 q->dblk = 0;
536 *((union u_spcl *)tslp->tblock) = *ntb;
537 ntb = (union u_spcl *)tslp->tblock[1];
540 slp->req[0] = *q;
541 nextblock = slp->tblock;
542 if (q->dblk == 0)
543 nextblock++;
544 trecno = 1;
547 * Clear the first slaves' response. One hopes that it
548 * worked ok, otherwise the tape is much too short!
550 if (slp->sent) {
551 if (atomic_read(slp->fd, (char *)&got, sizeof got)
552 != sizeof got) {
553 perror(" DUMP: error reading command pipe in master");
554 dumpabort(0);
556 slp->sent = 0;
558 if (got != writesize) {
559 quit("EOT detected at start of the tape!\n");
565 * We implement taking and restoring checkpoints on the tape level.
566 * When each tape is opened, a new process is created by forking; this
567 * saves all of the necessary context in the parent. The child
568 * continues the dump; the parent waits around, saving the context.
569 * If the child returns X_REWRITE, then it had problems writing that tape;
570 * this causes the parent to fork again, duplicating the context, and
571 * everything continues as if nothing had happened.
573 void
574 startnewtape(int top)
576 int parentpid;
577 int childpid;
578 int status;
579 int waitforpid;
580 char *p;
581 sig_t interrupt_save;
583 interrupt_save = signal(SIGINT, SIG_IGN);
584 parentpid = getpid();
585 tapea_volume = iswap32(spcl.c_tapea);
586 (void)time(&tstart_volume);
588 restore_check_point:
589 (void)signal(SIGINT, interrupt_save);
591 * All signals are inherited...
593 childpid = fork();
594 if (childpid < 0) {
595 msg("Context save fork fails in parent %d\n", parentpid);
596 Exit(X_ABORT);
598 if (childpid != 0) {
600 * PARENT:
601 * save the context by waiting
602 * until the child doing all of the work returns.
603 * don't catch the interrupt
605 signal(SIGINT, SIG_IGN);
606 signal(SIGINFO, SIG_IGN); /* only want child's stats */
607 #ifdef TDEBUG
608 msg("Tape: %d; parent process: %d child process %d\n",
609 tapeno+1, parentpid, childpid);
610 #endif /* TDEBUG */
611 while ((waitforpid = wait(&status)) != childpid)
612 msg("Parent %d waiting for child %d has another child %d return\n",
613 parentpid, childpid, waitforpid);
614 if (status & 0xFF) {
615 msg("Child %d returns LOB status %o\n",
616 childpid, status&0xFF);
618 status = (status >> 8) & 0xFF;
619 #ifdef TDEBUG
620 switch(status) {
621 case X_FINOK:
622 msg("Child %d finishes X_FINOK\n", childpid);
623 break;
624 case X_ABORT:
625 msg("Child %d finishes X_ABORT\n", childpid);
626 break;
627 case X_REWRITE:
628 msg("Child %d finishes X_REWRITE\n", childpid);
629 break;
630 default:
631 msg("Child %d finishes unknown %d\n",
632 childpid, status);
633 break;
635 #endif /* TDEBUG */
636 switch(status) {
637 case X_FINOK:
638 Exit(X_FINOK);
639 case X_ABORT:
640 Exit(X_ABORT);
641 case X_REWRITE:
642 goto restore_check_point;
643 default:
644 msg("Bad return code from dump: %d\n", status);
645 Exit(X_ABORT);
647 /*NOTREACHED*/
648 } else { /* we are the child; just continue */
649 signal(SIGINFO, statussig); /* now want child's stats */
650 #ifdef TDEBUG
651 sleep(4); /* allow time for parent's message to get out */
652 msg("Child on Tape %d has parent %d, my pid = %d\n",
653 tapeno+1, parentpid, getpid());
654 #endif /* TDEBUG */
656 * If we have a name like "/dev/rst0,/dev/rst1",
657 * use the name before the comma first, and save
658 * the remaining names for subsequent volumes.
660 tapeno++; /* current tape sequence */
661 if (nexttape || strchr(tape, ',')) {
662 if (nexttape && *nexttape)
663 tape = nexttape;
664 if ((p = strchr(tape, ',')) != NULL) {
665 *p = '\0';
666 nexttape = p + 1;
667 } else
668 nexttape = NULL;
669 msg("Dumping volume %d on %s\n", tapeno, tape);
671 #ifdef RDUMP
672 while ((tapefd = (host ? rmtopen(tape, 2, 1) :
673 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
674 #else
675 while ((tapefd = (pipeout ? 1 :
676 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
677 #endif
679 msg("Cannot open output \"%s\".\n", tape);
680 if (!query("Do you want to retry the open?"))
681 dumpabort(0);
684 enslave(); /* Share open tape file descriptor with slaves */
686 asize = 0;
687 blocksthisvol = 0;
688 if (top)
689 newtape++; /* new tape signal */
690 spcl.c_count = iswap32(slp->count);
692 * measure firstrec in TP_BSIZE units since restore doesn't
693 * know the correct ntrec value...
695 spcl.c_firstrec = iswap32(slp->firstrec);
696 spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
697 spcl.c_type = iswap32(TS_TAPE);
698 if (!is_ufs2)
699 spcl.c_flags = iswap32(iswap32(spcl.c_flags)
700 | DR_NEWHEADER);
701 writeheader((ino_t)slp->inode);
702 if (!is_ufs2)
703 spcl.c_flags = iswap32(iswap32(spcl.c_flags) &
704 ~ DR_NEWHEADER);
705 msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
706 if (tapeno > 1)
707 msg("Volume %d begins with blocks from inode %d\n",
708 tapeno, slp->inode);
712 void
713 dumpabort(int signo __unused)
716 if (master != 0 && master != getpid())
717 /* Signals master to call dumpabort */
718 (void) kill(master, SIGTERM);
719 else {
720 #ifdef DUMP_LFS
721 lfs_wrap_go();
722 #endif
723 killall();
724 msg("The ENTIRE dump is aborted.\n");
726 #ifdef RDUMP
727 rmtclose();
728 #endif
729 Exit(X_ABORT);
732 void
733 Exit(int status)
736 #ifdef TDEBUG
737 msg("pid = %d exits with status %d\n", getpid(), status);
738 #endif /* TDEBUG */
739 exit(status);
743 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
745 static void
746 proceed(int signo __unused)
748 caught++;
751 void
752 enslave(void)
754 int cmd[2];
755 int i, j;
757 master = getpid();
759 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
760 signal(SIGPIPE, sigpipe);
761 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
762 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
764 for (i = 0; i < SLAVES; i++) {
765 if (i == slp - &slaves[0]) {
766 caught = 1;
767 } else {
768 caught = 0;
771 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
772 (slaves[i].pid = fork()) < 0)
773 quit("too many slaves, %d (recompile smaller): %s\n",
774 i, strerror(errno));
776 slaves[i].fd = cmd[1];
777 slaves[i].sent = 0;
778 if (slaves[i].pid == 0) { /* Slave starts up here */
779 for (j = 0; j <= i; j++)
780 (void) close(slaves[j].fd);
781 signal(SIGINT, SIG_IGN); /* Master handles this */
782 signal(SIGINFO, SIG_IGN);
783 doslave(cmd[0], i);
784 Exit(X_FINOK);
788 for (i = 0; i < SLAVES; i++)
789 (void) atomic_write(slaves[i].fd,
790 (char *) &slaves[(i + 1) % SLAVES].pid,
791 sizeof slaves[0].pid);
793 master = 0;
796 void
797 killall(void)
799 int i;
801 for (i = 0; i < SLAVES; i++)
802 if (slaves[i].pid > 0) {
803 (void) kill(slaves[i].pid, SIGKILL);
804 slaves[i].sent = 0;
809 * Synchronization - each process has a lockfile, and shares file
810 * descriptors to the following process's lockfile. When our write
811 * completes, we release our lock on the following process's lock-
812 * file, allowing the following process to lock it and proceed. We
813 * get the lock back for the next cycle by swapping descriptors.
815 static void
816 doslave(int cmd, int slave_number __unused)
818 int nread, nextslave, size, wrote, eot_count, werror;
819 sigset_t nsigset, osigset;
821 wrote = 0;
823 * Need our own seek pointer.
825 (void) close(diskfd);
826 if ((diskfd = open(disk_dev, O_RDONLY)) < 0)
827 quit("slave couldn't reopen disk: %s\n", strerror(errno));
830 * Need the pid of the next slave in the loop...
832 if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
833 != sizeof nextslave) {
834 quit("master/slave protocol botched - didn't get pid of next slave.\n");
838 * Get list of blocks to dump, read the blocks into tape buffer
840 while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
841 struct req *p = slp->req;
843 for (trecno = 0; trecno < ntrec;
844 trecno += p->count, p += p->count) {
845 if (p->dblk) {
846 bread(p->dblk, slp->tblock[trecno],
847 p->count * TP_BSIZE);
848 } else {
849 if (p->count != 1 || atomic_read(cmd,
850 (char *)slp->tblock[trecno],
851 TP_BSIZE) != TP_BSIZE)
852 quit("master/slave protocol botched.\n");
856 sigemptyset(&nsigset);
857 sigaddset(&nsigset, SIGUSR2);
858 sigprocmask(SIG_BLOCK, &nsigset, &osigset);
859 while (!caught)
860 sigsuspend(&osigset);
861 caught = 0;
862 sigprocmask(SIG_SETMASK, &osigset, NULL);
864 /* Try to write the data... */
865 eot_count = 0;
866 size = 0;
867 werror = 0;
869 while (eot_count < 10 && size < writesize) {
870 #ifdef RDUMP
871 if (host)
872 wrote = rmtwrite(slp->tblock[0]+size,
873 writesize-size);
874 else
875 #endif
876 wrote = write(tapefd, slp->tblock[0]+size,
877 writesize-size);
878 werror = errno;
879 #ifdef WRITEDEBUG
880 fprintf(stderr, "slave %d wrote %d werror %d\n",
881 slave_number, wrote, werror);
882 #endif
883 if (wrote < 0)
884 break;
885 if (wrote == 0)
886 eot_count++;
887 size += wrote;
890 #ifdef WRITEDEBUG
891 if (size != writesize)
892 fprintf(stderr,
893 "slave %d only wrote %d out of %d bytes and gave up.\n",
894 slave_number, size, writesize);
895 #endif
898 * Handle ENOSPC as an EOT condition.
900 if (wrote < 0 && werror == ENOSPC) {
901 wrote = 0;
902 eot_count++;
905 if (eot_count > 0)
906 size = 0;
908 if (wrote < 0) {
909 (void) kill(master, SIGUSR1);
910 sigemptyset(&nsigset);
911 for (;;)
912 sigsuspend(&nsigset);
913 } else {
915 * pass size of write back to master
916 * (for EOT handling)
918 (void) atomic_write(cmd, (char *)&size, sizeof size);
922 * If partial write, don't want next slave to go.
923 * Also jolts him awake.
925 (void) kill(nextslave, SIGUSR2);
927 printcachestats();
928 if (nread != 0)
929 quit("error reading command pipe: %s\n", strerror(errno));
933 * Since a read from a pipe may not return all we asked for,
934 * loop until the count is satisfied (or error).
936 static ssize_t
937 atomic_read(int fd, char *buf, int count)
939 ssize_t got, need = count;
941 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
942 buf += got;
943 return (got < 0 ? got : count - need);
947 * Since a write may not write all we ask if we get a signal,
948 * loop until the count is satisfied (or error).
950 static ssize_t
951 atomic_write(int fd, char *buf, int count)
953 ssize_t got, need = count;
955 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
956 buf += got;
957 return (got < 0 ? got : count - need);