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