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
* 4. 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
32
static char sccsid[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
34
static const char rcsid[] =
35
"$FreeBSD: src/sbin/dump/tape.c,v 1.27.24.1 2010/02/10 00:26:20 kensmith Exp $";
38
#include <sys/param.h>
39
#include <sys/socket.h>
44
#include <ufs/ufs/dinode.h>
45
#include <ufs/ffs/fs.h>
47
#include <protocols/dumprestore.h>
61
int writesize; /* size of malloc()ed buffer for tape */
62
int64_t lastspclrec = -1; /* tape block number of last written header */
63
int trecno = 0; /* next record to write in current block */
64
extern long blocksperfile; /* number of blocks per output file */
65
long blocksthisvol; /* number of blocks on current output file */
66
extern int ntrec; /* blocking factor on tape */
72
static int atomic(ssize_t (*)(), int, char *, int);
73
static void doslave(int, int);
74
static void enslave(void);
75
static void flushtape(void);
76
static void killall(void);
77
static void rollforward(void);
80
* Concurrent dump mods (Caltech) - disk block reading and tape writing
81
* are exported to several slave processes. While one slave writes the
82
* tape, the others read disk blocks; they pass control of the tape in
83
* a ring via signals. The parent process traverses the file system and
84
* sends writeheader()'s and lists of daddr's to the slaves via pipes.
85
* The following structure defines the instruction packets sent to slaves.
93
#define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
95
int64_t tapea; /* header number at start of this chunk */
96
int64_t firstrec; /* record number of this block */
97
int count; /* count to next header (used for TS_TAPE */
99
int inode; /* inode that we are currently dealing with */
100
int fd; /* FD for this slave */
101
int pid; /* PID for this slave */
102
int sent; /* 1 == we've sent this slave requests */
103
char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
104
struct req *req; /* buffer for requests */
108
char (*nextblock)[TP_BSIZE];
110
int master; /* pid of master, for sending error signals */
111
int tenths; /* length of tape used per block written */
112
static volatile sig_atomic_t caught; /* have we caught the signal to proceed? */
113
static volatile sig_atomic_t ready; /* reached the lock point without having */
114
/* received the SIGUSR2 signal from the prev slave? */
115
static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
116
/* SIGUSR2 arrives from the previous slave */
121
int pgoff = getpagesize() - 1;
125
writesize = ntrec * TP_BSIZE;
126
reqsiz = (ntrec + 1) * sizeof(struct req);
128
* CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
129
* (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
130
* repositioning after stopping, i.e, streaming mode, where the gap is
131
* variable, 0.30" to 0.45". The gap is maximal when the tape stops.
133
if (blocksperfile == 0 && !unlimited)
134
tenths = writesize / density +
135
(cartridge ? 16 : density == 625 ? 5 : 8);
137
* Allocate tape buffer contiguous with the array of instruction
138
* packets, so flushtape() can write them together with one write().
139
* Align tape buffer on page boundary to speed up tape write().
141
for (i = 0; i <= SLAVES; i++) {
143
malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
146
slaves[i].tblock = (char (*)[TP_BSIZE])
147
(((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
148
slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
154
nextblock = slp->tblock;
159
writerec(char *dp, int isspcl)
162
slp->req[trecno].dblk = (ufs2_daddr_t)0;
163
slp->req[trecno].count = 1;
164
/* Can't do a structure assignment due to alignment problems */
165
bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
167
lastspclrec = spcl.c_tapea;
175
dumpblock(ufs2_daddr_t blkno, int size)
180
dblkno = fsbtodb(sblock, blkno);
181
tpblks = size >> tp_bshift;
182
while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
183
slp->req[trecno].dblk = dblkno;
184
slp->req[trecno].count = avail;
186
spcl.c_tapea += avail;
189
dblkno += avail << (tp_bshift - dev_bshift);
197
tperror(int signo __unused)
201
msg("write error on %s\n", tape);
202
quit("Cannot recover\n");
205
msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
206
broadcast("DUMP WRITE ERROR!\n");
207
if (!query("Do you want to restart?"))
209
msg("Closing this volume. Prepare to restart with new media;\n");
210
msg("this dump volume will be rewritten.\n");
218
sigpipe(int signo __unused)
221
quit("Broken pipe\n");
228
int64_t lastfirstrec;
230
int siz = (char *)nextblock - (char *)slp->req;
232
slp->req[trecno].count = 0; /* Sentinel */
234
if (atomic(write, slp->fd, (char *)slp->req, siz) != siz)
235
quit("error writing command pipe: %s\n", strerror(errno));
236
slp->sent = 1; /* we sent a request, read the response later */
238
lastfirstrec = slp->firstrec;
240
if (++slp >= &slaves[SLAVES])
243
/* Read results back from next slave */
245
if (atomic(read, slp->fd, (char *)&got, sizeof got)
247
perror(" DUMP: error reading command pipe in master");
252
/* Check for end of tape */
253
if (got < writesize) {
254
msg("End of tape detected\n");
257
* Drain the results, don't care what the values were.
258
* If we read them here then trewind won't...
260
for (i = 0; i < SLAVES; i++) {
261
if (slaves[i].sent) {
262
if (atomic(read, slaves[i].fd,
263
(char *)&got, sizeof got)
265
perror(" DUMP: error reading command pipe in master");
279
if (spcl.c_type != TS_END) {
280
for (i = 0; i < spcl.c_count; i++)
281
if (spcl.c_addr[i] != 0)
284
slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
285
slp->tapea = spcl.c_tapea;
286
slp->firstrec = lastfirstrec + ntrec;
288
nextblock = slp->tblock;
291
blockswritten += ntrec;
292
blocksthisvol += ntrec;
293
if (!pipeout && !unlimited && (blocksperfile ?
294
(blocksthisvol >= blocksperfile) : (asize > tsize))) {
308
for (f = 0; f < SLAVES; f++) {
310
* Drain the results, but unlike EOT we DO (or should) care
311
* what the return values were, since if we detect EOT after
312
* we think we've written the last blocks to the tape anyway,
313
* we have to replay those blocks with rollforward.
315
* fixme: punt for now.
317
if (slaves[f].sent) {
318
if (atomic(read, slaves[f].fd, (char *)&got, sizeof got)
320
perror(" DUMP: error reading command pipe in master");
324
if (got != writesize) {
325
msg("EOT detected in last 2 tape records!\n");
326
msg("Use a longer tape, decrease the size estimate\n");
327
quit("or use no size estimate at all.\n");
330
(void) close(slaves[f].fd);
332
while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
338
msg("Closing %s\n", tape);
342
(void)pclose(popenfp);
349
while (rmtopen(tape, 0) < 0)
355
if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
359
(void) close(tapefd);
360
while ((f = open(tape, 0)) < 0)
368
time_t tstart_changevol, tend_changevol;
373
(void)time((time_t *)&(tstart_changevol));
375
msg("Change Volumes: Mount volume #%d\n", tapeno+1);
376
broadcast("CHANGE DUMP VOLUMES!\a\a\n");
378
while (!query("Is the new volume mounted and ready to go?"))
379
if (query("Do you want to abort?")) {
383
(void)time((time_t *)&(tend_changevol));
384
if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
385
tstart_writing += (tend_changevol - tstart_changevol);
391
struct req *p, *q, *prev;
395
union u_spcl *ntb, *otb;
396
tslp = &slaves[SLAVES];
397
ntb = (union u_spcl *)tslp->tblock[1];
400
* Each of the N slaves should have requests that need to
401
* be replayed on the next tape. Use the extra slave buffers
402
* (slaves[SLAVES]) to construct request lists to be sent to
403
* each slave in turn.
405
for (i = 0; i < SLAVES; i++) {
407
otb = (union u_spcl *)slp->tblock;
410
* For each request in the current slave, copy it to tslp.
414
for (p = slp->req; p->count > 0; p += p->count) {
417
*ntb++ = *otb++; /* copy the datablock also */
422
quit("rollforward: protocol botch");
434
nextblock = tslp->tblock;
435
savedtapea = spcl.c_tapea;
436
spcl.c_tapea = slp->tapea;
438
spcl.c_tapea = savedtapea;
439
lastspclrec = savedtapea - 1;
441
size = (char *)ntb - (char *)q;
442
if (atomic(write, slp->fd, (char *)q, size) != size) {
443
perror(" DUMP: error writing command pipe");
447
if (++slp >= &slaves[SLAVES])
452
if (prev->dblk != 0) {
454
* If the last one was a disk block, make the
455
* first of this one be the last bit of that disk
458
q->dblk = prev->dblk +
459
prev->count * (TP_BSIZE / DEV_BSIZE);
460
ntb = (union u_spcl *)tslp->tblock;
463
* It wasn't a disk block. Copy the data to its
464
* new location in the buffer.
467
*((union u_spcl *)tslp->tblock) = *ntb;
468
ntb = (union u_spcl *)tslp->tblock[1];
472
nextblock = slp->tblock;
478
* Clear the first slaves' response. One hopes that it
479
* worked ok, otherwise the tape is much too short!
482
if (atomic(read, slp->fd, (char *)&got, sizeof got)
484
perror(" DUMP: error reading command pipe in master");
489
if (got != writesize) {
490
quit("EOT detected at start of the tape!\n");
496
* We implement taking and restoring checkpoints on the tape level.
497
* When each tape is opened, a new process is created by forking; this
498
* saves all of the necessary context in the parent. The child
499
* continues the dump; the parent waits around, saving the context.
500
* If the child returns X_REWRITE, then it had problems writing that tape;
501
* this causes the parent to fork again, duplicating the context, and
502
* everything continues as if nothing had happened.
505
startnewtape(int top)
511
sig_t interrupt_save;
513
interrupt_save = signal(SIGINT, SIG_IGN);
514
parentpid = getpid();
517
(void)signal(SIGINT, interrupt_save);
519
* All signals are inherited...
521
setproctitle(NULL); /* Restore the proctitle. */
524
msg("Context save fork fails in parent %d\n", parentpid);
530
* save the context by waiting
531
* until the child doing all of the work returns.
532
* don't catch the interrupt
534
signal(SIGINT, SIG_IGN);
536
msg("Tape: %d; parent process: %d child process %d\n",
537
tapeno+1, parentpid, childpid);
539
if (waitpid(childpid, &status, 0) == -1)
540
msg("Waiting for child %d: %s\n", childpid,
543
msg("Child %d returns LOB status %o\n",
544
childpid, status&0xFF);
546
status = (status >> 8) & 0xFF;
550
msg("Child %d finishes X_FINOK\n", childpid);
553
msg("Child %d finishes X_ABORT\n", childpid);
556
msg("Child %d finishes X_REWRITE\n", childpid);
559
msg("Child %d finishes unknown %d\n",
570
goto restore_check_point;
572
msg("Bad return code from dump: %d\n", status);
576
} else { /* we are the child; just continue */
578
sleep(4); /* allow time for parent's message to get out */
579
msg("Child on Tape %d has parent %d, my pid = %d\n",
580
tapeno+1, parentpid, getpid());
583
* If we have a name like "/dev/rmt0,/dev/rmt1",
584
* use the name before the comma first, and save
585
* the remaining names for subsequent volumes.
587
tapeno++; /* current tape sequence */
588
if (nexttape || strchr(tape, ',')) {
589
if (nexttape && *nexttape)
591
if ((p = strchr(tape, ',')) != NULL) {
596
msg("Dumping volume %d on %s\n", tapeno, tape);
599
tapefd = STDOUT_FILENO;
600
} else if (popenout) {
601
char volno[sizeof("2147483647")];
603
(void)sprintf(volno, "%d", spcl.c_volume + 1);
604
if (setenv("DUMP_VOLUME", volno, 1) == -1) {
605
msg("Cannot set $DUMP_VOLUME.\n");
608
popenfp = popen(popenout, "w");
609
if (popenfp == NULL) {
610
msg("Cannot open output pipeline \"%s\".\n",
614
tapefd = fileno(popenfp);
617
while ((tapefd = (host ? rmtopen(tape, 2) :
618
open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
621
open(tape, O_WRONLY|O_CREAT, 0666)) < 0)
624
msg("Cannot open output \"%s\".\n", tape);
625
if (!query("Do you want to retry the open?"))
630
enslave(); /* Share open tape file descriptor with slaves */
632
close(tapefd); /* Give up our copy of it. */
633
signal(SIGINFO, infosch);
638
newtape++; /* new tape signal */
639
spcl.c_count = slp->count;
641
* measure firstrec in TP_BSIZE units since restore doesn't
642
* know the correct ntrec value...
644
spcl.c_firstrec = slp->firstrec;
646
spcl.c_type = TS_TAPE;
647
writeheader((ino_t)slp->inode);
649
msg("Volume %d begins with blocks from inode %d\n",
655
dumpabort(int signo __unused)
658
if (master != 0 && master != getpid())
659
/* Signals master to call dumpabort */
660
(void) kill(master, SIGTERM);
663
msg("The ENTIRE dump is aborted.\n");
677
msg("pid = %d exits with status %d\n", getpid(), status);
683
* proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
686
proceed(int signo __unused)
702
signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
703
signal(SIGPIPE, sigpipe);
704
signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
705
signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
707
for (i = 0; i < SLAVES; i++) {
708
if (i == slp - &slaves[0]) {
714
if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
715
(slaves[i].pid = fork()) < 0)
716
quit("too many slaves, %d (recompile smaller): %s\n",
719
slaves[i].fd = cmd[1];
721
if (slaves[i].pid == 0) { /* Slave starts up here */
722
for (j = 0; j <= i; j++)
723
(void) close(slaves[j].fd);
724
signal(SIGINT, SIG_IGN); /* Master handles this */
730
for (i = 0; i < SLAVES; i++)
731
(void) atomic(write, slaves[i].fd,
732
(char *) &slaves[(i + 1) % SLAVES].pid,
733
sizeof slaves[0].pid);
743
for (i = 0; i < SLAVES; i++)
744
if (slaves[i].pid > 0) {
745
(void) kill(slaves[i].pid, SIGKILL);
751
* Synchronization - each process has a lockfile, and shares file
752
* descriptors to the following process's lockfile. When our write
753
* completes, we release our lock on the following process's lock-
754
* file, allowing the following process to lock it and proceed. We
755
* get the lock back for the next cycle by swapping descriptors.
758
doslave(int cmd, int slave_number)
761
int nextslave, size, wrote, eot_count;
764
* Need our own seek pointer.
766
(void) close(diskfd);
767
if ((diskfd = open(disk, O_RDONLY)) < 0)
768
quit("slave couldn't reopen disk: %s\n", strerror(errno));
771
* Need the pid of the next slave in the loop...
773
if ((nread = atomic(read, cmd, (char *)&nextslave, sizeof nextslave))
774
!= sizeof nextslave) {
775
quit("master/slave protocol botched - didn't get pid of next slave.\n");
779
* Get list of blocks to dump, read the blocks into tape buffer
781
while ((nread = atomic(read, cmd, (char *)slp->req, reqsiz)) == reqsiz) {
782
struct req *p = slp->req;
784
for (trecno = 0; trecno < ntrec;
785
trecno += p->count, p += p->count) {
787
bread(p->dblk, slp->tblock[trecno],
788
p->count * TP_BSIZE);
790
if (p->count != 1 || atomic(read, cmd,
791
(char *)slp->tblock[trecno],
792
TP_BSIZE) != TP_BSIZE)
793
quit("master/slave protocol botched.\n");
796
if (setjmp(jmpbuf) == 0) {
804
/* Try to write the data... */
809
while (eot_count < 10 && size < writesize) {
812
wrote = rmtwrite(slp->tblock[0]+size,
816
wrote = write(tapefd, slp->tblock[0]+size,
819
printf("slave %d wrote %d\n", slave_number, wrote);
829
if (size != writesize)
830
printf("slave %d only wrote %d out of %d bytes and gave up.\n",
831
slave_number, size, writesize);
835
* Handle ENOSPC as an EOT condition.
837
if (wrote < 0 && errno == ENOSPC) {
846
(void) kill(master, SIGUSR1);
851
* pass size of write back to master
854
(void) atomic(write, cmd, (char *)&size, sizeof size);
858
* If partial write, don't want next slave to go.
859
* Also jolts him awake.
861
(void) kill(nextslave, SIGUSR2);
864
quit("error reading command pipe: %s\n", strerror(errno));
868
* Since a read from a pipe may not return all we asked for,
869
* or a write may not write all we ask if we get a signal,
870
* loop until the count is satisfied (or error).
873
atomic(ssize_t (*func)(), int fd, char *buf, int count)
875
int got, need = count;
877
while ((got = (*func)(fd, buf, need)) > 0 && (need -= got) > 0)
879
return (got < 0 ? got : count - need);