1 /* $OpenBSD: tape.c,v 1.46 2020/10/01 07:58:54 otto 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 int writesize; /* size of malloc()ed buffer for tape */ 59 int64_t lastspclrec = -1; /* tape block number of last written header */ 60 int trecno = 0; /* next record to write in current block */ 61 extern int64_t blocksperfile; /* number of blocks per output file */ 62 int64_t blocksthisvol; /* number of blocks on current output file */ 63 extern int ntrec; /* blocking factor on tape */ 64 extern int cartridge; 65 extern char *host; 66 char *nexttape; 67 68 static ssize_t atomic(ssize_t (*)(int, void *, size_t), int, char *, int); 69 static void doslave(int, int); 70 static void enslave(void); 71 static void flushtape(void); 72 static void killall(void); 73 static void rollforward(void); 74 75 void tperror(int signo); 76 void sigpipe(int signo); 77 void proceed(int signo); 78 79 /* 80 * Concurrent dump mods (Caltech) - disk block reading and tape writing 81 * are exported to several slave processes. While one slave writes the 82 * tape, the others read disk blocks; they pass control of the tape in 83 * a ring via signals. The parent process traverses the filesystem and 84 * sends writeheader()'s and lists of daddr's to the slaves via pipes. 85 * The following structure defines the instruction packets sent to slaves. 86 */ 87 struct req { 88 daddr_t dblk; 89 int count; 90 }; 91 int reqsiz; 92 93 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 94 struct slave { 95 int64_t tapea; /* header number at start of this chunk */ 96 int64_t firstrec; /* record number of this block */ 97 int count; /* count to next header (used for TS_TAPE */ 98 /* after EOT) */ 99 int inode; /* inode that we are currently dealing with */ 100 int fd; /* FD for this slave */ 101 pid_t pid; /* PID for this slave */ 102 int sent; /* 1 == we've sent this slave requests */ 103 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */ 104 struct req *req; /* buffer for requests */ 105 } slaves[SLAVES+1]; 106 struct slave *slp; 107 108 char (*nextblock)[TP_BSIZE]; 109 110 static time_t tstart_volume; /* time of volume start */ 111 static int64_t tapea_volume; /* value of spcl.c_tapea at volume start */ 112 113 pid_t master; /* pid of master, for sending error signals */ 114 int tenths; /* length of tape used per block written */ 115 static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */ 116 117 int 118 alloctape(void) 119 { 120 int pgoff = getpagesize() - 1; 121 char *buf; 122 int i; 123 124 writesize = ntrec * TP_BSIZE; 125 reqsiz = (ntrec + 1) * sizeof(struct req); 126 /* 127 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 128 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 129 * repositioning after stopping, i.e, streaming mode, where the gap is 130 * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 131 */ 132 if (blocksperfile == 0 && !unlimited) 133 tenths = writesize / density + 134 (cartridge ? 16 : density == 625 ? 5 : 8); 135 /* 136 * Allocate tape buffer contiguous with the array of instruction 137 * packets, so flushtape() can write them together with one write(). 138 * Align tape buffer on page boundary to speed up tape write(). 139 */ 140 for (i = 0; i <= SLAVES; i++) { 141 buf = malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE)); 142 if (buf == NULL) 143 return(0); 144 slaves[i].tblock = (char (*)[TP_BSIZE]) 145 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff); 146 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1; 147 } 148 slp = &slaves[0]; 149 slp->count = 1; 150 slp->tapea = 0; 151 slp->firstrec = 0; 152 nextblock = slp->tblock; 153 return(1); 154 } 155 156 void 157 writerec(char *dp, int isspcl) 158 { 159 160 slp->req[trecno].dblk = 0; 161 slp->req[trecno].count = 1; 162 *(union u_spcl *)(*(nextblock)++) = *(union u_spcl *)dp; 163 if (isspcl) 164 lastspclrec = spcl.c_tapea; 165 trecno++; 166 spcl.c_tapea++; 167 if (trecno >= ntrec) 168 flushtape(); 169 } 170 171 void 172 dumpblock(daddr_t blkno, int size) 173 { 174 int avail, tpblks; 175 daddr_t dblkno; 176 177 dblkno = fsbtodb(sblock, blkno); 178 tpblks = size >> tp_bshift; 179 while ((avail = MINIMUM(tpblks, ntrec - trecno)) > 0) { 180 slp->req[trecno].dblk = dblkno; 181 slp->req[trecno].count = avail; 182 trecno += avail; 183 spcl.c_tapea += avail; 184 if (trecno >= ntrec) 185 flushtape(); 186 dblkno += avail << (tp_bshift - (ffs(DEV_BSIZE) - 1)); 187 tpblks -= avail; 188 } 189 } 190 191 int nogripe = 0; 192 193 /* ARGSUSED */ 194 void 195 tperror(int signo) 196 { 197 /* XXX - signal races */ 198 199 if (pipeout) { 200 msg("write error on %s\n", tape); 201 quit("Cannot recover\n"); 202 /* NOTREACHED */ 203 } 204 msg("write error %lld blocks into volume %d\n", 205 (long long)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 /* ARGSUSED */ 218 void 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 230 do_stats(void) 231 { 232 time_t tnow, ttaken; 233 int64_t blocks; 234 235 (void)time(&tnow); 236 ttaken = tnow - tstart_volume; 237 blocks = spcl.c_tapea - tapea_volume; 238 msg("Volume %d completed at: %s", tapeno, ctime(&tnow)); 239 if (ttaken > 0) { 240 msg("Volume %d took %lld:%02lld:%02lld\n", tapeno, 241 (long long)ttaken / 3600, ((long long)ttaken % 3600) / 60, 242 (long long)ttaken % 60); 243 blocks /= ttaken; 244 msg("Volume %d transfer rate: %lld KB/s\n", tapeno, blocks); 245 xferrate += blocks; 246 } 247 return(tnow); 248 } 249 250 /* 251 * statussig -- 252 * information message upon receipt of SIGINFO 253 * (derived from optr.c::timeest()) 254 * XXX not safe 255 */ 256 /* ARGSUSED */ 257 void 258 statussig(int signo) 259 { 260 time_t tnow, deltat; 261 int save_errno = errno; 262 263 if (blockswritten < 500) 264 return; 265 (void) time(&tnow); 266 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing)) 267 / blockswritten * tapesize; 268 /* XXX not safe due to floating point printf */ 269 dprintf(STDERR_FILENO, 270 "dump: %s %3.2f%% done at %lld KB/s, finished in %d:%02d\n", 271 tape, (blockswritten * 100.0) / tapesize, 272 (spcl.c_tapea - tapea_volume) / (tnow - tstart_volume), 273 (int)(deltat / 3600), (int)((deltat % 3600) / 60)); 274 errno = save_errno; 275 } 276 277 static void 278 flushtape(void) 279 { 280 int i, blks, got; 281 int64_t lastfirstrec; 282 283 int siz = (char *)nextblock - (char *)slp->req; 284 285 slp->req[trecno].count = 0; /* Sentinel */ 286 287 if (atomic((ssize_t (*)(int, void *, size_t))write, slp->fd, 288 (char *)slp->req, siz) != siz) 289 quit("error writing command pipe: %s\n", strerror(errno)); 290 slp->sent = 1; /* we sent a request, read the response later */ 291 292 lastfirstrec = slp->firstrec; 293 294 if (++slp >= &slaves[SLAVES]) 295 slp = &slaves[0]; 296 297 /* Read results back from next slave */ 298 if (slp->sent) { 299 if (atomic(read, slp->fd, (char *)&got, sizeof(got)) 300 != sizeof(got)) { 301 perror(" DUMP: error reading command pipe in master"); 302 dumpabort(0); 303 } 304 slp->sent = 0; 305 306 /* Check for end of tape */ 307 if (got < writesize) { 308 msg("End of tape detected\n"); 309 310 /* 311 * Drain the results, don't care what the values were. 312 * If we read them here then trewind won't... 313 */ 314 for (i = 0; i < SLAVES; i++) { 315 if (slaves[i].sent) { 316 if (atomic(read, slaves[i].fd, 317 (char *)&got, sizeof(got)) 318 != sizeof(got)) { 319 perror(" DUMP: error reading command pipe in master"); 320 dumpabort(0); 321 } 322 slaves[i].sent = 0; 323 } 324 } 325 326 close_rewind(); 327 rollforward(); 328 return; 329 } 330 } 331 332 blks = 0; 333 if (spcl.c_type != TS_END && spcl.c_type != TS_CLRI && 334 spcl.c_type != TS_BITS) { 335 if (spcl.c_count > TP_NINDIR) 336 quit("c_count too large\n"); 337 for (i = 0; i < spcl.c_count; i++) 338 if (spcl.c_addr[i] != 0) 339 blks++; 340 } 341 slp->count = lastspclrec + blks + 1 - spcl.c_tapea; 342 slp->tapea = spcl.c_tapea; 343 slp->firstrec = lastfirstrec + ntrec; 344 slp->inode = curino; 345 nextblock = slp->tblock; 346 trecno = 0; 347 asize += tenths; 348 blockswritten += ntrec; 349 blocksthisvol += ntrec; 350 if (!pipeout && !unlimited && (blocksperfile ? 351 (blocksthisvol >= blocksperfile) : (asize > tsize))) { 352 close_rewind(); 353 startnewtape(0); 354 } 355 timeest(); 356 } 357 358 void 359 trewind(void) 360 { 361 struct stat sb; 362 int f, got; 363 364 for (f = 0; f < SLAVES; f++) { 365 /* 366 * Drain the results, but unlike EOT we DO (or should) care 367 * what the return values were, since if we detect EOT after 368 * we think we've written the last blocks to the tape anyway, 369 * we have to replay those blocks with rollforward. 370 * 371 * fixme: punt for now. 372 */ 373 if (slaves[f].sent) { 374 if (atomic(read, slaves[f].fd, (char *)&got, sizeof(got)) 375 != sizeof(got)) { 376 perror(" DUMP: error reading command pipe in master"); 377 dumpabort(0); 378 } 379 slaves[f].sent = 0; 380 if (got != writesize) { 381 msg("EOT detected in last 2 tape records!\n"); 382 msg("Use a longer tape, decrease the size estimate\n"); 383 quit("or use no size estimate at all.\n"); 384 } 385 } 386 (void) close(slaves[f].fd); 387 } 388 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */ 389 /* void */; 390 391 if (pipeout) 392 return; 393 394 msg("Closing %s\n", tape); 395 396 #ifdef RDUMP 397 if (host) { 398 rmtclose(); 399 while (rmtopen(tape, O_RDONLY) < 0) 400 sleep(10); 401 rmtclose(); 402 return; 403 } 404 #endif 405 /* 406 * st(4) says: "Bit 1 of the minor number specifies whether an eject is 407 * attempted when the device is closed. When it is set, the device 408 * will attempt to eject its media on close ...". 409 * 410 * If the tape has been ejected, looping on open() will generate 'Media 411 * not present' errors until a tape is loaded. Once loaded the tape 412 * will be immediately ejected as a result of the second close(). 413 * 414 * So if the tape will be ejected, just close and return. 415 */ 416 if ((fstat(tapefd, &sb) == 0) && (minor(sb.st_rdev) & 0x02)) { 417 (void) close(tapefd); 418 return; 419 } 420 421 (void) close(tapefd); 422 while ((f = open(tape, O_RDONLY)) == -1) 423 sleep (10); 424 (void) close(f); 425 } 426 427 void 428 close_rewind(void) 429 { 430 trewind(); 431 (void)do_stats(); 432 if (nexttape) 433 return; 434 if (!nogripe) { 435 msg("Change Volumes: Mount volume #%d\n", tapeno+1); 436 broadcast("CHANGE DUMP VOLUMES!\7\7\n"); 437 } 438 while (!query("Is the new volume mounted and ready to go?")) 439 if (query("Do you want to abort?")) { 440 dumpabort(0); 441 /*NOTREACHED*/ 442 } 443 } 444 445 void 446 rollforward(void) 447 { 448 struct req *p, *q, *prev; 449 struct slave *tslp; 450 int i, size, got; 451 int64_t savedtapea; 452 union u_spcl *ntb, *otb; 453 tslp = &slaves[SLAVES]; 454 ntb = (union u_spcl *)tslp->tblock[1]; 455 456 /* 457 * Each of the N slaves should have requests that need to 458 * be replayed on the next tape. Use the extra slave buffers 459 * (slaves[SLAVES]) to construct request lists to be sent to 460 * each slave in turn. 461 */ 462 for (i = 0; i < SLAVES; i++) { 463 q = &tslp->req[1]; 464 otb = (union u_spcl *)slp->tblock; 465 466 /* 467 * For each request in the current slave, copy it to tslp. 468 */ 469 470 prev = NULL; 471 for (p = slp->req; p->count > 0; p += p->count) { 472 *q = *p; 473 if (p->dblk == 0) 474 *ntb++ = *otb++; /* copy the datablock also */ 475 prev = q; 476 q += q->count; 477 } 478 if (prev == NULL) 479 quit("rollforward: protocol botch\n"); 480 if (prev->dblk != 0) 481 prev->count -= 1; 482 else 483 ntb--; 484 q -= 1; 485 q->count = 0; 486 q = &tslp->req[0]; 487 if (i == 0) { 488 q->dblk = 0; 489 q->count = 1; 490 trecno = 0; 491 nextblock = tslp->tblock; 492 savedtapea = spcl.c_tapea; 493 spcl.c_tapea = slp->tapea; 494 startnewtape(0); 495 spcl.c_tapea = savedtapea; 496 lastspclrec = savedtapea - 1; 497 } 498 size = (char *)ntb - (char *)q; 499 if (atomic((ssize_t (*)(int, void *, size_t))write, 500 slp->fd, (char *)q, size) != size) { 501 perror(" DUMP: error writing command pipe"); 502 dumpabort(0); 503 } 504 slp->sent = 1; 505 if (++slp >= &slaves[SLAVES]) 506 slp = &slaves[0]; 507 508 q->count = 1; 509 510 if (prev->dblk != 0) { 511 /* 512 * If the last one was a disk block, make the 513 * first of this one be the last bit of that disk 514 * block... 515 */ 516 q->dblk = prev->dblk + 517 prev->count * (TP_BSIZE / DEV_BSIZE); 518 ntb = (union u_spcl *)tslp->tblock; 519 } else { 520 /* 521 * It wasn't a disk block. Copy the data to its 522 * new location in the buffer. 523 */ 524 q->dblk = 0; 525 *((union u_spcl *)tslp->tblock) = *ntb; 526 ntb = (union u_spcl *)tslp->tblock[1]; 527 } 528 } 529 slp->req[0] = *q; 530 nextblock = slp->tblock; 531 if (q->dblk == 0) 532 nextblock++; 533 trecno = 1; 534 535 /* 536 * Clear the first slaves' response. One hopes that it 537 * worked ok, otherwise the tape is much too short! 538 */ 539 if (slp->sent) { 540 if (atomic(read, slp->fd, (char *)&got, sizeof(got)) 541 != sizeof(got)) { 542 perror(" DUMP: error reading command pipe in master"); 543 dumpabort(0); 544 } 545 slp->sent = 0; 546 547 if (got != writesize) { 548 quit("EOT detected at start of the tape!\n"); 549 } 550 } 551 } 552 553 /* 554 * We implement taking and restoring checkpoints on the tape level. 555 * When each tape is opened, a new process is created by forking; this 556 * saves all of the necessary context in the parent. The child 557 * continues the dump; the parent waits around, saving the context. 558 * If the child returns X_REWRITE, then it had problems writing that tape; 559 * this causes the parent to fork again, duplicating the context, and 560 * everything continues as if nothing had happened. 561 */ 562 void 563 startnewtape(int top) 564 { 565 pid_t parentpid; 566 pid_t childpid; 567 int status; 568 pid_t waitingpid; 569 char *p; 570 sig_t interrupt_save; 571 572 interrupt_save = signal(SIGINT, SIG_IGN); 573 parentpid = getpid(); 574 tapea_volume = spcl.c_tapea; 575 (void)time(&tstart_volume); 576 577 restore_check_point: 578 (void)signal(SIGINT, interrupt_save); 579 /* 580 * All signals are inherited... 581 */ 582 childpid = fork(); 583 if (childpid == -1) { 584 msg("Context save fork fails in parent %d\n", parentpid); 585 Exit(X_ABORT); 586 } 587 if (childpid != 0) { 588 /* 589 * PARENT: 590 * save the context by waiting 591 * until the child doing all of the work returns. 592 * don't catch the interrupt 593 */ 594 signal(SIGINT, SIG_IGN); 595 #ifdef TDEBUG 596 msg("Tape: %d; parent process: %d child process %d\n", 597 tapeno+1, parentpid, childpid); 598 #endif /* TDEBUG */ 599 while ((waitingpid = wait(&status)) != childpid) 600 msg("Parent %d waiting for child %d has another child %d return\n", 601 parentpid, childpid, waitingpid); 602 if (status & 0xFF) { 603 msg("Child %d returns LOB status %o\n", 604 childpid, status&0xFF); 605 } 606 status = (status >> 8) & 0xFF; 607 #ifdef TDEBUG 608 switch(status) { 609 case X_FINOK: 610 msg("Child %d finishes X_FINOK\n", childpid); 611 break; 612 case X_ABORT: 613 msg("Child %d finishes X_ABORT\n", childpid); 614 break; 615 case X_REWRITE: 616 msg("Child %d finishes X_REWRITE\n", childpid); 617 break; 618 default: 619 msg("Child %d finishes unknown %d\n", 620 childpid, status); 621 break; 622 } 623 #endif /* TDEBUG */ 624 switch(status) { 625 case X_FINOK: 626 Exit(X_FINOK); 627 break; 628 case X_ABORT: 629 Exit(X_ABORT); 630 break; 631 case X_REWRITE: 632 goto restore_check_point; 633 default: 634 msg("Bad return code from dump: %d\n", status); 635 Exit(X_ABORT); 636 } 637 /*NOTREACHED*/ 638 } else { /* we are the child; just continue */ 639 #ifdef TDEBUG 640 sleep(4); /* allow time for parent's message to get out */ 641 msg("Child on Tape %d has parent %d, my pid = %d\n", 642 tapeno+1, parentpid, getpid()); 643 #endif /* TDEBUG */ 644 /* 645 * If we have a name like "/dev/rst0,/dev/rst1", 646 * use the name before the comma first, and save 647 * the remaining names for subsequent volumes. 648 */ 649 tapeno++; /* current tape sequence */ 650 if (nexttape || strchr(tape, ',')) { 651 if (nexttape && *nexttape) 652 tape = nexttape; 653 if ((p = strchr(tape, ',')) != NULL) { 654 *p = '\0'; 655 nexttape = p + 1; 656 } else 657 nexttape = NULL; 658 msg("Dumping volume %d on %s\n", tapeno, tape); 659 } 660 #ifdef RDUMP 661 while ((tapefd = (host ? rmtopen(tape, O_WRONLY|O_CREAT) : 662 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) == -1) 663 #else 664 while ((tapefd = (pipeout ? 1 : 665 open(tape, O_WRONLY|O_CREAT, 0666))) == -1) 666 #endif 667 { 668 msg("Cannot open output \"%s\".\n", tape); 669 if (!query("Do you want to retry the open?")) 670 dumpabort(0); 671 } 672 673 enslave(); /* Share open tape file descriptor with slaves */ 674 675 asize = 0; 676 blocksthisvol = 0; 677 if (top) 678 newtape++; /* new tape signal */ 679 spcl.c_count = slp->count; 680 /* 681 * measure firstrec in TP_BSIZE units since restore doesn't 682 * know the correct ntrec value... 683 */ 684 spcl.c_firstrec = slp->firstrec; 685 spcl.c_volume++; 686 spcl.c_type = TS_TAPE; 687 if (sblock->fs_magic != FS_UFS2_MAGIC) 688 spcl.c_flags |= DR_NEWHEADER; 689 writeheader((ino_t)slp->inode); 690 if (sblock->fs_magic != FS_UFS2_MAGIC) 691 spcl.c_flags &=~ DR_NEWHEADER; 692 msg("Volume %d started at: %s", tapeno, ctime(&tstart_volume)); 693 if (tapeno > 1) 694 msg("Volume %d begins with blocks from inode %llu\n", 695 tapeno, (unsigned long long)slp->inode); 696 } 697 } 698 699 /* ARGSUSED */ 700 void 701 dumpabort(int signo) 702 { 703 704 if (master != 0 && master != getpid()) 705 /* Signals master to call dumpabort */ 706 (void) kill(master, SIGTERM); 707 else { 708 killall(); 709 msg("The ENTIRE dump is aborted.\n"); 710 } 711 #ifdef RDUMP 712 rmtclose(); 713 #endif 714 Exit(X_ABORT); 715 } 716 717 __dead void 718 Exit(int status) 719 { 720 721 #ifdef TDEBUG 722 msg("pid = %d exits with status %d\n", getpid(), status); 723 #endif /* TDEBUG */ 724 exit(status); 725 } 726 727 /* 728 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves. 729 */ 730 /* ARGSUSED */ 731 void 732 proceed(int signo) 733 { 734 caught++; 735 } 736 737 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 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 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 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