| 1 | /* |
| 2 | * Copyright (c) 1980 Regents of the University of California. |
| 3 | * All rights reserved. The Berkeley software License Agreement |
| 4 | * specifies the terms and conditions for redistribution. |
| 5 | */ |
| 6 | |
| 7 | #ifndef lint |
| 8 | static char sccsid[] = "@(#)tape.c 5.9 (Berkeley) %G%"; |
| 9 | #endif not lint |
| 10 | |
| 11 | #include <sys/file.h> |
| 12 | #include "dump.h" |
| 13 | #include "pathnames.h" |
| 14 | |
| 15 | char (*tblock)[TP_BSIZE]; /* pointer to malloc()ed buffer for tape */ |
| 16 | int writesize; /* size of malloc()ed buffer for tape */ |
| 17 | long lastspclrec = -1; /* tape block number of last written header */ |
| 18 | int trecno = 0; /* next record to write in current block */ |
| 19 | extern int ntrec; /* blocking factor on tape */ |
| 20 | extern int cartridge; |
| 21 | extern int read(), write(); |
| 22 | #ifdef RDUMP |
| 23 | extern char *host; |
| 24 | #endif RDUMP |
| 25 | |
| 26 | /* |
| 27 | * Concurrent dump mods (Caltech) - disk block reading and tape writing |
| 28 | * are exported to several slave processes. While one slave writes the |
| 29 | * tape, the others read disk blocks; they pass control of the tape in |
| 30 | * a ring via flock(). The parent process traverses the filesystem and |
| 31 | * sends spclrec()'s and lists of daddr's to the slaves via pipes. |
| 32 | */ |
| 33 | struct req { /* instruction packets sent to slaves */ |
| 34 | daddr_t dblk; |
| 35 | int count; |
| 36 | } *req; |
| 37 | int reqsiz; |
| 38 | |
| 39 | #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */ |
| 40 | int slavefd[SLAVES]; /* pipes from master to each slave */ |
| 41 | int slavepid[SLAVES]; /* used by killall() */ |
| 42 | int rotor; /* next slave to be instructed */ |
| 43 | int master; /* pid of master, for sending error signals */ |
| 44 | int tenths; /* length of tape used per block written */ |
| 45 | |
| 46 | alloctape() |
| 47 | { |
| 48 | int pgoff = getpagesize() - 1; |
| 49 | |
| 50 | writesize = ntrec * TP_BSIZE; |
| 51 | reqsiz = ntrec * sizeof(struct req); |
| 52 | /* |
| 53 | * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode |
| 54 | * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require |
| 55 | * repositioning after stopping, i.e, streaming mode, where the gap is |
| 56 | * variable, 0.30" to 0.45". The gap is maximal when the tape stops. |
| 57 | */ |
| 58 | tenths = writesize/density + (cartridge ? 16 : density == 625 ? 5 : 8); |
| 59 | /* |
| 60 | * Allocate tape buffer contiguous with the array of instruction |
| 61 | * packets, so flusht() can write them together with one write(). |
| 62 | * Align tape buffer on page boundary to speed up tape write(). |
| 63 | */ |
| 64 | req = (struct req *)malloc(reqsiz + writesize + pgoff); |
| 65 | if (req == NULL) |
| 66 | return(0); |
| 67 | tblock = (char (*)[TP_BSIZE]) (((long)&req[ntrec] + pgoff) &~ pgoff); |
| 68 | req = (struct req *)tblock - ntrec; |
| 69 | return(1); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | taprec(dp) |
| 74 | char *dp; |
| 75 | { |
| 76 | req[trecno].dblk = (daddr_t)0; |
| 77 | req[trecno].count = 1; |
| 78 | *(union u_spcl *)(*tblock++) = *(union u_spcl *)dp; /* movc3 */ |
| 79 | lastspclrec = spcl.c_tapea; |
| 80 | trecno++; |
| 81 | spcl.c_tapea++; |
| 82 | if(trecno >= ntrec) |
| 83 | flusht(); |
| 84 | } |
| 85 | |
| 86 | dmpblk(blkno, size) |
| 87 | daddr_t blkno; |
| 88 | int size; |
| 89 | { |
| 90 | int avail, tpblks, dblkno; |
| 91 | |
| 92 | dblkno = fsbtodb(sblock, blkno); |
| 93 | tpblks = size / TP_BSIZE; |
| 94 | while ((avail = MIN(tpblks, ntrec - trecno)) > 0) { |
| 95 | req[trecno].dblk = dblkno; |
| 96 | req[trecno].count = avail; |
| 97 | trecno += avail; |
| 98 | spcl.c_tapea += avail; |
| 99 | if (trecno >= ntrec) |
| 100 | flusht(); |
| 101 | dblkno += avail * (TP_BSIZE / dev_bsize); |
| 102 | tpblks -= avail; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | int nogripe = 0; |
| 107 | |
| 108 | tperror() { |
| 109 | if (pipeout) { |
| 110 | msg("Tape write error on %s\n", tape); |
| 111 | msg("Cannot recover\n"); |
| 112 | dumpabort(); |
| 113 | /* NOTREACHED */ |
| 114 | } |
| 115 | msg("Tape write error %d feet into tape %d\n", asize/120L, tapeno); |
| 116 | broadcast("TAPE ERROR!\n"); |
| 117 | if (!query("Do you want to restart?")) |
| 118 | dumpabort(); |
| 119 | msg("This tape will rewind. After it is rewound,\n"); |
| 120 | msg("replace the faulty tape with a new one;\n"); |
| 121 | msg("this dump volume will be rewritten.\n"); |
| 122 | killall(); |
| 123 | nogripe = 1; |
| 124 | close_rewind(); |
| 125 | Exit(X_REWRITE); |
| 126 | } |
| 127 | |
| 128 | sigpipe() |
| 129 | { |
| 130 | |
| 131 | msg("Broken pipe\n"); |
| 132 | dumpabort(); |
| 133 | } |
| 134 | |
| 135 | #ifdef RDUMP |
| 136 | /* |
| 137 | * compatibility routine |
| 138 | */ |
| 139 | tflush(i) |
| 140 | int i; |
| 141 | { |
| 142 | |
| 143 | for (i = 0; i < ntrec; i++) |
| 144 | spclrec(); |
| 145 | } |
| 146 | #endif RDUMP |
| 147 | |
| 148 | flusht() |
| 149 | { |
| 150 | int siz = (char *)tblock - (char *)req; |
| 151 | |
| 152 | if (atomic(write, slavefd[rotor], req, siz) != siz) { |
| 153 | perror(" DUMP: error writing command pipe"); |
| 154 | dumpabort(); |
| 155 | } |
| 156 | if (++rotor >= SLAVES) rotor = 0; |
| 157 | tblock = (char (*)[TP_BSIZE]) &req[ntrec]; |
| 158 | trecno = 0; |
| 159 | asize += tenths; |
| 160 | blockswritten += ntrec; |
| 161 | if (!pipeout && asize > tsize) { |
| 162 | close_rewind(); |
| 163 | otape(); |
| 164 | } |
| 165 | timeest(); |
| 166 | } |
| 167 | |
| 168 | rewind() |
| 169 | { |
| 170 | int f; |
| 171 | |
| 172 | if (pipeout) |
| 173 | return; |
| 174 | for (f = 0; f < SLAVES; f++) |
| 175 | close(slavefd[f]); |
| 176 | while (wait(NULL) >= 0) ; /* wait for any signals from slaves */ |
| 177 | msg("Tape rewinding\n"); |
| 178 | #ifdef RDUMP |
| 179 | if (host) { |
| 180 | rmtclose(); |
| 181 | while (rmtopen(tape, 0) < 0) |
| 182 | sleep(10); |
| 183 | rmtclose(); |
| 184 | return; |
| 185 | } |
| 186 | #endif RDUMP |
| 187 | close(to); |
| 188 | while ((f = open(tape, 0)) < 0) |
| 189 | sleep (10); |
| 190 | close(f); |
| 191 | } |
| 192 | |
| 193 | close_rewind() |
| 194 | { |
| 195 | rewind(); |
| 196 | if (!nogripe) { |
| 197 | msg("Change Tapes: Mount tape #%d\n", tapeno+1); |
| 198 | broadcast("CHANGE TAPES!\7\7\n"); |
| 199 | } |
| 200 | while (!query("Is the new tape mounted and ready to go?")) |
| 201 | if (query("Do you want to abort?")) { |
| 202 | dumpabort(); |
| 203 | /*NOTREACHED*/ |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * We implement taking and restoring checkpoints on the tape level. |
| 209 | * When each tape is opened, a new process is created by forking; this |
| 210 | * saves all of the necessary context in the parent. The child |
| 211 | * continues the dump; the parent waits around, saving the context. |
| 212 | * If the child returns X_REWRITE, then it had problems writing that tape; |
| 213 | * this causes the parent to fork again, duplicating the context, and |
| 214 | * everything continues as if nothing had happened. |
| 215 | */ |
| 216 | |
| 217 | otape() |
| 218 | { |
| 219 | int parentpid; |
| 220 | int childpid; |
| 221 | int status; |
| 222 | int waitpid; |
| 223 | int (*interrupt)() = signal(SIGINT, SIG_IGN); |
| 224 | int blks, i; |
| 225 | |
| 226 | parentpid = getpid(); |
| 227 | |
| 228 | restore_check_point: |
| 229 | signal(SIGINT, interrupt); |
| 230 | /* |
| 231 | * All signals are inherited... |
| 232 | */ |
| 233 | childpid = fork(); |
| 234 | if (childpid < 0) { |
| 235 | msg("Context save fork fails in parent %d\n", parentpid); |
| 236 | Exit(X_ABORT); |
| 237 | } |
| 238 | if (childpid != 0) { |
| 239 | /* |
| 240 | * PARENT: |
| 241 | * save the context by waiting |
| 242 | * until the child doing all of the work returns. |
| 243 | * don't catch the interrupt |
| 244 | */ |
| 245 | signal(SIGINT, SIG_IGN); |
| 246 | #ifdef TDEBUG |
| 247 | msg("Tape: %d; parent process: %d child process %d\n", |
| 248 | tapeno+1, parentpid, childpid); |
| 249 | #endif TDEBUG |
| 250 | while ((waitpid = wait(&status)) != childpid) |
| 251 | msg("Parent %d waiting for child %d has another child %d return\n", |
| 252 | parentpid, childpid, waitpid); |
| 253 | if (status & 0xFF) { |
| 254 | msg("Child %d returns LOB status %o\n", |
| 255 | childpid, status&0xFF); |
| 256 | } |
| 257 | status = (status >> 8) & 0xFF; |
| 258 | #ifdef TDEBUG |
| 259 | switch(status) { |
| 260 | case X_FINOK: |
| 261 | msg("Child %d finishes X_FINOK\n", childpid); |
| 262 | break; |
| 263 | case X_ABORT: |
| 264 | msg("Child %d finishes X_ABORT\n", childpid); |
| 265 | break; |
| 266 | case X_REWRITE: |
| 267 | msg("Child %d finishes X_REWRITE\n", childpid); |
| 268 | break; |
| 269 | default: |
| 270 | msg("Child %d finishes unknown %d\n", |
| 271 | childpid, status); |
| 272 | break; |
| 273 | } |
| 274 | #endif TDEBUG |
| 275 | switch(status) { |
| 276 | case X_FINOK: |
| 277 | Exit(X_FINOK); |
| 278 | case X_ABORT: |
| 279 | Exit(X_ABORT); |
| 280 | case X_REWRITE: |
| 281 | goto restore_check_point; |
| 282 | default: |
| 283 | msg("Bad return code from dump: %d\n", status); |
| 284 | Exit(X_ABORT); |
| 285 | } |
| 286 | /*NOTREACHED*/ |
| 287 | } else { /* we are the child; just continue */ |
| 288 | #ifdef TDEBUG |
| 289 | sleep(4); /* allow time for parent's message to get out */ |
| 290 | msg("Child on Tape %d has parent %d, my pid = %d\n", |
| 291 | tapeno+1, parentpid, getpid()); |
| 292 | #endif TDEBUG |
| 293 | #ifdef RDUMP |
| 294 | while ((to = (host ? rmtopen(tape, 2) : |
| 295 | pipeout ? 1 : creat(tape, 0666))) < 0) |
| 296 | #else RDUMP |
| 297 | while ((to = pipeout ? 1 : creat(tape, 0666)) < 0) |
| 298 | #endif RDUMP |
| 299 | { |
| 300 | msg("Cannot open tape \"%s\".\n", tape); |
| 301 | if (!query("Do you want to retry the open?")) |
| 302 | dumpabort(); |
| 303 | } |
| 304 | |
| 305 | enslave(); /* Share open tape file descriptor with slaves */ |
| 306 | |
| 307 | asize = 0; |
| 308 | tapeno++; /* current tape sequence */ |
| 309 | newtape++; /* new tape signal */ |
| 310 | blks = 0; |
| 311 | if (spcl.c_type != TS_END) |
| 312 | for (i = 0; i < spcl.c_count; i++) |
| 313 | if (spcl.c_addr[i] != 0) |
| 314 | blks++; |
| 315 | spcl.c_count = blks + 1 - spcl.c_tapea + lastspclrec; |
| 316 | spcl.c_volume++; |
| 317 | spcl.c_type = TS_TAPE; |
| 318 | spcl.c_flags |= DR_NEWHEADER; |
| 319 | spclrec(); |
| 320 | spcl.c_flags &=~ DR_NEWHEADER; |
| 321 | if (tapeno > 1) |
| 322 | msg("Tape %d begins with blocks from ino %d\n", |
| 323 | tapeno, ino); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | dumpabort() |
| 328 | { |
| 329 | if (master != 0 && master != getpid()) |
| 330 | kill(master, SIGTERM); /* Signals master to call dumpabort */ |
| 331 | else { |
| 332 | killall(); |
| 333 | msg("The ENTIRE dump is aborted.\n"); |
| 334 | } |
| 335 | Exit(X_ABORT); |
| 336 | } |
| 337 | |
| 338 | Exit(status) |
| 339 | { |
| 340 | #ifdef TDEBUG |
| 341 | msg("pid = %d exits with status %d\n", getpid(), status); |
| 342 | #endif TDEBUG |
| 343 | exit(status); |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * could use pipe() for this if flock() worked on pipes |
| 348 | */ |
| 349 | lockfile(fd) |
| 350 | int fd[2]; |
| 351 | { |
| 352 | char tmpname[20]; |
| 353 | |
| 354 | strcpy(tmpname, _PATH_LOCK); |
| 355 | mktemp(tmpname); |
| 356 | if ((fd[1] = creat(tmpname, 0400)) < 0) { |
| 357 | msg("Could not create lockfile "); |
| 358 | perror(tmpname); |
| 359 | dumpabort(); |
| 360 | } |
| 361 | if ((fd[0] = open(tmpname, 0)) < 0) { |
| 362 | msg("Could not reopen lockfile "); |
| 363 | perror(tmpname); |
| 364 | dumpabort(); |
| 365 | } |
| 366 | unlink(tmpname); |
| 367 | } |
| 368 | |
| 369 | enslave() |
| 370 | { |
| 371 | int first[2], prev[2], next[2], cmd[2]; /* file descriptors */ |
| 372 | register int i, j; |
| 373 | |
| 374 | master = getpid(); |
| 375 | signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */ |
| 376 | signal(SIGPIPE, sigpipe); |
| 377 | signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */ |
| 378 | lockfile(first); |
| 379 | for (i = 0; i < SLAVES; i++) { |
| 380 | if (i == 0) { |
| 381 | prev[0] = first[1]; |
| 382 | prev[1] = first[0]; |
| 383 | } else { |
| 384 | prev[0] = next[0]; |
| 385 | prev[1] = next[1]; |
| 386 | flock(prev[1], LOCK_EX); |
| 387 | } |
| 388 | if (i < SLAVES - 1) { |
| 389 | lockfile(next); |
| 390 | } else { |
| 391 | next[0] = first[0]; |
| 392 | next[1] = first[1]; /* Last slave loops back */ |
| 393 | } |
| 394 | if (pipe(cmd) < 0 || (slavepid[i] = fork()) < 0) { |
| 395 | msg("too many slaves, %d (recompile smaller) ", i); |
| 396 | perror(""); |
| 397 | dumpabort(); |
| 398 | } |
| 399 | slavefd[i] = cmd[1]; |
| 400 | if (slavepid[i] == 0) { /* Slave starts up here */ |
| 401 | for (j = 0; j <= i; j++) |
| 402 | close(slavefd[j]); |
| 403 | signal(SIGINT, SIG_IGN); /* Master handles this */ |
| 404 | doslave(cmd[0], prev, next); |
| 405 | Exit(X_FINOK); |
| 406 | } |
| 407 | close(cmd[0]); |
| 408 | if (i > 0) { |
| 409 | close(prev[0]); |
| 410 | close(prev[1]); |
| 411 | } |
| 412 | } |
| 413 | close(first[0]); |
| 414 | close(first[1]); |
| 415 | master = 0; rotor = 0; |
| 416 | } |
| 417 | |
| 418 | killall() |
| 419 | { |
| 420 | register int i; |
| 421 | |
| 422 | for (i = 0; i < SLAVES; i++) |
| 423 | if (slavepid[i] > 0) |
| 424 | kill(slavepid[i], SIGKILL); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Synchronization - each process has a lockfile, and shares file |
| 429 | * descriptors to the following process's lockfile. When our write |
| 430 | * completes, we release our lock on the following process's lock- |
| 431 | * file, allowing the following process to lock it and proceed. We |
| 432 | * get the lock back for the next cycle by swapping descriptors. |
| 433 | */ |
| 434 | doslave(cmd, prev, next) |
| 435 | register int cmd, prev[2], next[2]; |
| 436 | { |
| 437 | register int nread, toggle = 0; |
| 438 | |
| 439 | close(fi); |
| 440 | if ((fi = open(disk, 0)) < 0) { /* Need our own seek pointer */ |
| 441 | perror(" DUMP: slave couldn't reopen disk"); |
| 442 | dumpabort(); |
| 443 | } |
| 444 | /* |
| 445 | * Get list of blocks to dump, read the blocks into tape buffer |
| 446 | */ |
| 447 | while ((nread = atomic(read, cmd, req, reqsiz)) == reqsiz) { |
| 448 | register struct req *p = req; |
| 449 | for (trecno = 0; trecno < ntrec; trecno += p->count, p += p->count) { |
| 450 | if (p->dblk) { |
| 451 | bread(p->dblk, tblock[trecno], |
| 452 | p->count * TP_BSIZE); |
| 453 | } else { |
| 454 | if (p->count != 1 || atomic(read, cmd, |
| 455 | tblock[trecno], TP_BSIZE) != TP_BSIZE) { |
| 456 | msg("Master/slave protocol botched.\n"); |
| 457 | dumpabort(); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | flock(prev[toggle], LOCK_EX); /* Wait our turn */ |
| 462 | |
| 463 | #ifdef RDUMP |
| 464 | if ((host ? rmtwrite(tblock[0], writesize) |
| 465 | : write(to, tblock[0], writesize)) != writesize) { |
| 466 | #else RDUMP |
| 467 | if (write(to, tblock[0], writesize) != writesize) { |
| 468 | #endif RDUMP |
| 469 | kill(master, SIGUSR1); |
| 470 | for (;;) |
| 471 | sigpause(0); |
| 472 | } |
| 473 | toggle ^= 1; |
| 474 | flock(next[toggle], LOCK_UN); /* Next slave's turn */ |
| 475 | } /* Also jolts him awake */ |
| 476 | if (nread != 0) { |
| 477 | perror(" DUMP: error reading command pipe"); |
| 478 | dumpabort(); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Since a read from a pipe may not return all we asked for, |
| 484 | * or a write may not write all we ask if we get a signal, |
| 485 | * loop until the count is satisfied (or error). |
| 486 | */ |
| 487 | atomic(func, fd, buf, count) |
| 488 | int (*func)(), fd, count; |
| 489 | char *buf; |
| 490 | { |
| 491 | int got, need = count; |
| 492 | |
| 493 | while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0) |
| 494 | buf += got; |
| 495 | return (got < 0 ? got : count - need); |
| 496 | } |