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