2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)tape.c 8.4 (Berkeley) 5/1/95
30 * $FreeBSD: src/sbin/dump/tape.c,v 1.12.2.3 2002/02/23 22:32:51 iedowse Exp $
33 #include <sys/param.h>
34 #include <sys/socket.h>
38 #include <vfs/ufs/dinode.h>
39 #include <vfs/ufs/fs.h>
41 #include <protocols/dumprestore.h>
54 static int writesize; /* size of malloc()ed buffer for tape */
55 static long lastspclrec = -1; /* tape block number of last written header */
56 static int trecno = 0; /* next record to write in current block */
57 static long blocksthisvol; /* number of blocks on current output file */
58 static const char *nexttape;
59 static int tapeno = 0; /* current tape number */
61 static int atomic_read(int, void *, int);
62 static int atomic_write(int, const void *, int);
64 static void doslave(int, int);
66 static void doslave(int);
68 static void enslave(void);
69 static void flushtape(void);
70 static void killall(void);
71 static void rollforward(void);
74 * Concurrent dump mods (Caltech) - disk block reading and tape writing
75 * are exported to several slave processes. While one slave writes the
76 * tape, the others read disk blocks; they pass control of the tape in
77 * a ring via signals. The parent process traverses the filesystem and
78 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
79 * The following structure defines the instruction packets sent to slaves.
87 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
89 int tapea; /* header number at start of this chunk */
90 int count; /* count to next header (used for TS_TAPE */
92 int inode; /* inode that we are currently dealing with */
93 int fd; /* FD for this slave */
94 int pid; /* PID for this slave */
95 int sent; /* 1 == we've sent this slave requests */
96 int firstrec; /* record number of this block */
97 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
98 struct req *req; /* buffer for requests */
102 char (*nextblock)[TP_BSIZE];
104 int master; /* pid of master, for sending error signals */
105 int tenths; /* length of tape used per block written */
106 static int caught; /* have we caught the signal to proceed? */
107 static int ready; /* have we reached the lock point without having */
108 /* received the SIGUSR2 signal from the prev slave? */
109 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
110 /* SIGUSR2 arrives from the previous slave */
115 int pgoff = getpagesize() - 1;
119 writesize = ntrec * TP_BSIZE;
120 reqsiz = (ntrec + 1) * sizeof(struct req);
122 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
123 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
124 * repositioning after stopping, i.e, streaming mode, where the gap is
125 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
127 if (blocksperfile == 0 && !unlimited)
128 tenths = writesize / density +
129 (cartridge ? 16 : density == 625 ? 5 : 8);
131 * Allocate tape buffer contiguous with the array of instruction
132 * packets, so flushtape() can write them together with one write().
133 * Align tape buffer on page boundary to speed up tape write().
135 for (i = 0; i <= SLAVES; i++) {
137 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
140 slaves[i].tblock = (char (*)[TP_BSIZE])
141 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
142 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
148 nextblock = slp->tblock;
153 writerec(const void *dp, int isspcl)
156 slp->req[trecno].dblk = (daddr_t)0;
157 slp->req[trecno].count = 1;
158 bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
160 lastspclrec = spcl.c_tapea;
168 dumpblock(daddr_t blkno, int size)
170 int avail, tpblks, dblkno;
172 dblkno = fsbtodb(sblock, blkno);
173 tpblks = size >> tp_bshift;
174 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
175 slp->req[trecno].dblk = dblkno;
176 slp->req[trecno].count = avail;
178 spcl.c_tapea += avail;
181 dblkno += avail << (tp_bshift - dev_bshift);
189 tperror(int signo __unused)
193 msg("write error on %s\n", tape);
194 quit("Cannot recover\n");
197 msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
198 broadcast("DUMP WRITE ERROR!\n");
199 if (!query("Do you want to restart?"))
201 msg("Closing this volume. Prepare to restart with new media;\n");
202 msg("this dump volume will be rewritten.\n");
210 sigpipe(int signo __unused)
213 quit("Broken pipe\n");
222 int siz = (char *)nextblock - (char *)slp->req;
224 slp->req[trecno].count = 0; /* Sentinel */
226 if (atomic_write(slp->fd, slp->req, siz) != siz)
227 quit("error writing command pipe: %s\n", strerror(errno));
228 slp->sent = 1; /* we sent a request, read the response later */
230 lastfirstrec = slp->firstrec;
232 if (++slp >= &slaves[SLAVES])
235 /* Read results back from next slave */
237 if (atomic_read(slp->fd, &got, sizeof got)
239 perror(" DUMP: error reading command pipe in master");
244 /* Check for end of tape */
245 if (got < writesize) {
246 msg("End of tape detected\n");
249 * Drain the results, don't care what the values were.
250 * If we read them here then trewind won't...
252 for (i = 0; i < SLAVES; i++) {
253 if (slaves[i].sent) {
254 if (atomic_read(slaves[i].fd,
257 perror(" DUMP: error reading command pipe in master");
271 if (spcl.c_type != TS_END) {
272 for (i = 0; i < spcl.c_count; i++)
273 if (spcl.c_addr[i] != 0)
276 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
277 slp->tapea = spcl.c_tapea;
278 slp->firstrec = lastfirstrec + ntrec;
280 nextblock = slp->tblock;
283 blockswritten += ntrec;
284 blocksthisvol += ntrec;
285 if (!pipeout && !unlimited && (blocksperfile ?
286 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
300 for (f = 0; f < SLAVES; f++) {
302 * Drain the results, but unlike EOT we DO (or should) care
303 * what the return values were, since if we detect EOT after
304 * we think we've written the last blocks to the tape anyway,
305 * we have to replay those blocks with rollforward.
307 * fixme: punt for now.
309 if (slaves[f].sent) {
310 if (atomic_read(slaves[f].fd, &got, sizeof got)
312 perror(" DUMP: error reading command pipe in master");
316 if (got != writesize) {
317 msg("EOT detected in last 2 tape records!\n");
318 msg("Use a longer tape, decrease the size estimate\n");
319 quit("or use no size estimate at all.\n");
324 while (wait(NULL) >= 0) /* wait for any signals from slaves */
330 msg("Closing %s\n", tape);
335 while (rmtopen(tape, 0) < 0)
341 if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
346 while ((f = open(tape, 0)) < 0)
354 time_t tstart_changevol, tend_changevol;
359 time((time_t *)&(tstart_changevol));
361 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
362 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
364 while (!query("Is the new volume mounted and ready to go?"))
365 if (query("Do you want to abort?")) {
369 time((time_t *)&(tend_changevol));
370 if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
371 tstart_writing += (tend_changevol - tstart_changevol);
377 struct req *p, *q, *prev;
379 int i, size, savedtapea, got;
380 union u_spcl *ntb, *otb;
381 tslp = &slaves[SLAVES];
382 ntb = (union u_spcl *)tslp->tblock[1];
385 * Each of the N slaves should have requests that need to
386 * be replayed on the next tape. Use the extra slave buffers
387 * (slaves[SLAVES]) to construct request lists to be sent to
388 * each slave in turn.
390 for (i = 0; i < SLAVES; i++) {
392 otb = (union u_spcl *)slp->tblock;
395 * For each request in the current slave, copy it to tslp.
399 for (p = slp->req; p->count > 0; p += p->count) {
402 *ntb++ = *otb++; /* copy the datablock also */
407 quit("rollforward: protocol botch");
419 nextblock = tslp->tblock;
420 savedtapea = spcl.c_tapea;
421 spcl.c_tapea = slp->tapea;
423 spcl.c_tapea = savedtapea;
424 lastspclrec = savedtapea - 1;
426 size = (char *)ntb - (char *)q;
427 if (atomic_write(slp->fd, q, size) != size) {
428 perror(" DUMP: error writing command pipe");
432 if (++slp >= &slaves[SLAVES])
437 if (prev->dblk != 0) {
439 * If the last one was a disk block, make the
440 * first of this one be the last bit of that disk
443 q->dblk = prev->dblk +
444 prev->count * (TP_BSIZE / DEV_BSIZE);
445 ntb = (union u_spcl *)tslp->tblock;
448 * It wasn't a disk block. Copy the data to its
449 * new location in the buffer.
452 *((union u_spcl *)tslp->tblock) = *ntb;
453 ntb = (union u_spcl *)tslp->tblock[1];
457 nextblock = slp->tblock;
463 * Clear the first slaves' response. One hopes that it
464 * worked ok, otherwise the tape is much too short!
467 if (atomic_read(slp->fd, &got, sizeof got)
469 perror(" DUMP: error reading command pipe in master");
474 if (got != writesize) {
475 quit("EOT detected at start of the tape!\n");
481 * We implement taking and restoring checkpoints on the tape level.
482 * When each tape is opened, a new process is created by forking; this
483 * saves all of the necessary context in the parent. The child
484 * continues the dump; the parent waits around, saving the context.
485 * If the child returns X_REWRITE, then it had problems writing that tape;
486 * this causes the parent to fork again, duplicating the context, and
487 * everything continues as if nothing had happened.
490 startnewtape(int top)
497 sig_t interrupt_save;
499 interrupt_save = signal(SIGINT, SIG_IGN);
500 parentpid = getpid();
503 signal(SIGINT, interrupt_save);
505 * All signals are inherited...
507 setproctitle(NULL); /* Restore the proctitle. */
510 msg("Context save fork fails in parent %d\n", parentpid);
516 * save the context by waiting
517 * until the child doing all of the work returns.
518 * don't catch the interrupt
520 signal(SIGINT, SIG_IGN);
522 msg("Tape: %d; parent process: %d child process %d\n",
523 tapeno+1, parentpid, childpid);
525 while ((wait_pid = wait(&status)) != childpid)
526 msg("Parent %d waiting for child %d has another child %d return\n",
527 parentpid, childpid, wait_pid);
529 msg("Child %d returns LOB status %o\n",
530 childpid, status&0xFF);
532 status = (status >> 8) & 0xFF;
536 msg("Child %d finishes X_FINOK\n", childpid);
539 msg("Child %d finishes X_ABORT\n", childpid);
542 msg("Child %d finishes X_REWRITE\n", childpid);
545 msg("Child %d finishes unknown %d\n",
556 goto restore_check_point;
558 msg("Bad return code from dump: %d\n", status);
562 } else { /* we are the child; just continue */
564 sleep(4); /* allow time for parent's message to get out */
565 msg("Child on Tape %d has parent %d, my pid = %d\n",
566 tapeno+1, parentpid, getpid());
569 * If we have a name like "/dev/rmt0,/dev/rmt1",
570 * use the name before the comma first, and save
571 * the remaining names for subsequent volumes.
573 tapeno++; /* current tape sequence */
574 if (nexttape || strchr(tape, ',')) {
575 if (nexttape && *nexttape)
577 if ((p = strchr(tape, ',')) != NULL) {
582 msg("Dumping volume %d on %s\n", tapeno, tape);
585 while ((tapefd = (host ? rmtopen(tape, 2) :
586 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
588 while ((tapefd = (pipeout ? 1 :
589 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
592 msg("Cannot open output \"%s\".\n", tape);
593 if (!query("Do you want to retry the open?"))
597 enslave(); /* Share open tape file descriptor with slaves */
598 signal(SIGINFO, infosch);
603 newtape++; /* new tape signal */
604 spcl.c_count = slp->count;
606 * measure firstrec in TP_BSIZE units since restore doesn't
607 * know the correct ntrec value...
609 spcl.c_firstrec = slp->firstrec;
611 spcl.c_type = TS_TAPE;
612 spcl.c_flags |= DR_NEWHEADER;
613 writeheader((ufs1_ino_t)slp->inode);
614 spcl.c_flags &=~ DR_NEWHEADER;
616 msg("Volume %d begins with blocks from inode %d\n",
622 dumpabort(int signo __unused)
625 if (master != 0 && master != getpid())
626 /* Signals master to call dumpabort */
627 kill(master, SIGTERM);
630 msg("The ENTIRE dump is aborted.\n");
643 msg("pid = %d exits with status %d\n", getpid(), status);
649 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
652 proceed(int signo __unused)
668 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
669 signal(SIGPIPE, sigpipe);
670 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
671 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
673 for (i = 0; i < SLAVES; i++) {
674 if (i == slp - &slaves[0]) {
680 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
681 (slaves[i].pid = fork()) < 0)
682 quit("too many slaves, %d (recompile smaller): %s\n",
685 slaves[i].fd = cmd[1];
687 if (slaves[i].pid == 0) { /* Slave starts up here */
688 for (j = 0; j <= i; j++)
690 signal(SIGINT, SIG_IGN); /* Master handles this */
700 for (i = 0; i < SLAVES; i++)
701 atomic_write(slaves[i].fd, &slaves[(i + 1) % SLAVES].pid,
702 sizeof slaves[0].pid);
712 for (i = 0; i < SLAVES; i++)
713 if (slaves[i].pid > 0) {
714 kill(slaves[i].pid, SIGKILL);
720 * Synchronization - each process has a lockfile, and shares file
721 * descriptors to the following process's lockfile. When our write
722 * completes, we release our lock on the following process's lock-
723 * file, allowing the following process to lock it and proceed. We
724 * get the lock back for the next cycle by swapping descriptors.
728 doslave(int cmd, int slave_number)
734 int nextslave, size, wrote, eot_count;
737 * Need our own seek pointer.
740 if ((diskfd = open(disk, O_RDONLY)) < 0)
741 quit("slave couldn't reopen disk: %s\n", strerror(errno));
744 * Need the pid of the next slave in the loop...
746 if ((nread = atomic_read(cmd, &nextslave, sizeof nextslave))
747 != sizeof nextslave) {
748 quit("master/slave protocol botched - didn't get pid of next slave.\n");
752 * Get list of blocks to dump, read the blocks into tape buffer
754 while ((nread = atomic_read(cmd, slp->req, reqsiz)) == reqsiz) {
755 struct req *p = slp->req;
757 for (trecno = 0; trecno < ntrec;
758 trecno += p->count, p += p->count) {
760 bread(p->dblk, slp->tblock[trecno],
761 p->count * TP_BSIZE);
764 atomic_read(cmd, slp->tblock[trecno],
765 TP_BSIZE) != TP_BSIZE)
766 quit("master/slave protocol botched.\n");
769 if (setjmp(jmpbuf) == 0) {
777 /* Try to write the data... */
782 while (eot_count < 10 && size < writesize) {
785 wrote = rmtwrite(slp->tblock[0]+size,
789 wrote = write(tapefd, slp->tblock[0]+size,
792 printf("slave %d wrote %d\n", slave_number, wrote);
802 if (size != writesize)
803 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
804 slave_number, size, writesize);
808 * Handle ENOSPC as an EOT condition.
810 if (wrote < 0 && errno == ENOSPC) {
819 kill(master, SIGUSR1);
824 * pass size of write back to master
827 atomic_write(cmd, &size, sizeof size);
831 * If partial write, don't want next slave to go.
832 * Also jolts him awake.
834 kill(nextslave, SIGUSR2);
837 quit("error reading command pipe: %s\n", strerror(errno));
841 atomic_read(int fd, void *buf, int count)
843 int got, need = count;
845 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
846 buf = (uint8_t *)buf + got;
847 return (got < 0 ? got : count - need);
851 atomic_write(int fd, const void *buf, int count)
853 int got, need = count;
855 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
856 buf = (const uint8_t *)buf + got;
857 return (got < 0 ? got : count - need);