tape.c revision 1.50.4.1 1 /* $NetBSD: tape.c,v 1.50.4.1 2016/03/08 10:05:43 snj Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
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.
18 *
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.
30 */
31
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.50.4.1 2016/03/08 10:05:43 snj Exp $");
38 #endif
39 #endif /* not lint */
40
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>
48
49 #include <protocols/dumprestore.h>
50
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>
59
60 #include "dump.h"
61 #include "pathnames.h"
62
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;
72
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);
83
84 /*
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.
91 */
92 struct req {
93 daddr_t dblk;
94 int count;
95 };
96 int reqsiz;
97
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;
112
113 char (*nextblock)[TP_BSIZE];
114
115 static int64_t tapea_volume; /* value of spcl.c_tapea at volume start */
116
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? */
120
121 int
122 alloctape(void)
123 {
124 int pgoff = getpagesize() - 1;
125 char *buf;
126 int i;
127
128 writesize = ntrec * TP_BSIZE;
129 reqsiz = (ntrec + 1) * sizeof(struct req);
130 /*
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.
135 */
136 if (blocksperfile == 0 && !unlimited)
137 tenths = writesize / density +
138 (cartridge ? 16 : density == 625 ? 5 : 8);
139 /*
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().
143 */
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;
150 }
151 slp = &slaves[0];
152 slp->count = 1;
153 slp->tapea = 0;
154 slp->firstrec = 0;
155 nextblock = slp->tblock;
156 return(1);
157 }
158
159 void
160 writerec(char *dp, int isspcl)
161 {
162
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();
172 }
173
174 void
175 dumpblock(daddr_t blkno, int size)
176 {
177 int avail, tpblks;
178 daddr_t dblkno;
179
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;
191 }
192 }
193
194 int nogripe = 0;
195
196 static void
197 tperror(int signo __unused)
198 {
199
200 if (pipeout) {
201 msg("write error on %s\n", tape);
202 quit("Cannot recover\n");
203 /* NOTREACHED */
204 }
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);
215 }
216
217 static void
218 sigpipe(int signo __unused)
219 {
220
221 quit("Broken pipe\n");
222 }
223
224 /*
225 * do_stats --
226 * Update xferrate stats
227 */
228 time_t
229 do_stats(void)
230 {
231 time_t tnow, ttaken;
232 int64_t blocks;
233
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;
245 }
246 return(tnow);
247 }
248
249 /*
250 * statussig --
251 * information message upon receipt of SIGINFO
252 * (derived from optr.c::timeest())
253 */
254 void
255 statussig(int notused __unused)
256 {
257 time_t tnow, deltat;
258 char msgbuf[128];
259 int errno_save;
260
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;
277 }
278
279 static void
280 flushtape(void)
281 {
282 int i, blks, got;
283 int64_t lastfirstrec;
284
285 int siz = (char *)nextblock - (char *)slp->req;
286
287 slp->req[trecno].count = 0; /* Sentinel */
288
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 */
292
293 lastfirstrec = slp->firstrec;
294
295 if (++slp >= &slaves[SLAVES])
296 slp = &slaves[0];
297
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);
304 }
305 slp->sent = 0;
306
307 /* Check for end of tape */
308 if (got < writesize) {
309 msg("End of tape detected\n");
310
311 /*
312 * Drain the results, don't care what the values were.
313 * If we read them here then trewind won't...
314 */
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);
322 }
323 slaves[i].sent = 0;
324 }
325 }
326
327 close_rewind();
328 rollforward();
329 return;
330 }
331 }
332
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++;
338 }
339 slp->count = lastspclrec + blks + 1 - iswap64(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);
352 }
353 timeest();
354 }
355
356 void
357 trewind(int eject)
358 {
359 int f;
360 int got;
361
362 for (f = 0; f < SLAVES; f++) {
363 /*
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.
368 *
369 * fixme: punt for now.
370 */
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);
376 }
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");
382 }
383 }
384 (void) close(slaves[f].fd);
385 }
386 while (wait(NULL) >= 0) /* wait for any signals from slaves */
387 /* void */;
388
389 if (pipeout)
390 return;
391
392 msg("Closing %s\n", tape);
393
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);
402 }
403 rmtclose();
404 return;
405 }
406 #endif
407 (void) close(tapefd);
408 while ((f = open(tape, 0)) < 0)
409 sleep (10);
410 if (eflag && eject) {
411 struct mtop offl;
412
413 msg("Ejecting %s\n", tape);
414 offl.mt_op = MTOFFL;
415 offl.mt_count = 0;
416 (void) ioctl(f, MTIOCTOP, &offl);
417 }
418 (void) close(f);
419 }
420
421 void
422 close_rewind(void)
423 {
424 int i, f;
425
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");
433 }
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;
440 }
441 } else {
442 if ((f = open(tape, 0)) >= 0) {
443 close(f);
444 return;
445 }
446 }
447 sleep (10);
448 }
449 }
450
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*/
455 }
456 }
457
458 void
459 rollforward(void)
460 {
461 struct req *p, *q, *prev;
462 struct slave *tslp;
463 int i, size, got;
464 int64_t savedtapea;
465 union u_spcl *ntb, *otb;
466 tslp = &slaves[SLAVES];
467 ntb = (union u_spcl *)tslp->tblock[1];
468
469 /*
470 * Each of the N slaves should have requests that need to
471 * be replayed on the next tape. Use the extra slave buffers
472 * (slaves[SLAVES]) to construct request lists to be sent to
473 * each slave in turn.
474 */
475 for (i = 0; i < SLAVES; i++) {
476 q = &tslp->req[1];
477 otb = (union u_spcl *)slp->tblock;
478
479 /*
480 * For each request in the current slave, copy it to tslp.
481 */
482
483 prev = NULL;
484 for (p = slp->req; p->count > 0; p += p->count) {
485 *q = *p;
486 if (p->dblk == 0)
487 *ntb++ = *otb++; /* copy the datablock also */
488 prev = q;
489 q += q->count;
490 }
491 if (prev == NULL)
492 quit("rollforward: protocol botch");
493 if (prev->dblk != 0)
494 prev->count -= 1;
495 else
496 ntb--;
497 q -= 1;
498 q->count = 0;
499 q = &tslp->req[0];
500 if (i == 0) {
501 q->dblk = 0;
502 q->count = 1;
503 trecno = 0;
504 nextblock = tslp->tblock;
505 savedtapea = iswap64(spcl.c_tapea);
506 spcl.c_tapea = iswap64(slp->tapea);
507 startnewtape(0);
508 spcl.c_tapea = iswap64(savedtapea);
509 lastspclrec = savedtapea - 1;
510 }
511 size = (char *)ntb - (char *)q;
512 if (atomic_write(slp->fd, (char *)q, size) != size) {
513 perror(" DUMP: error writing command pipe");
514 dumpabort(0);
515 }
516 slp->sent = 1;
517 if (++slp >= &slaves[SLAVES])
518 slp = &slaves[0];
519
520 q->count = 1;
521
522 if (prev->dblk != 0) {
523 /*
524 * If the last one was a disk block, make the
525 * first of this one be the last bit of that disk
526 * block...
527 */
528 q->dblk = prev->dblk +
529 prev->count * (TP_BSIZE / DEV_BSIZE);
530 ntb = (union u_spcl *)tslp->tblock;
531 } else {
532 /*
533 * It wasn't a disk block. Copy the data to its
534 * new location in the buffer.
535 */
536 q->dblk = 0;
537 *((union u_spcl *)tslp->tblock) = *ntb;
538 ntb = (union u_spcl *)tslp->tblock[1];
539 }
540 }
541 slp->req[0] = *q;
542 nextblock = slp->tblock;
543 if (q->dblk == 0)
544 nextblock++;
545 trecno = 1;
546
547 /*
548 * Clear the first slaves' response. One hopes that it
549 * worked ok, otherwise the tape is much too short!
550 */
551 if (slp->sent) {
552 if (atomic_read(slp->fd, (char *)&got, sizeof got)
553 != sizeof got) {
554 perror(" DUMP: error reading command pipe in master");
555 dumpabort(0);
556 }
557 slp->sent = 0;
558
559 if (got != writesize) {
560 quit("EOT detected at start of the tape!\n");
561 }
562 }
563 }
564
565 /*
566 * We implement taking and restoring checkpoints on the tape level.
567 * When each tape is opened, a new process is created by forking; this
568 * saves all of the necessary context in the parent. The child
569 * continues the dump; the parent waits around, saving the context.
570 * If the child returns X_REWRITE, then it had problems writing that tape;
571 * this causes the parent to fork again, duplicating the context, and
572 * everything continues as if nothing had happened.
573 */
574 void
575 startnewtape(int top)
576 {
577 int parentpid;
578 int childpid;
579 int status;
580 int waitforpid;
581 char *p;
582 sig_t interrupt_save;
583
584 interrupt_save = signal(SIGINT, SIG_IGN);
585 parentpid = getpid();
586 tapea_volume = iswap64(spcl.c_tapea);
587 (void)time(&tstart_volume);
588
589 restore_check_point:
590 (void)signal(SIGINT, interrupt_save);
591 /*
592 * All signals are inherited...
593 */
594 childpid = fork();
595 if (childpid < 0) {
596 msg("Context save fork fails in parent %d\n", parentpid);
597 Exit(X_ABORT);
598 }
599 if (childpid != 0) {
600 /*
601 * PARENT:
602 * save the context by waiting
603 * until the child doing all of the work returns.
604 * don't catch the interrupt
605 */
606 signal(SIGINT, SIG_IGN);
607 signal(SIGINFO, SIG_IGN); /* only want child's stats */
608 #ifdef TDEBUG
609 msg("Tape: %d; parent process: %d child process %d\n",
610 tapeno+1, parentpid, childpid);
611 #endif /* TDEBUG */
612 while ((waitforpid = wait(&status)) != childpid)
613 msg("Parent %d waiting for child %d has another child %d return\n",
614 parentpid, childpid, waitforpid);
615 if (status & 0xFF) {
616 msg("Child %d returns LOB status %o\n",
617 childpid, status&0xFF);
618 }
619 status = (status >> 8) & 0xFF;
620 #ifdef TDEBUG
621 switch(status) {
622 case X_FINOK:
623 msg("Child %d finishes X_FINOK\n", childpid);
624 break;
625 case X_ABORT:
626 msg("Child %d finishes X_ABORT\n", childpid);
627 break;
628 case X_REWRITE:
629 msg("Child %d finishes X_REWRITE\n", childpid);
630 break;
631 default:
632 msg("Child %d finishes unknown %d\n",
633 childpid, status);
634 break;
635 }
636 #endif /* TDEBUG */
637 switch(status) {
638 case X_FINOK:
639 Exit(X_FINOK);
640 case X_ABORT:
641 Exit(X_ABORT);
642 case X_REWRITE:
643 goto restore_check_point;
644 default:
645 msg("Bad return code from dump: %d\n", status);
646 Exit(X_ABORT);
647 }
648 /*NOTREACHED*/
649 } else { /* we are the child; just continue */
650 signal(SIGINFO, statussig); /* now want child's stats */
651 #ifdef TDEBUG
652 sleep(4); /* allow time for parent's message to get out */
653 msg("Child on Tape %d has parent %d, my pid = %d\n",
654 tapeno+1, parentpid, getpid());
655 #endif /* TDEBUG */
656 /*
657 * If we have a name like "/dev/rst0,/dev/rst1",
658 * use the name before the comma first, and save
659 * the remaining names for subsequent volumes.
660 */
661 tapeno++; /* current tape sequence */
662 if (nexttape || strchr(tape, ',')) {
663 if (nexttape && *nexttape)
664 tape = nexttape;
665 if ((p = strchr(tape, ',')) != NULL) {
666 *p = '\0';
667 nexttape = p + 1;
668 } else
669 nexttape = NULL;
670 msg("Dumping volume %d on %s\n", tapeno, tape);
671 }
672 #ifdef RDUMP
673 while ((tapefd = (host ? rmtopen(tape, 2, 1) :
674 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
675 #else
676 while ((tapefd = (pipeout ? 1 :
677 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
678 #endif
679 {
680 msg("Cannot open output \"%s\".\n", tape);
681 if (!query("Do you want to retry the open?"))
682 dumpabort(0);
683 }
684
685 enslave(); /* Share open tape file descriptor with slaves */
686
687 asize = 0;
688 blocksthisvol = 0;
689 if (top)
690 newtape++; /* new tape signal */
691 spcl.c_count = iswap32(slp->count);
692 /*
693 * measure firstrec in TP_BSIZE units since restore doesn't
694 * know the correct ntrec value...
695 */
696 spcl.c_firstrec = iswap32(slp->firstrec);
697 spcl.c_volume = iswap32(iswap32(spcl.c_volume) + 1);
698 spcl.c_type = iswap32(TS_TAPE);
699 if (!is_ufs2)
700 spcl.c_flags = iswap32(iswap32(spcl.c_flags)
701 | DR_NEWHEADER);
702 writeheader((ino_t)slp->inode);
703 if (!is_ufs2)
704 spcl.c_flags = iswap32(iswap32(spcl.c_flags) &
705 ~ DR_NEWHEADER);
706 msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume));
707 if (tapeno > 1)
708 msg("Volume %d begins with blocks from inode %d\n",
709 tapeno, slp->inode);
710 }
711 }
712
713 void
714 dumpabort(int signo __unused)
715 {
716
717 if (master != 0 && master != getpid())
718 /* Signals master to call dumpabort */
719 (void) kill(master, SIGTERM);
720 else {
721 #ifdef DUMP_LFS
722 lfs_wrap_go();
723 #endif
724 killall();
725 msg("The ENTIRE dump is aborted.\n");
726 }
727 #ifdef RDUMP
728 rmtclose();
729 #endif
730 Exit(X_ABORT);
731 }
732
733 void
734 Exit(int status)
735 {
736
737 #ifdef TDEBUG
738 msg("pid = %d exits with status %d\n", getpid(), status);
739 #endif /* TDEBUG */
740 exit(status);
741 }
742
743 /*
744 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
745 */
746 static void
747 proceed(int signo __unused)
748 {
749 caught++;
750 }
751
752 void
753 enslave(void)
754 {
755 int cmd[2];
756 int i, j;
757
758 master = getpid();
759
760 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
761 signal(SIGPIPE, sigpipe);
762 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
763 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
764
765 for (i = 0; i < SLAVES; i++) {
766 if (i == slp - &slaves[0]) {
767 caught = 1;
768 } else {
769 caught = 0;
770 }
771
772 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, cmd) < 0 ||
773 (slaves[i].pid = fork()) < 0)
774 quit("too many slaves, %d (recompile smaller): %s\n",
775 i, strerror(errno));
776
777 slaves[i].fd = cmd[1];
778 slaves[i].sent = 0;
779 if (slaves[i].pid == 0) { /* Slave starts up here */
780 for (j = 0; j <= i; j++)
781 (void) close(slaves[j].fd);
782 signal(SIGINT, SIG_IGN); /* Master handles this */
783 signal(SIGINFO, SIG_IGN);
784 doslave(cmd[0], i);
785 Exit(X_FINOK);
786 }
787 }
788
789 for (i = 0; i < SLAVES; i++)
790 (void) atomic_write(slaves[i].fd,
791 (char *) &slaves[(i + 1) % SLAVES].pid,
792 sizeof slaves[0].pid);
793
794 master = 0;
795 }
796
797 void
798 killall(void)
799 {
800 int i;
801
802 for (i = 0; i < SLAVES; i++)
803 if (slaves[i].pid > 0) {
804 (void) kill(slaves[i].pid, SIGKILL);
805 slaves[i].sent = 0;
806 }
807 }
808
809 /*
810 * Synchronization - each process has a lockfile, and shares file
811 * descriptors to the following process's lockfile. When our write
812 * completes, we release our lock on the following process's lock-
813 * file, allowing the following process to lock it and proceed. We
814 * get the lock back for the next cycle by swapping descriptors.
815 */
816 static void
817 doslave(int cmd, int slave_number __unused)
818 {
819 int nread, nextslave, size, wrote, eot_count, werror;
820 sigset_t nsigset, osigset;
821
822 wrote = 0;
823 /*
824 * Need our own seek pointer.
825 */
826 (void) close(diskfd);
827 if ((diskfd = open(disk_dev, O_RDONLY)) < 0)
828 quit("slave couldn't reopen disk: %s\n", strerror(errno));
829
830 /*
831 * Need the pid of the next slave in the loop...
832 */
833 if ((nread = atomic_read(cmd, (char *)&nextslave, sizeof nextslave))
834 != sizeof nextslave) {
835 quit("master/slave protocol botched - didn't get pid of next slave.\n");
836 }
837
838 /*
839 * Get list of blocks to dump, read the blocks into tape buffer
840 */
841 while ((nread = atomic_read(cmd, (char *)slp->req, reqsiz)) == reqsiz) {
842 struct req *p = slp->req;
843
844 for (trecno = 0; trecno < ntrec;
845 trecno += p->count, p += p->count) {
846 if (p->dblk) {
847 bread(p->dblk, slp->tblock[trecno],
848 p->count * TP_BSIZE);
849 } else {
850 if (p->count != 1 || atomic_read(cmd,
851 (char *)slp->tblock[trecno],
852 TP_BSIZE) != TP_BSIZE)
853 quit("master/slave protocol botched.\n");
854 }
855 }
856
857 sigemptyset(&nsigset);
858 sigaddset(&nsigset, SIGUSR2);
859 sigprocmask(SIG_BLOCK, &nsigset, &osigset);
860 while (!caught)
861 sigsuspend(&osigset);
862 caught = 0;
863 sigprocmask(SIG_SETMASK, &osigset, NULL);
864
865 /* Try to write the data... */
866 eot_count = 0;
867 size = 0;
868 werror = 0;
869
870 while (eot_count < 10 && size < writesize) {
871 #ifdef RDUMP
872 if (host)
873 wrote = rmtwrite(slp->tblock[0]+size,
874 writesize-size);
875 else
876 #endif
877 wrote = write(tapefd, slp->tblock[0]+size,
878 writesize-size);
879 werror = errno;
880 #ifdef WRITEDEBUG
881 fprintf(stderr, "slave %d wrote %d werror %d\n",
882 slave_number, wrote, werror);
883 #endif
884 if (wrote < 0)
885 break;
886 if (wrote == 0)
887 eot_count++;
888 size += wrote;
889 }
890
891 #ifdef WRITEDEBUG
892 if (size != writesize)
893 fprintf(stderr,
894 "slave %d only wrote %d out of %d bytes and gave up.\n",
895 slave_number, size, writesize);
896 #endif
897
898 /*
899 * Handle ENOSPC as an EOT condition.
900 */
901 if (wrote < 0 && werror == ENOSPC) {
902 wrote = 0;
903 eot_count++;
904 }
905
906 if (eot_count > 0)
907 size = 0;
908
909 if (wrote < 0) {
910 (void) kill(master, SIGUSR1);
911 sigemptyset(&nsigset);
912 for (;;)
913 sigsuspend(&nsigset);
914 } else {
915 /*
916 * pass size of write back to master
917 * (for EOT handling)
918 */
919 (void) atomic_write(cmd, (char *)&size, sizeof size);
920 }
921
922 /*
923 * If partial write, don't want next slave to go.
924 * Also jolts him awake.
925 */
926 (void) kill(nextslave, SIGUSR2);
927 }
928 printcachestats();
929 if (nread != 0)
930 quit("error reading command pipe: %s\n", strerror(errno));
931 }
932
933 /*
934 * Since a read from a pipe may not return all we asked for,
935 * loop until the count is satisfied (or error).
936 */
937 static ssize_t
938 atomic_read(int fd, char *buf, int count)
939 {
940 ssize_t got, need = count;
941
942 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
943 buf += got;
944 return (got < 0 ? got : count - need);
945 }
946
947 /*
948 * Since a write may not write all we ask if we get a signal,
949 * loop until the count is satisfied (or error).
950 */
951 static ssize_t
952 atomic_write(int fd, char *buf, int count)
953 {
954 ssize_t got, need = count;
955
956 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
957 buf += got;
958 return (got < 0 ? got : count - need);
959 }
960