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