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 int writesize; /* size of malloc()ed buffer for tape */
65 int64_t lastspclrec = -1; /* tape block number of last written header */
66 int trecno = 0; /* next record to write in current block */
67 extern long blocksperfile; /* number of blocks per output file */
68 long blocksthisvol; /* number of blocks on current output file */
69 extern int ntrec; /* blocking factor on tape */
75 static int atomic(ssize_t (*)(), int, char *, int);
76 static void doslave(int, int);
77 static void enslave(void);
78 static void flushtape(void);
79 static void killall(void);
80 static void rollforward(void);
83 * Concurrent dump mods (Caltech) - disk block reading and tape writing
84 * are exported to several slave processes. While one slave writes the
85 * tape, the others read disk blocks; they pass control of the tape in
86 * a ring via signals. The parent process traverses the file system and
87 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
88 * The following structure defines the instruction packets sent to slaves.
96 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
98 int64_t tapea; /* header number at start of this chunk */
99 int64_t firstrec; /* record number of this block */
100 int count; /* count to next header (used for TS_TAPE */
102 int inode; /* inode that we are currently dealing with */
103 int fd; /* FD for this slave */
104 int pid; /* PID for this slave */
105 int sent; /* 1 == we've sent this slave requests */
106 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
107 struct req *req; /* buffer for requests */
111 char (*nextblock)[TP_BSIZE];
113 int master; /* pid of master, for sending error signals */
114 int tenths; /* length of tape used per block written */
115 static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
116 static volatile sig_atomic_t ready; /* reached the lock point without having */
117 /* received the SIGUSR2 signal from the prev slave? */
118 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
119 /* SIGUSR2 arrives from the previous slave */
124 int pgoff = getpagesize() - 1;
128 writesize = ntrec * TP_BSIZE;
129 reqsiz = (ntrec + 1) * sizeof(struct req);
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.
136 if (blocksperfile == 0 && !unlimited)
137 tenths = writesize / density +
138 (cartridge ? 16 : density == 625 ? 5 : 8);
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().
144 for (i = 0; i <= SLAVES; i++) {
146 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
149 slaves[i].tblock = (char (*)[TP_BSIZE])
150 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
151 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
157 nextblock = slp->tblock;
162 writerec(char *dp, int isspcl)
165 slp->req[trecno].dblk = (ufs2_daddr_t)0;
166 slp->req[trecno].count = 1;
167 /* Can't do a structure assignment due to alignment problems */
168 bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
170 lastspclrec = spcl.c_tapea;
178 dumpblock(ufs2_daddr_t blkno, int size)
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;
189 spcl.c_tapea += avail;
192 dblkno += avail << (tp_bshift - dev_bshift);
200 tperror(int signo __unused)
204 msg("write error on %s\n", tape);
205 quit("Cannot recover\n");
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?"))
212 msg("Closing this volume. Prepare to restart with new media;\n");
213 msg("this dump volume will be rewritten.\n");
221 sigpipe(int signo __unused)
224 quit("Broken pipe\n");
231 int64_t lastfirstrec;
233 int siz = (char *)nextblock - (char *)slp->req;
235 slp->req[trecno].count = 0; /* Sentinel */
237 if (atomic(write, slp->fd, (char *)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 */
241 lastfirstrec = slp->firstrec;
243 if (++slp >= &slaves[SLAVES])
246 /* Read results back from next slave */
248 if (atomic(read, slp->fd, (char *)&got, sizeof got)
250 perror(" DUMP: error reading command pipe in master");
255 /* Check for end of tape */
256 if (got < writesize) {
257 msg("End of tape detected\n");
260 * Drain the results, don't care what the values were.
261 * If we read them here then trewind won't...
263 for (i = 0; i < SLAVES; i++) {
264 if (slaves[i].sent) {
265 if (atomic(read, slaves[i].fd,
266 (char *)&got, sizeof got)
268 perror(" DUMP: error reading command pipe in master");
282 if (spcl.c_type != TS_END && spcl.c_type != TS_CLRI &&
283 spcl.c_type != TS_BITS) {
284 assert(spcl.c_count <= TP_NINDIR);
285 for (i = 0; i < spcl.c_count; i++)
286 if (spcl.c_addr[i] != 0)
289 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
290 slp->tapea = spcl.c_tapea;
291 slp->firstrec = lastfirstrec + ntrec;
293 nextblock = slp->tblock;
296 blockswritten += ntrec;
297 blocksthisvol += ntrec;
298 if (!pipeout && !unlimited && (blocksperfile ?
299 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
313 for (f = 0; f < SLAVES; f++) {
315 * Drain the results, but unlike EOT we DO (or should) care
316 * what the return values were, since if we detect EOT after
317 * we think we've written the last blocks to the tape anyway,
318 * we have to replay those blocks with rollforward.
320 * fixme: punt for now.
322 if (slaves[f].sent) {
323 if (atomic(read, slaves[f].fd, (char *)&got, sizeof got)
325 perror(" DUMP: error reading command pipe in master");
329 if (got != writesize) {
330 msg("EOT detected in last 2 tape records!\n");
331 msg("Use a longer tape, decrease the size estimate\n");
332 quit("or use no size estimate at all.\n");
335 (void) close(slaves[f].fd);
337 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
343 msg("Closing %s\n", tape);
347 (void)pclose(popenfp);
354 while (rmtopen(tape, 0) < 0)
360 if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
364 (void) close(tapefd);
365 while ((f = open(tape, 0)) < 0)
373 time_t tstart_changevol, tend_changevol;
378 (void)time((time_t *)&(tstart_changevol));
380 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
381 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
383 while (!query("Is the new volume mounted and ready to go?"))
384 if (query("Do you want to abort?")) {
388 (void)time((time_t *)&(tend_changevol));
389 if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
390 tstart_writing += (tend_changevol - tstart_changevol);
396 struct req *p, *q, *prev;
400 union u_spcl *ntb, *otb;
401 tslp = &slaves[SLAVES];
402 ntb = (union u_spcl *)tslp->tblock[1];
405 * Each of the N slaves should have requests that need to
406 * be replayed on the next tape. Use the extra slave buffers
407 * (slaves[SLAVES]) to construct request lists to be sent to
408 * each slave in turn.
410 for (i = 0; i < SLAVES; i++) {
412 otb = (union u_spcl *)slp->tblock;
415 * For each request in the current slave, copy it to tslp.
419 for (p = slp->req; p->count > 0; p += p->count) {
422 *ntb++ = *otb++; /* copy the datablock also */
427 quit("rollforward: protocol botch");
439 nextblock = tslp->tblock;
440 savedtapea = spcl.c_tapea;
441 spcl.c_tapea = slp->tapea;
443 spcl.c_tapea = savedtapea;
444 lastspclrec = savedtapea - 1;
446 size = (char *)ntb - (char *)q;
447 if (atomic(write, slp->fd, (char *)q, size) != size) {
448 perror(" DUMP: error writing command pipe");
452 if (++slp >= &slaves[SLAVES])
457 if (prev->dblk != 0) {
459 * If the last one was a disk block, make the
460 * first of this one be the last bit of that disk
463 q->dblk = prev->dblk +
464 prev->count * (TP_BSIZE / DEV_BSIZE);
465 ntb = (union u_spcl *)tslp->tblock;
468 * It wasn't a disk block. Copy the data to its
469 * new location in the buffer.
472 *((union u_spcl *)tslp->tblock) = *ntb;
473 ntb = (union u_spcl *)tslp->tblock[1];
477 nextblock = slp->tblock;
483 * Clear the first slaves' response. One hopes that it
484 * worked ok, otherwise the tape is much too short!
487 if (atomic(read, slp->fd, (char *)&got, sizeof got)
489 perror(" DUMP: error reading command pipe in master");
494 if (got != writesize) {
495 quit("EOT detected at start of the tape!\n");
501 * We implement taking and restoring checkpoints on the tape level.
502 * When each tape is opened, a new process is created by forking; this
503 * saves all of the necessary context in the parent. The child
504 * continues the dump; the parent waits around, saving the context.
505 * If the child returns X_REWRITE, then it had problems writing that tape;
506 * this causes the parent to fork again, duplicating the context, and
507 * everything continues as if nothing had happened.
510 startnewtape(int top)
516 sig_t interrupt_save;
518 interrupt_save = signal(SIGINT, SIG_IGN);
519 parentpid = getpid();
522 (void)signal(SIGINT, interrupt_save);
524 * All signals are inherited...
526 setproctitle(NULL); /* Restore the proctitle. */
529 msg("Context save fork fails in parent %d\n", parentpid);
535 * save the context by waiting
536 * until the child doing all of the work returns.
537 * don't catch the interrupt
539 signal(SIGINT, SIG_IGN);
541 msg("Tape: %d; parent process: %d child process %d\n",
542 tapeno+1, parentpid, childpid);
544 if (waitpid(childpid, &status, 0) == -1)
545 msg("Waiting for child %d: %s\n", childpid,
548 msg("Child %d returns LOB status %o\n",
549 childpid, status&0xFF);
551 status = (status >> 8) & 0xFF;
555 msg("Child %d finishes X_FINOK\n", childpid);
558 msg("Child %d finishes X_ABORT\n", childpid);
561 msg("Child %d finishes X_REWRITE\n", childpid);
564 msg("Child %d finishes unknown %d\n",
575 goto restore_check_point;
577 msg("Bad return code from dump: %d\n", status);
581 } else { /* we are the child; just continue */
583 sleep(4); /* allow time for parent's message to get out */
584 msg("Child on Tape %d has parent %d, my pid = %d\n",
585 tapeno+1, parentpid, getpid());
588 * If we have a name like "/dev/rmt0,/dev/rmt1",
589 * use the name before the comma first, and save
590 * the remaining names for subsequent volumes.
592 tapeno++; /* current tape sequence */
593 if (nexttape || strchr(tape, ',')) {
594 if (nexttape && *nexttape)
596 if ((p = strchr(tape, ',')) != NULL) {
601 msg("Dumping volume %d on %s\n", tapeno, tape);
604 tapefd = STDOUT_FILENO;
605 } else if (popenout) {
606 char volno[sizeof("2147483647")];
608 (void)sprintf(volno, "%d", spcl.c_volume + 1);
609 if (setenv("DUMP_VOLUME", volno, 1) == -1) {
610 msg("Cannot set $DUMP_VOLUME.\n");
613 popenfp = popen(popenout, "w");
614 if (popenfp == NULL) {
615 msg("Cannot open output pipeline \"%s\".\n",
619 tapefd = fileno(popenfp);
622 while ((tapefd = (host ? rmtopen(tape, 2) :
623 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
626 open(tape, O_WRONLY|O_CREAT, 0666)) < 0)
629 msg("Cannot open output \"%s\".\n", tape);
630 if (!query("Do you want to retry the open?"))
635 enslave(); /* Share open tape file descriptor with slaves */
637 close(tapefd); /* Give up our copy of it. */
638 signal(SIGINFO, infosch);
643 newtape++; /* new tape signal */
644 spcl.c_count = slp->count;
646 * measure firstrec in TP_BSIZE units since restore doesn't
647 * know the correct ntrec value...
649 spcl.c_firstrec = slp->firstrec;
651 spcl.c_type = TS_TAPE;
652 writeheader((ino_t)slp->inode);
654 msg("Volume %d begins with blocks from inode %d\n",
660 dumpabort(int signo __unused)
663 if (master != 0 && master != getpid())
664 /* Signals master to call dumpabort */
665 (void) kill(master, SIGTERM);
668 msg("The ENTIRE dump is aborted.\n");
682 msg("pid = %d exits with status %d\n", getpid(), status);
688 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
691 proceed(int signo __unused)
707 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
708 signal(SIGPIPE, sigpipe);
709 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
710 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
712 for (i = 0; i < SLAVES; i++) {
713 if (i == slp - &slaves[0]) {
719 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
720 (slaves[i].pid = fork()) < 0)
721 quit("too many slaves, %d (recompile smaller): %s\n",
724 slaves[i].fd = cmd[1];
726 if (slaves[i].pid == 0) { /* Slave starts up here */
727 for (j = 0; j <= i; j++)
728 (void) close(slaves[j].fd);
729 signal(SIGINT, SIG_IGN); /* Master handles this */
735 for (i = 0; i < SLAVES; i++)
736 (void) atomic(write, slaves[i].fd,
737 (char *) &slaves[(i + 1) % SLAVES].pid,
738 sizeof slaves[0].pid);
748 for (i = 0; i < SLAVES; i++)
749 if (slaves[i].pid > 0) {
750 (void) kill(slaves[i].pid, SIGKILL);
756 * Synchronization - each process has a lockfile, and shares file
757 * descriptors to the following process's lockfile. When our write
758 * completes, we release our lock on the following process's lock-
759 * file, allowing the following process to lock it and proceed. We
760 * get the lock back for the next cycle by swapping descriptors.
763 doslave(int cmd, int slave_number)
766 int nextslave, size, wrote, eot_count;
769 * Need our own seek pointer.
771 (void) close(diskfd);
772 if ((diskfd = open(disk, O_RDONLY)) < 0)
773 quit("slave couldn't reopen disk: %s\n", strerror(errno));
776 * Need the pid of the next slave in the loop...
778 if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave))
779 != sizeof nextslave) {
780 quit("master/slave protocol botched - didn't get pid of next slave.\n");
784 * Get list of blocks to dump, read the blocks into tape buffer
786 while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) {
787 struct req *p = slp->req;
789 for (trecno = 0; trecno < ntrec;
790 trecno += p->count, p += p->count) {
792 blkread(p->dblk, slp->tblock[trecno],
793 p->count * TP_BSIZE);
795 if (p->count != 1 || atomic(read, cmd,
796 (char *)slp->tblock[trecno],
797 TP_BSIZE) != TP_BSIZE)
798 quit("master/slave protocol botched.\n");
801 if (setjmp(jmpbuf) == 0) {
809 /* Try to write the data... */
814 while (eot_count < 10 && size < writesize) {
817 wrote = rmtwrite(slp->tblock[0]+size,
821 wrote = write(tapefd, slp->tblock[0]+size,
824 printf("slave %d wrote %d\n", slave_number, wrote);
834 if (size != writesize)
835 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
836 slave_number, size, writesize);
840 * Handle ENOSPC as an EOT condition.
842 if (wrote < 0 && errno == ENOSPC) {
851 (void) kill(master, SIGUSR1);
856 * pass size of write back to master
859 (void) atomic(write, cmd, (char *)&size, sizeof size);
863 * If partial write, don't want next slave to go.
864 * Also jolts him awake.
866 (void) kill(nextslave, SIGUSR2);
869 quit("error reading command pipe: %s\n", strerror(errno));
873 * Since a read from a pipe may not return all we asked for,
874 * or a write may not write all we ask if we get a signal,
875 * loop until the count is satisfied (or error).
878 atomic(ssize_t (*func)(), int fd, char *buf, int count)
880 int got, need = count;
882 while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
884 return (got < 0 ? got : count - need);