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