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