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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tape.c 8.4 (Berkeley) 5/1/95
34 * $FreeBSD: src/sbin/dump/tape.c,v 1.12.2.3 2002/02/23 22:32:51 iedowse Exp $
37 #include <sys/param.h>
38 #include <sys/socket.h>
43 #include <sys/vnode.h>
46 #include <ufs/inode.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/fs.h>
52 #include <protocols/dumprestore.h>
65 static int writesize; /* size of malloc()ed buffer for tape */
66 static long lastspclrec = -1; /* tape block number of last written header */
67 static int trecno = 0; /* next record to write in current block */
68 static long blocksthisvol; /* number of blocks on current output file */
69 static const char *nexttape;
70 static int tapeno = 0; /* current tape number */
72 static int atomic_read(int, void *, int);
73 static int atomic_write(int, const void *, int);
75 static void doslave(int, int);
77 static void doslave(int);
79 static void enslave(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 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 filesystem 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.
98 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
100 int tapea; /* header number at start of this chunk */
101 int count; /* count to next header (used for TS_TAPE */
103 int inode; /* inode that we are currently dealing with */
104 int fd; /* FD for this slave */
105 int pid; /* PID for this slave */
106 int sent; /* 1 == we've sent this slave requests */
107 int firstrec; /* record number of this block */
108 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
109 struct req *req; /* buffer for requests */
113 char (*nextblock)[TP_BSIZE];
115 int master; /* pid of master, for sending error signals */
116 int tenths; /* length of tape used per block written */
117 static int caught; /* have we caught the signal to proceed? */
118 static int ready; /* have we reached the lock point without having */
119 /* received the SIGUSR2 signal from the prev slave? */
120 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
121 /* SIGUSR2 arrives from the previous slave */
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 <= SLAVES; i++) {
148 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
151 slaves[i].tblock = (char (*)[TP_BSIZE])
152 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
153 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
159 nextblock = slp->tblock;
164 writerec(const void *dp, int isspcl)
167 slp->req[trecno].dblk = (daddr_t)0;
168 slp->req[trecno].count = 1;
169 bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
171 lastspclrec = spcl.c_tapea;
179 dumpblock(daddr_t blkno, int size)
181 int avail, tpblks, dblkno;
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");
233 int siz = (char *)nextblock - (char *)slp->req;
235 slp->req[trecno].count = 0; /* Sentinel */
237 if (atomic_write(slp->fd, 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, &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,
268 perror(" DUMP: error reading command pipe in master");
282 if (spcl.c_type != TS_END) {
283 for (i = 0; i < spcl.c_count; i++)
284 if (spcl.c_addr[i] != 0)
287 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
288 slp->tapea = spcl.c_tapea;
289 slp->firstrec = lastfirstrec + ntrec;
291 nextblock = slp->tblock;
294 blockswritten += ntrec;
295 blocksthisvol += ntrec;
296 if (!pipeout && !unlimited && (blocksperfile ?
297 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
311 for (f = 0; f < SLAVES; f++) {
313 * Drain the results, but unlike EOT we DO (or should) care
314 * what the return values were, since if we detect EOT after
315 * we think we've written the last blocks to the tape anyway,
316 * we have to replay those blocks with rollforward.
318 * fixme: punt for now.
320 if (slaves[f].sent) {
321 if (atomic_read(slaves[f].fd, &got, sizeof got)
323 perror(" DUMP: error reading command pipe in master");
327 if (got != writesize) {
328 msg("EOT detected in last 2 tape records!\n");
329 msg("Use a longer tape, decrease the size estimate\n");
330 quit("or use no size estimate at all.\n");
335 while (wait(NULL) >= 0) /* wait for any signals from slaves */
341 msg("Closing %s\n", tape);
346 while (rmtopen(tape, 0) < 0)
352 if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
357 while ((f = open(tape, 0)) < 0)
365 time_t tstart_changevol, tend_changevol;
370 time((time_t *)&(tstart_changevol));
372 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
373 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
375 while (!query("Is the new volume mounted and ready to go?"))
376 if (query("Do you want to abort?")) {
380 time((time_t *)&(tend_changevol));
381 if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
382 tstart_writing += (tend_changevol - tstart_changevol);
388 struct req *p, *q, *prev;
390 int i, size, savedtapea, got;
391 union u_spcl *ntb, *otb;
392 tslp = &slaves[SLAVES];
393 ntb = (union u_spcl *)tslp->tblock[1];
396 * Each of the N slaves should have requests that need to
397 * be replayed on the next tape. Use the extra slave buffers
398 * (slaves[SLAVES]) to construct request lists to be sent to
399 * each slave in turn.
401 for (i = 0; i < SLAVES; i++) {
403 otb = (union u_spcl *)slp->tblock;
406 * For each request in the current slave, copy it to tslp.
410 for (p = slp->req; p->count > 0; p += p->count) {
413 *ntb++ = *otb++; /* copy the datablock also */
418 quit("rollforward: protocol botch");
430 nextblock = tslp->tblock;
431 savedtapea = spcl.c_tapea;
432 spcl.c_tapea = slp->tapea;
434 spcl.c_tapea = savedtapea;
435 lastspclrec = savedtapea - 1;
437 size = (char *)ntb - (char *)q;
438 if (atomic_write(slp->fd, q, size) != size) {
439 perror(" DUMP: error writing command pipe");
443 if (++slp >= &slaves[SLAVES])
448 if (prev->dblk != 0) {
450 * If the last one was a disk block, make the
451 * first of this one be the last bit of that disk
454 q->dblk = prev->dblk +
455 prev->count * (TP_BSIZE / DEV_BSIZE);
456 ntb = (union u_spcl *)tslp->tblock;
459 * It wasn't a disk block. Copy the data to its
460 * new location in the buffer.
463 *((union u_spcl *)tslp->tblock) = *ntb;
464 ntb = (union u_spcl *)tslp->tblock[1];
468 nextblock = slp->tblock;
474 * Clear the first slaves' response. One hopes that it
475 * worked ok, otherwise the tape is much too short!
478 if (atomic_read(slp->fd, &got, sizeof got)
480 perror(" DUMP: error reading command pipe in master");
485 if (got != writesize) {
486 quit("EOT detected at start of the tape!\n");
492 * We implement taking and restoring checkpoints on the tape level.
493 * When each tape is opened, a new process is created by forking; this
494 * saves all of the necessary context in the parent. The child
495 * continues the dump; the parent waits around, saving the context.
496 * If the child returns X_REWRITE, then it had problems writing that tape;
497 * this causes the parent to fork again, duplicating the context, and
498 * everything continues as if nothing had happened.
501 startnewtape(int top)
509 void (*interrupt_save)();
511 sig_t interrupt_save;
514 interrupt_save = signal(SIGINT, SIG_IGN);
515 parentpid = getpid();
518 signal(SIGINT, interrupt_save);
520 * All signals are inherited...
522 setproctitle(NULL); /* Restore the proctitle. */
525 msg("Context save fork fails in parent %d\n", parentpid);
531 * save the context by waiting
532 * until the child doing all of the work returns.
533 * don't catch the interrupt
535 signal(SIGINT, SIG_IGN);
537 msg("Tape: %d; parent process: %d child process %d\n",
538 tapeno+1, parentpid, childpid);
540 while ((wait_pid = wait(&status)) != childpid)
541 msg("Parent %d waiting for child %d has another child %d return\n",
542 parentpid, childpid, wait_pid);
544 msg("Child %d returns LOB status %o\n",
545 childpid, status&0xFF);
547 status = (status >> 8) & 0xFF;
551 msg("Child %d finishes X_FINOK\n", childpid);
554 msg("Child %d finishes X_ABORT\n", childpid);
557 msg("Child %d finishes X_REWRITE\n", childpid);
560 msg("Child %d finishes unknown %d\n",
571 goto restore_check_point;
573 msg("Bad return code from dump: %d\n", status);
577 } else { /* we are the child; just continue */
579 sleep(4); /* allow time for parent's message to get out */
580 msg("Child on Tape %d has parent %d, my pid = %d\n",
581 tapeno+1, parentpid, getpid());
584 * If we have a name like "/dev/rmt0,/dev/rmt1",
585 * use the name before the comma first, and save
586 * the remaining names for subsequent volumes.
588 tapeno++; /* current tape sequence */
589 if (nexttape || strchr(tape, ',')) {
590 if (nexttape && *nexttape)
592 if ((p = strchr(tape, ',')) != NULL) {
597 msg("Dumping volume %d on %s\n", tapeno, tape);
600 while ((tapefd = (host ? rmtopen(tape, 2) :
601 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
603 while ((tapefd = (pipeout ? 1 :
604 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
607 msg("Cannot open output \"%s\".\n", tape);
608 if (!query("Do you want to retry the open?"))
612 enslave(); /* Share open tape file descriptor with slaves */
613 signal(SIGINFO, infosch);
618 newtape++; /* new tape signal */
619 spcl.c_count = slp->count;
621 * measure firstrec in TP_BSIZE units since restore doesn't
622 * know the correct ntrec value...
624 spcl.c_firstrec = slp->firstrec;
626 spcl.c_type = TS_TAPE;
627 spcl.c_flags |= DR_NEWHEADER;
628 writeheader((ufs1_ino_t)slp->inode);
629 spcl.c_flags &=~ DR_NEWHEADER;
631 msg("Volume %d begins with blocks from inode %d\n",
637 dumpabort(int signo __unused)
640 if (master != 0 && master != getpid())
641 /* Signals master to call dumpabort */
642 kill(master, SIGTERM);
645 msg("The ENTIRE dump is aborted.\n");
658 msg("pid = %d exits with status %d\n", getpid(), status);
664 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
667 proceed(int signo __unused)
683 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
684 signal(SIGPIPE, sigpipe);
685 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
686 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
688 for (i = 0; i < SLAVES; i++) {
689 if (i == slp - &slaves[0]) {
695 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
696 (slaves[i].pid = fork()) < 0)
697 quit("too many slaves, %d (recompile smaller): %s\n",
700 slaves[i].fd = cmd[1];
702 if (slaves[i].pid == 0) { /* Slave starts up here */
703 for (j = 0; j <= i; j++)
705 signal(SIGINT, SIG_IGN); /* Master handles this */
715 for (i = 0; i < SLAVES; i++)
716 atomic_write(slaves[i].fd, &slaves[(i + 1) % SLAVES].pid,
717 sizeof slaves[0].pid);
727 for (i = 0; i < SLAVES; i++)
728 if (slaves[i].pid > 0) {
729 kill(slaves[i].pid, SIGKILL);
735 * Synchronization - each process has a lockfile, and shares file
736 * descriptors to the following process's lockfile. When our write
737 * completes, we release our lock on the following process's lock-
738 * file, allowing the following process to lock it and proceed. We
739 * get the lock back for the next cycle by swapping descriptors.
743 doslave(int cmd, int slave_number)
749 int nextslave, size, wrote, eot_count;
752 * Need our own seek pointer.
755 if ((diskfd = open(disk, O_RDONLY)) < 0)
756 quit("slave couldn't reopen disk: %s\n", strerror(errno));
759 * Need the pid of the next slave in the loop...
761 if ((nread = atomic_read(cmd, &nextslave, sizeof nextslave))
762 != sizeof nextslave) {
763 quit("master/slave protocol botched - didn't get pid of next slave.\n");
767 * Get list of blocks to dump, read the blocks into tape buffer
769 while ((nread = atomic_read(cmd, slp->req, reqsiz)) == reqsiz) {
770 struct req *p = slp->req;
772 for (trecno = 0; trecno < ntrec;
773 trecno += p->count, p += p->count) {
775 bread(p->dblk, slp->tblock[trecno],
776 p->count * TP_BSIZE);
779 atomic_read(cmd, slp->tblock[trecno],
780 TP_BSIZE) != TP_BSIZE)
781 quit("master/slave protocol botched.\n");
784 if (setjmp(jmpbuf) == 0) {
792 /* Try to write the data... */
797 while (eot_count < 10 && size < writesize) {
800 wrote = rmtwrite(slp->tblock[0]+size,
804 wrote = write(tapefd, slp->tblock[0]+size,
807 printf("slave %d wrote %d\n", slave_number, wrote);
817 if (size != writesize)
818 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
819 slave_number, size, writesize);
823 * Handle ENOSPC as an EOT condition.
825 if (wrote < 0 && errno == ENOSPC) {
834 kill(master, SIGUSR1);
839 * pass size of write back to master
842 atomic_write(cmd, &size, sizeof size);
846 * If partial write, don't want next slave to go.
847 * Also jolts him awake.
849 kill(nextslave, SIGUSR2);
852 quit("error reading command pipe: %s\n", strerror(errno));
856 atomic_read(int fd, void *buf, int count)
858 int got, need = count;
860 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
861 buf = (uint8_t *)buf + got;
862 return (got < 0 ? got : count - need);
866 atomic_write(int fd, const void *buf, int count)
868 int got, need = count;
870 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
871 buf = (const uint8_t *)buf + got;
872 return (got < 0 ? got : count - need);