2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
36 static const char rcsid[] =
40 #include <sys/param.h>
41 #include <sys/socket.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
48 #include <protocols/dumprestore.h>
64 ino_t curino; /* current inumber; used globally */
65 int newtape; /* new tape flag */
66 union u_spcl u_spcl; /* mapping of variables in a control block */
68 static int tapefd; /* tape file descriptor */
69 static long asize; /* number of 0.1" units written on cur tape */
70 static int writesize; /* size of malloc()ed buffer for tape */
71 static int64_t lastspclrec = -1; /* tape block number of last written header */
72 static int trecno = 0; /* next record to write in current block */
73 static long blocksthisvol; /* number of blocks on current output file */
74 static char *nexttape;
75 static FILE *popenfp = NULL;
77 static int atomic(ssize_t (*)(), int, char *, int);
78 static void worker(int, int);
79 static void create_workers(void);
80 static void flushtape(void);
81 static void killall(void);
82 static void rollforward(void);
85 * Concurrent dump mods (Caltech) - disk block reading and tape writing
86 * are exported to several worker processes. While one worker 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 workers via pipes.
90 * The following structure defines the instruction packets sent to workers.
98 #define WORKERS 3 /* 1 worker writing, 1 reading, 1 for slack */
99 static struct worker {
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 */
104 int inode; /* inode that we are currently dealing with */
105 int fd; /* FD for this worker */
106 int pid; /* PID for this worker */
107 int sent; /* 1 == we've sent this worker requests */
108 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
109 struct req *req; /* buffer for requests */
110 } workers[WORKERS+1];
111 static struct worker *wp;
113 static char (*nextblock)[TP_BSIZE];
115 static int master; /* pid of master, for sending error signals */
116 static int tenths; /* length of tape used per block written */
117 static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
118 static volatile sig_atomic_t ready; /* reached the lock point without having */
119 /* received the SIGUSR2 signal from the prev worker? */
120 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
121 /* SIGUSR2 arrives from the previous worker */
126 int pgoff = getpagesize() - 1;
130 writesize = ntrec * TP_BSIZE;
131 reqsiz = (ntrec + 1) * sizeof(struct req);
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.
138 if (blocksperfile == 0 && !unlimited)
139 tenths = writesize / density +
140 (cartridge ? 16 : density == 625 ? 5 : 8);
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().
146 for (i = 0; i <= WORKERS; i++) {
148 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
151 workers[i].tblock = (char (*)[TP_BSIZE])
152 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
153 workers[i].req = (struct req *)workers[i].tblock - ntrec - 1;
159 nextblock = wp->tblock;
164 writerec(char *dp, int isspcl)
167 wp->req[trecno].dblk = (ufs2_daddr_t)0;
168 wp->req[trecno].count = 1;
169 /* Can't do a structure assignment due to alignment problems */
170 bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
172 lastspclrec = spcl.c_tapea;
180 dumpblock(ufs2_daddr_t blkno, int size)
185 dblkno = fsbtodb(sblock, blkno);
186 tpblks = size >> tp_bshift;
187 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
188 wp->req[trecno].dblk = dblkno;
189 wp->req[trecno].count = avail;
191 spcl.c_tapea += avail;
194 dblkno += avail << (tp_bshift - dev_bshift);
202 tperror(int signo __unused)
206 msg("write error on %s\n", tape);
207 quit("Cannot recover\n");
210 msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
211 broadcast("DUMP WRITE ERROR!\n");
212 if (!query("Do you want to restart?"))
214 msg("Closing this volume. Prepare to restart with new media;\n");
215 msg("this dump volume will be rewritten.\n");
223 sigpipe(int signo __unused)
226 quit("Broken pipe\n");
233 int64_t lastfirstrec;
235 int siz = (char *)nextblock - (char *)wp->req;
237 wp->req[trecno].count = 0; /* Sentinel */
239 if (atomic(write, wp->fd, (char *)wp->req, siz) != siz)
240 quit("error writing command pipe: %s\n", strerror(errno));
241 wp->sent = 1; /* we sent a request, read the response later */
243 lastfirstrec = wp->firstrec;
245 if (++wp >= &workers[WORKERS])
248 /* Read results back from next worker */
250 if (atomic(read, wp->fd, (char *)&got, sizeof got)
252 perror(" DUMP: error reading command pipe in master");
257 /* Check for end of tape */
258 if (got < writesize) {
259 msg("End of tape detected\n");
262 * Drain the results, don't care what the values were.
263 * If we read them here then trewind won't...
265 for (i = 0; i < WORKERS; i++) {
266 if (workers[i].sent) {
267 if (atomic(read, workers[i].fd,
268 (char *)&got, sizeof got)
270 perror(" DUMP: error reading command pipe in master");
284 if (spcl.c_type != TS_END && spcl.c_type != TS_CLRI &&
285 spcl.c_type != TS_BITS) {
286 assert(spcl.c_count <= TP_NINDIR);
287 for (i = 0; i < spcl.c_count; i++)
288 if (spcl.c_addr[i] != 0)
291 wp->count = lastspclrec + blks + 1 - spcl.c_tapea;
292 wp->tapea = spcl.c_tapea;
293 wp->firstrec = lastfirstrec + ntrec;
295 nextblock = wp->tblock;
298 blockswritten += ntrec;
299 blocksthisvol += ntrec;
300 if (!pipeout && !unlimited && (blocksperfile ?
301 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
315 for (f = 0; f < WORKERS; f++) {
317 * Drain the results, but unlike EOT we DO (or should) care
318 * what the return values were, since if we detect EOT after
319 * we think we've written the last blocks to the tape anyway,
320 * we have to replay those blocks with rollforward.
322 * fixme: punt for now.
324 if (workers[f].sent) {
325 if (atomic(read, workers[f].fd, (char *)&got, sizeof got)
327 perror(" DUMP: error reading command pipe in master");
331 if (got != writesize) {
332 msg("EOT detected in last 2 tape records!\n");
333 msg("Use a longer tape, decrease the size estimate\n");
334 quit("or use no size estimate at all.\n");
337 (void) close(workers[f].fd);
339 while (wait((int *)NULL) >= 0) /* wait for any signals from workers */
345 msg("Closing %s\n", tape);
349 (void)pclose(popenfp);
356 while (rmtopen(tape, 0) < 0)
362 if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
366 (void) close(tapefd);
367 while ((f = open(tape, 0)) < 0)
375 time_t tstart_changevol, tend_changevol;
380 (void)time((time_t *)&(tstart_changevol));
382 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
383 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
385 while (!query("Is the new volume mounted and ready to go?"))
386 if (query("Do you want to abort?")) {
390 (void)time((time_t *)&(tend_changevol));
391 if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
392 tstart_writing += (tend_changevol - tstart_changevol);
398 struct req *p, *q, *prev;
402 union u_spcl *ntb, *otb;
403 twp = &workers[WORKERS];
404 ntb = (union u_spcl *)twp->tblock[1];
407 * Each of the N workers should have requests that need to
408 * be replayed on the next tape. Use the extra worker buffers
409 * (workers[WORKERS]) to construct request lists to be sent to
410 * each worker in turn.
412 for (i = 0; i < WORKERS; i++) {
414 otb = (union u_spcl *)wp->tblock;
417 * For each request in the current worker, copy it to twp.
421 for (p = wp->req; p->count > 0; p += p->count) {
424 *ntb++ = *otb++; /* copy the datablock also */
429 quit("rollforward: protocol botch");
441 nextblock = twp->tblock;
442 savedtapea = spcl.c_tapea;
443 spcl.c_tapea = wp->tapea;
445 spcl.c_tapea = savedtapea;
446 lastspclrec = savedtapea - 1;
448 size = (char *)ntb - (char *)q;
449 if (atomic(write, wp->fd, (char *)q, size) != size) {
450 perror(" DUMP: error writing command pipe");
454 if (++wp >= &workers[WORKERS])
459 if (prev->dblk != 0) {
461 * If the last one was a disk block, make the
462 * first of this one be the last bit of that disk
465 q->dblk = prev->dblk +
466 prev->count * (TP_BSIZE / DEV_BSIZE);
467 ntb = (union u_spcl *)twp->tblock;
470 * It wasn't a disk block. Copy the data to its
471 * new location in the buffer.
474 *((union u_spcl *)twp->tblock) = *ntb;
475 ntb = (union u_spcl *)twp->tblock[1];
479 nextblock = wp->tblock;
485 * Clear the first workers' response. One hopes that it
486 * worked ok, otherwise the tape is much too short!
489 if (atomic(read, wp->fd, (char *)&got, sizeof got)
491 perror(" DUMP: error reading command pipe in master");
496 if (got != writesize) {
497 quit("EOT detected at start of the tape!\n");
503 * We implement taking and restoring checkpoints on the tape level.
504 * When each tape is opened, a new process is created by forking; this
505 * saves all of the necessary context in the parent. The child
506 * continues the dump; the parent waits around, saving the context.
507 * If the child returns X_REWRITE, then it had problems writing that tape;
508 * this causes the parent to fork again, duplicating the context, and
509 * everything continues as if nothing had happened.
512 startnewtape(int top)
518 sig_t interrupt_save;
520 interrupt_save = signal(SIGINT, SIG_IGN);
521 parentpid = getpid();
524 (void)signal(SIGINT, interrupt_save);
526 * All signals are inherited...
528 setproctitle(NULL); /* Restore the proctitle. */
531 msg("Context save fork fails in parent %d\n", parentpid);
537 * save the context by waiting
538 * until the child doing all of the work returns.
539 * don't catch the interrupt
541 signal(SIGINT, SIG_IGN);
543 msg("Tape: %d; parent process: %d child process %d\n",
544 tapeno+1, parentpid, childpid);
546 if (waitpid(childpid, &status, 0) == -1)
547 msg("Waiting for child %d: %s\n", childpid,
550 msg("Child %d returns LOB status %o\n",
551 childpid, status&0xFF);
553 status = (status >> 8) & 0xFF;
557 msg("Child %d finishes X_FINOK\n", childpid);
560 msg("Child %d finishes X_ABORT\n", childpid);
563 msg("Child %d finishes X_REWRITE\n", childpid);
566 msg("Child %d finishes unknown %d\n",
577 goto restore_check_point;
579 msg("Bad return code from dump: %d\n", status);
583 } else { /* we are the child; just continue */
585 sleep(4); /* allow time for parent's message to get out */
586 msg("Child on Tape %d has parent %d, my pid = %d\n",
587 tapeno+1, parentpid, getpid());
590 * If we have a name like "/dev/rmt0,/dev/rmt1",
591 * use the name before the comma first, and save
592 * the remaining names for subsequent volumes.
594 tapeno++; /* current tape sequence */
595 if (nexttape || strchr(tape, ',')) {
596 if (nexttape && *nexttape)
598 if ((p = strchr(tape, ',')) != NULL) {
603 msg("Dumping volume %d on %s\n", tapeno, tape);
606 tapefd = STDOUT_FILENO;
607 } else if (popenout) {
608 char volno[sizeof("2147483647")];
610 (void)sprintf(volno, "%d", spcl.c_volume + 1);
611 if (setenv("DUMP_VOLUME", volno, 1) == -1) {
612 msg("Cannot set $DUMP_VOLUME.\n");
615 popenfp = popen(popenout, "w");
616 if (popenfp == NULL) {
617 msg("Cannot open output pipeline \"%s\".\n",
621 tapefd = fileno(popenfp);
624 while ((tapefd = (host ? rmtopen(tape, 2) :
625 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
628 open(tape, O_WRONLY|O_CREAT, 0666)) < 0)
631 msg("Cannot open output \"%s\".\n", tape);
632 if (!query("Do you want to retry the open?"))
637 create_workers(); /* Share open tape file descriptor with workers */
639 close(tapefd); /* Give up our copy of it. */
640 signal(SIGINFO, infosch);
645 newtape++; /* new tape signal */
646 spcl.c_count = wp->count;
648 * measure firstrec in TP_BSIZE units since restore doesn't
649 * know the correct ntrec value...
651 spcl.c_firstrec = wp->firstrec;
653 spcl.c_type = TS_TAPE;
654 writeheader((ino_t)wp->inode);
656 msg("Volume %d begins with blocks from inode %d\n",
662 dumpabort(int signo __unused)
665 if (master != 0 && master != getpid())
666 /* Signals master to call dumpabort */
667 (void) kill(master, SIGTERM);
670 msg("The ENTIRE dump is aborted.\n");
684 msg("pid = %d exits with status %d\n", getpid(), status);
690 * proceed - handler for SIGUSR2, used to synchronize IO between the workers.
693 proceed(int signo __unused)
709 signal(SIGTERM, dumpabort); /* Worker sends SIGTERM on dumpabort() */
710 signal(SIGPIPE, sigpipe);
711 signal(SIGUSR1, tperror); /* Worker sends SIGUSR1 on tape errors */
712 signal(SIGUSR2, proceed); /* Worker sends SIGUSR2 to next worker */
714 for (i = 0; i < WORKERS; i++) {
715 if (i == wp - &workers[0]) {
721 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
722 (workers[i].pid = fork()) < 0)
723 quit("too many workers, %d (recompile smaller): %s\n",
726 workers[i].fd = cmd[1];
728 if (workers[i].pid == 0) { /* Worker starts up here */
729 for (j = 0; j <= i; j++)
730 (void) close(workers[j].fd);
731 signal(SIGINT, SIG_IGN); /* Master handles this */
737 for (i = 0; i < WORKERS; i++)
738 (void) atomic(write, workers[i].fd,
739 (char *) &workers[(i + 1) % WORKERS].pid,
740 sizeof workers[0].pid);
750 for (i = 0; i < WORKERS; i++)
751 if (workers[i].pid > 0) {
752 (void) kill(workers[i].pid, SIGKILL);
758 * Synchronization - each process has a lockfile, and shares file
759 * descriptors to the following process's lockfile. When our write
760 * completes, we release our lock on the following process's lock-
761 * file, allowing the following process to lock it and proceed. We
762 * get the lock back for the next cycle by swapping descriptors.
765 worker(int cmd, int worker_number)
768 int nextworker, size, wrote, eot_count;
771 * Need our own seek pointer.
773 (void) close(diskfd);
774 if ((diskfd = open(disk, O_RDONLY)) < 0)
775 quit("worker couldn't reopen disk: %s\n", strerror(errno));
778 * Need the pid of the next worker in the loop...
780 if ((nread = atomic(read, cmd, (char *)&nextworker, sizeof nextworker))
781 != sizeof nextworker) {
782 quit("master/worker protocol botched - didn't get pid of next worker.\n");
786 * Get list of blocks to dump, read the blocks into tape buffer
788 while ((nread = atomic(read, cmd, (char *)wp->req, reqsiz)) == reqsiz) {
789 struct req *p = wp->req;
791 for (trecno = 0; trecno < ntrec;
792 trecno += p->count, p += p->count) {
794 blkread(p->dblk, wp->tblock[trecno],
795 p->count * TP_BSIZE);
797 if (p->count != 1 || atomic(read, cmd,
798 (char *)wp->tblock[trecno],
799 TP_BSIZE) != TP_BSIZE)
800 quit("master/worker protocol botched.\n");
803 if (setjmp(jmpbuf) == 0) {
811 /* Try to write the data... */
816 while (eot_count < 10 && size < writesize) {
819 wrote = rmtwrite(wp->tblock[0]+size,
823 wrote = write(tapefd, wp->tblock[0]+size,
826 printf("worker %d wrote %d\n", worker_number, wrote);
836 if (size != writesize)
837 printf("worker %d only wrote %d out of %d bytes and gave up.\n",
838 worker_number, size, writesize);
842 * Handle ENOSPC as an EOT condition.
844 if (wrote < 0 && errno == ENOSPC) {
853 (void) kill(master, SIGUSR1);
858 * pass size of write back to master
861 (void) atomic(write, cmd, (char *)&size, sizeof size);
865 * If partial write, don't want next worker to go.
866 * Also jolts him awake.
868 (void) kill(nextworker, SIGUSR2);
871 quit("error reading command pipe: %s\n", strerror(errno));
875 * Since a read from a pipe may not return all we asked for,
876 * or a write may not write all we ask if we get a signal,
877 * loop until the count is satisfied (or error).
880 atomic(ssize_t (*func)(), int fd, char *buf, int count)
882 int got, need = count;
884 while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
886 return (got < 0 ? got : count - need);