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