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 * @(#)tape.c 8.4 (Berkeley) 5/1/95 34 * $FreeBSD: src/sbin/dump/tape.c,v 1.12.2.3 2002/02/23 22:32:51 iedowse Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/socket.h> 39 #include <sys/time.h> 40 #include <sys/wait.h> 41 #include <sys/stat.h> 42 #ifdef sunos 43 #include <sys/vnode.h> 44 45 #include <ufs/fs.h> 46 #include <ufs/inode.h> 47 #else 48 #include <vfs/ufs/dinode.h> 49 #include <vfs/ufs/fs.h> 50 #endif 51 52 #include <protocols/dumprestore.h> 53 54 #include <errno.h> 55 #include <fcntl.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 static int writesize; /* size of malloc()ed buffer for tape */ 66 static long lastspclrec = -1; /* tape block number of last written header */ 67 static int trecno = 0; /* next record to write in current block */ 68 static long blocksthisvol; /* number of blocks on current output file */ 69 static const char *nexttape; 70 static int tapeno = 0; /* current tape number */ 71 72 static int atomic_read(int, void *, int); 73 static int atomic_write(int, const void *, int); 74 #ifdef WRITEDEBUG 75 static void doslave(int, int); 76 #else 77 static void doslave(int); 78 #endif 79 static void enslave(void); 80 static void flushtape(void); 81 static void killall(void); 82 static void rollforward(void); 83 84 /* 85 * Concurrent dump mods (Caltech) - disk block reading and tape writing 86 * are exported to several slave processes. While one slave writes the 87 * tape, the others read disk blocks; they pass control of the tape in 88 * a ring via signals. The parent process traverses the filesystem and 89 * sends writeheader()'s and lists of daddr's to the slaves via pipes. 90 * The following structure defines the instruction packets sent to slaves. 91 */ 92 struct req { 93 daddr_t dblk; 94 int count; 95 }; 96 int reqsiz; 97 98 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ 99 struct slave { 100 int tapea; /* header number at start of this chunk */ 101 int count; /* count to next header (used for TS_TAPE */ 102 /* after EOT) */ 103 int inode; /* inode that we are currently dealing with */ 104 int fd; /* FD for this slave */ 105 int pid; /* PID for this slave */ 106 int sent; /* 1 == we've sent this slave requests */ 107 int firstrec; /* record number of this block */ 108 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */ 109 struct req *req; /* buffer for requests */ 110 } slaves[SLAVES+1]; 111 struct slave *slp; 112 113 char (*nextblock)[TP_BSIZE]; 114 115 int master; /* pid of master, for sending error signals */ 116 int tenths; /* length of tape used per block written */ 117 static int caught; /* have we caught the signal to proceed? */ 118 static int ready; /* have we reached the lock point without having */ 119 /* received the SIGUSR2 signal from the prev slave? */ 120 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */ 121 /* SIGUSR2 arrives from the previous slave */ 122 123 int 124 alloctape(void) 125 { 126 int pgoff = getpagesize() - 1; 127 char *buf; 128 int i; 129 130 writesize = ntrec * TP_BSIZE; 131 reqsiz = (ntrec + 1) * sizeof(struct req); 132 /* 133 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode 134 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require 135 * repositioning after stopping, i.e, streaming mode, where the gap is 136 * variable, 0.30" to 0.45". The gap is maximal when the tape stops. 137 */ 138 if (blocksperfile == 0 && !unlimited) 139 tenths = writesize / density + 140 (cartridge ? 16 : density == 625 ? 5 : 8); 141 /* 142 * Allocate tape buffer contiguous with the array of instruction 143 * packets, so flushtape() can write them together with one write(). 144 * Align tape buffer on page boundary to speed up tape write(). 145 */ 146 for (i = 0; i <= SLAVES; i++) { 147 buf = (char *) 148 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE)); 149 if (buf == NULL) 150 return(0); 151 slaves[i].tblock = (char (*)[TP_BSIZE]) 152 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff); 153 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1; 154 } 155 slp = &slaves[0]; 156 slp->count = 1; 157 slp->tapea = 0; 158 slp->firstrec = 0; 159 nextblock = slp->tblock; 160 return(1); 161 } 162 163 void 164 writerec(const void *dp, int isspcl) 165 { 166 167 slp->req[trecno].dblk = (daddr_t)0; 168 slp->req[trecno].count = 1; 169 bcopy(dp, *(nextblock)++, sizeof (union u_spcl)); 170 if (isspcl) 171 lastspclrec = spcl.c_tapea; 172 trecno++; 173 spcl.c_tapea++; 174 if (trecno >= ntrec) 175 flushtape(); 176 } 177 178 void 179 dumpblock(daddr_t blkno, int size) 180 { 181 int avail, tpblks, 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 static 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 static 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 long 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, 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, &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 &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, &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 close(slaves[f].fd); 334 } 335 while (wait(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 close(tapefd); 354 return; 355 } 356 close(tapefd); 357 while ((f = open(tape, 0)) < 0) 358 sleep (10); 359 close(f); 360 } 361 362 void 363 close_rewind(void) 364 { 365 time_t tstart_changevol, tend_changevol; 366 367 trewind(); 368 if (nexttape) 369 return; 370 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 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, savedtapea, got; 391 union u_spcl *ntb, *otb; 392 tslp = &slaves[SLAVES]; 393 ntb = (union u_spcl *)tslp->tblock[1]; 394 395 /* 396 * Each of the N slaves should have requests that need to 397 * be replayed on the next tape. Use the extra slave buffers 398 * (slaves[SLAVES]) to construct request lists to be sent to 399 * each slave in turn. 400 */ 401 for (i = 0; i < SLAVES; i++) { 402 q = &tslp->req[1]; 403 otb = (union u_spcl *)slp->tblock; 404 405 /* 406 * For each request in the current slave, copy it to tslp. 407 */ 408 409 prev = NULL; 410 for (p = slp->req; p->count > 0; p += p->count) { 411 *q = *p; 412 if (p->dblk == 0) 413 *ntb++ = *otb++; /* copy the datablock also */ 414 prev = q; 415 q += q->count; 416 } 417 if (prev == NULL) 418 quit("rollforward: protocol botch"); 419 if (prev->dblk != 0) 420 prev->count -= 1; 421 else 422 ntb--; 423 q -= 1; 424 q->count = 0; 425 q = &tslp->req[0]; 426 if (i == 0) { 427 q->dblk = 0; 428 q->count = 1; 429 trecno = 0; 430 nextblock = tslp->tblock; 431 savedtapea = spcl.c_tapea; 432 spcl.c_tapea = slp->tapea; 433 startnewtape(0); 434 spcl.c_tapea = savedtapea; 435 lastspclrec = savedtapea - 1; 436 } 437 size = (char *)ntb - (char *)q; 438 if (atomic_write(slp->fd, q, size) != size) { 439 perror(" DUMP: error writing command pipe"); 440 dumpabort(0); 441 } 442 slp->sent = 1; 443 if (++slp >= &slaves[SLAVES]) 444 slp = &slaves[0]; 445 446 q->count = 1; 447 448 if (prev->dblk != 0) { 449 /* 450 * If the last one was a disk block, make the 451 * first of this one be the last bit of that disk 452 * block... 453 */ 454 q->dblk = prev->dblk + 455 prev->count * (TP_BSIZE / DEV_BSIZE); 456 ntb = (union u_spcl *)tslp->tblock; 457 } else { 458 /* 459 * It wasn't a disk block. Copy the data to its 460 * new location in the buffer. 461 */ 462 q->dblk = 0; 463 *((union u_spcl *)tslp->tblock) = *ntb; 464 ntb = (union u_spcl *)tslp->tblock[1]; 465 } 466 } 467 slp->req[0] = *q; 468 nextblock = slp->tblock; 469 if (q->dblk == 0) 470 nextblock++; 471 trecno = 1; 472 473 /* 474 * Clear the first slaves' response. One hopes that it 475 * worked ok, otherwise the tape is much too short! 476 */ 477 if (slp->sent) { 478 if (atomic_read(slp->fd, &got, sizeof got) 479 != sizeof got) { 480 perror(" DUMP: error reading command pipe in master"); 481 dumpabort(0); 482 } 483 slp->sent = 0; 484 485 if (got != writesize) { 486 quit("EOT detected at start of the tape!\n"); 487 } 488 } 489 } 490 491 /* 492 * We implement taking and restoring checkpoints on the tape level. 493 * When each tape is opened, a new process is created by forking; this 494 * saves all of the necessary context in the parent. The child 495 * continues the dump; the parent waits around, saving the context. 496 * If the child returns X_REWRITE, then it had problems writing that tape; 497 * this causes the parent to fork again, duplicating the context, and 498 * everything continues as if nothing had happened. 499 */ 500 void 501 startnewtape(int top) 502 { 503 int parentpid; 504 int childpid; 505 int status; 506 int wait_pid; 507 char *p; 508 #ifdef sunos 509 void (*interrupt_save)(); 510 #else 511 sig_t interrupt_save; 512 #endif 513 514 interrupt_save = signal(SIGINT, SIG_IGN); 515 parentpid = getpid(); 516 517 restore_check_point: 518 signal(SIGINT, interrupt_save); 519 /* 520 * All signals are inherited... 521 */ 522 setproctitle(NULL); /* Restore the proctitle. */ 523 childpid = fork(); 524 if (childpid < 0) { 525 msg("Context save fork fails in parent %d\n", parentpid); 526 Exit(X_ABORT); 527 } 528 if (childpid != 0) { 529 /* 530 * PARENT: 531 * save the context by waiting 532 * until the child doing all of the work returns. 533 * don't catch the interrupt 534 */ 535 signal(SIGINT, SIG_IGN); 536 #ifdef TDEBUG 537 msg("Tape: %d; parent process: %d child process %d\n", 538 tapeno+1, parentpid, childpid); 539 #endif /* TDEBUG */ 540 while ((wait_pid = wait(&status)) != childpid) 541 msg("Parent %d waiting for child %d has another child %d return\n", 542 parentpid, childpid, wait_pid); 543 if (status & 0xFF) { 544 msg("Child %d returns LOB status %o\n", 545 childpid, status&0xFF); 546 } 547 status = (status >> 8) & 0xFF; 548 #ifdef TDEBUG 549 switch(status) { 550 case X_FINOK: 551 msg("Child %d finishes X_FINOK\n", childpid); 552 break; 553 case X_ABORT: 554 msg("Child %d finishes X_ABORT\n", childpid); 555 break; 556 case X_REWRITE: 557 msg("Child %d finishes X_REWRITE\n", childpid); 558 break; 559 default: 560 msg("Child %d finishes unknown %d\n", 561 childpid, status); 562 break; 563 } 564 #endif /* TDEBUG */ 565 switch(status) { 566 case X_FINOK: 567 Exit(X_FINOK); 568 case X_ABORT: 569 Exit(X_ABORT); 570 case X_REWRITE: 571 goto restore_check_point; 572 default: 573 msg("Bad return code from dump: %d\n", status); 574 Exit(X_ABORT); 575 } 576 /*NOTREACHED*/ 577 } else { /* we are the child; just continue */ 578 #ifdef TDEBUG 579 sleep(4); /* allow time for parent's message to get out */ 580 msg("Child on Tape %d has parent %d, my pid = %d\n", 581 tapeno+1, parentpid, getpid()); 582 #endif /* TDEBUG */ 583 /* 584 * If we have a name like "/dev/rmt0,/dev/rmt1", 585 * use the name before the comma first, and save 586 * the remaining names for subsequent volumes. 587 */ 588 tapeno++; /* current tape sequence */ 589 if (nexttape || strchr(tape, ',')) { 590 if (nexttape && *nexttape) 591 tape = nexttape; 592 if ((p = strchr(tape, ',')) != NULL) { 593 *p = '\0'; 594 nexttape = p + 1; 595 } else 596 nexttape = NULL; 597 msg("Dumping volume %d on %s\n", tapeno, tape); 598 } 599 #ifdef RDUMP 600 while ((tapefd = (host ? rmtopen(tape, 2) : 601 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0) 602 #else 603 while ((tapefd = (pipeout ? 1 : 604 open(tape, O_WRONLY|O_CREAT, 0666))) < 0) 605 #endif 606 { 607 msg("Cannot open output \"%s\".\n", tape); 608 if (!query("Do you want to retry the open?")) 609 dumpabort(0); 610 } 611 612 enslave(); /* Share open tape file descriptor with slaves */ 613 signal(SIGINFO, infosch); 614 615 asize = 0; 616 blocksthisvol = 0; 617 if (top) 618 newtape++; /* new tape signal */ 619 spcl.c_count = slp->count; 620 /* 621 * measure firstrec in TP_BSIZE units since restore doesn't 622 * know the correct ntrec value... 623 */ 624 spcl.c_firstrec = slp->firstrec; 625 spcl.c_volume++; 626 spcl.c_type = TS_TAPE; 627 spcl.c_flags |= DR_NEWHEADER; 628 writeheader((ufs1_ino_t)slp->inode); 629 spcl.c_flags &=~ DR_NEWHEADER; 630 if (tapeno > 1) 631 msg("Volume %d begins with blocks from inode %d\n", 632 tapeno, slp->inode); 633 } 634 } 635 636 void 637 dumpabort(int signo __unused) 638 { 639 640 if (master != 0 && master != getpid()) 641 /* Signals master to call dumpabort */ 642 kill(master, SIGTERM); 643 else { 644 killall(); 645 msg("The ENTIRE dump is aborted.\n"); 646 } 647 #ifdef RDUMP 648 rmtclose(); 649 #endif 650 Exit(X_ABORT); 651 } 652 653 void 654 Exit(int status) 655 { 656 657 #ifdef TDEBUG 658 msg("pid = %d exits with status %d\n", getpid(), status); 659 #endif /* TDEBUG */ 660 exit(status); 661 } 662 663 /* 664 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves. 665 */ 666 static void 667 proceed(int signo __unused) 668 { 669 670 if (ready) 671 longjmp(jmpbuf, 1); 672 caught++; 673 } 674 675 static void 676 enslave(void) 677 { 678 int cmd[2]; 679 int i, j; 680 681 master = getpid(); 682 683 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ 684 signal(SIGPIPE, sigpipe); 685 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ 686 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */ 687 688 for (i = 0; i < SLAVES; i++) { 689 if (i == slp - &slaves[0]) { 690 caught = 1; 691 } else { 692 caught = 0; 693 } 694 695 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 || 696 (slaves[i].pid = fork()) < 0) 697 quit("too many slaves, %d (recompile smaller): %s\n", 698 i, strerror(errno)); 699 700 slaves[i].fd = cmd[1]; 701 slaves[i].sent = 0; 702 if (slaves[i].pid == 0) { /* Slave starts up here */ 703 for (j = 0; j <= i; j++) 704 close(slaves[j].fd); 705 signal(SIGINT, SIG_IGN); /* Master handles this */ 706 #ifdef WRITEDEBUG 707 doslave(cmd[0], i); 708 #else 709 doslave(cmd[0]); 710 #endif 711 Exit(X_FINOK); 712 } 713 } 714 715 for (i = 0; i < SLAVES; i++) 716 atomic_write(slaves[i].fd, &slaves[(i + 1) % SLAVES].pid, 717 sizeof slaves[0].pid); 718 719 master = 0; 720 } 721 722 void 723 killall(void) 724 { 725 int i; 726 727 for (i = 0; i < SLAVES; i++) 728 if (slaves[i].pid > 0) { 729 kill(slaves[i].pid, SIGKILL); 730 slaves[i].sent = 0; 731 } 732 } 733 734 /* 735 * Synchronization - each process has a lockfile, and shares file 736 * descriptors to the following process's lockfile. When our write 737 * completes, we release our lock on the following process's lock- 738 * file, allowing the following process to lock it and proceed. We 739 * get the lock back for the next cycle by swapping descriptors. 740 */ 741 static void 742 #ifdef WRITEDEBUG 743 doslave(int cmd, int slave_number) 744 #else 745 doslave(int cmd) 746 #endif 747 { 748 int nread; 749 int nextslave, size, wrote, eot_count; 750 751 /* 752 * Need our own seek pointer. 753 */ 754 close(diskfd); 755 if ((diskfd = open(disk, O_RDONLY)) < 0) 756 quit("slave couldn't reopen disk: %s\n", strerror(errno)); 757 758 /* 759 * Need the pid of the next slave in the loop... 760 */ 761 if ((nread = atomic_read(cmd, &nextslave, sizeof nextslave)) 762 != sizeof nextslave) { 763 quit("master/slave protocol botched - didn't get pid of next slave.\n"); 764 } 765 766 /* 767 * Get list of blocks to dump, read the blocks into tape buffer 768 */ 769 while ((nread = atomic_read(cmd, slp->req, reqsiz)) == reqsiz) { 770 struct req *p = slp->req; 771 772 for (trecno = 0; trecno < ntrec; 773 trecno += p->count, p += p->count) { 774 if (p->dblk) { 775 bread(p->dblk, slp->tblock[trecno], 776 p->count * TP_BSIZE); 777 } else { 778 if (p->count != 1 || 779 atomic_read(cmd, slp->tblock[trecno], 780 TP_BSIZE) != TP_BSIZE) 781 quit("master/slave protocol botched.\n"); 782 } 783 } 784 if (setjmp(jmpbuf) == 0) { 785 ready = 1; 786 if (!caught) 787 pause(); 788 } 789 ready = 0; 790 caught = 0; 791 792 /* Try to write the data... */ 793 eot_count = 0; 794 size = 0; 795 wrote = 0; 796 797 while (eot_count < 10 && size < writesize) { 798 #ifdef RDUMP 799 if (host) 800 wrote = rmtwrite(slp->tblock[0]+size, 801 writesize-size); 802 else 803 #endif 804 wrote = write(tapefd, slp->tblock[0]+size, 805 writesize-size); 806 #ifdef WRITEDEBUG 807 printf("slave %d wrote %d\n", slave_number, wrote); 808 #endif 809 if (wrote < 0) 810 break; 811 if (wrote == 0) 812 eot_count++; 813 size += wrote; 814 } 815 816 #ifdef WRITEDEBUG 817 if (size != writesize) 818 printf("slave %d only wrote %d out of %d bytes and gave up.\n", 819 slave_number, size, writesize); 820 #endif 821 822 /* 823 * Handle ENOSPC as an EOT condition. 824 */ 825 if (wrote < 0 && errno == ENOSPC) { 826 wrote = 0; 827 eot_count++; 828 } 829 830 if (eot_count > 0) 831 size = 0; 832 833 if (wrote < 0) { 834 kill(master, SIGUSR1); 835 for (;;) 836 sigpause(0); 837 } else { 838 /* 839 * pass size of write back to master 840 * (for EOT handling) 841 */ 842 atomic_write(cmd, &size, sizeof size); 843 } 844 845 /* 846 * If partial write, don't want next slave to go. 847 * Also jolts him awake. 848 */ 849 kill(nextslave, SIGUSR2); 850 } 851 if (nread != 0) 852 quit("error reading command pipe: %s\n", strerror(errno)); 853 } 854 855 static int 856 atomic_read(int fd, void *buf, int count) 857 { 858 int got, need = count; 859 860 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0) 861 buf = (uint8_t *)buf + got; 862 return (got < 0 ? got : count - need); 863 } 864 865 static int 866 atomic_write(int fd, const void *buf, int count) 867 { 868 int got, need = count; 869 870 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0) 871 buf = (const uint8_t *)buf + got; 872 return (got < 0 ? got : count - need); 873 } 874