1 /* $NetBSD: tape.c,v 1.48 2006/12/18 20:07:32 christos Exp $ */
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)tape.c 8.4 (Berkeley) 5/1/95";
37 __RCSID("$NetBSD: tape.c,v 1.48 2006/12/18 20:07:32 christos Exp $");
41 #include <sys/param.h>
42 #include <sys/socket.h>
45 #include <ufs/ufs/dinode.h>
46 #include <sys/ioctl.h>
49 #include <protocols/dumprestore.h>
61 #include "pathnames.h"
63 int writesize
; /* size of malloc()ed buffer for tape */
64 int64_t lastspclrec
= -1; /* tape block number of last written header */
65 int trecno
= 0; /* next record to write in current block */
66 extern long blocksperfile
; /* number of blocks per output file */
67 long blocksthisvol
; /* number of blocks on current output file */
68 extern int ntrec
; /* blocking factor on tape */
70 extern const char *host
;
73 static ssize_t
atomic_read(int, char *, int);
74 static ssize_t
atomic_write(int, char *, int);
75 static void doslave(int, int);
76 static void enslave(void);
77 static void flushtape(void);
78 static void killall(void);
79 static void proceed(int);
80 static void rollforward(void);
81 static void sigpipe(int);
82 static void tperror(int);
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 file system 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 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 slave */
106 int pid
; /* PID for this slave */
107 int sent
; /* 1 == we've sent this slave requests */
108 char (*tblock
)[TP_BSIZE
]; /* buffer for data blocks */
109 struct req
*req
; /* buffer for requests */
113 char (*nextblock
)[TP_BSIZE
];
115 static int64_t tapea_volume
; /* value of spcl.c_tapea at volume start */
117 int master
; /* pid of master, for sending error signals */
118 int tenths
; /* length of tape used per block written */
119 static volatile sig_atomic_t caught
; /* have we caught the signal to proceed? */
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 xmalloc((unsigned)(reqsiz
+ writesize
+ pgoff
+ TP_BSIZE
));
147 slaves
[i
].tblock
= (char (*)[TP_BSIZE
])
148 (((long)&buf
[ntrec
+ 1] + pgoff
) &~ pgoff
);
149 slaves
[i
].req
= (struct req
*)slaves
[i
].tblock
- ntrec
- 1;
155 nextblock
= slp
->tblock
;
160 writerec(char *dp
, int isspcl
)
163 slp
->req
[trecno
].dblk
= (daddr_t
)0;
164 slp
->req
[trecno
].count
= 1;
165 *(union u_spcl
*)(*(nextblock
)++) = *(union u_spcl
*)dp
;
167 lastspclrec
= iswap64(spcl
.c_tapea
);
169 spcl
.c_tapea
= iswap64(iswap64(spcl
.c_tapea
) +1);
175 dumpblock(daddr_t blkno
, int size
)
180 dblkno
= fsatoda(ufsib
, 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
= iswap64(iswap64(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");
226 * Update xferrate stats
235 ttaken
= tnow
- tstart_volume
;
236 blocks
= iswap64(spcl
.c_tapea
) - tapea_volume
;
237 msg("Volume %d completed at: %s", tapeno
, ctime(&tnow
));
239 msg("Volume %d took %d:%02d:%02d\n", tapeno
,
240 (int) (ttaken
/ 3600), (int) ((ttaken
% 3600) / 60),
241 (int) (ttaken
% 60));
242 msg("Volume %d transfer rate: %d KB/s\n", tapeno
,
243 (int) (blocks
/ ttaken
));
244 xferrate
+= blocks
/ ttaken
;
251 * information message upon receipt of SIGINFO
252 * (derived from optr.c::timeest())
255 statussig(int notused __unused
)
261 if (blockswritten
< 500)
264 (void) time((time_t *) &tnow
);
265 if (tnow
<= tstart_volume
)
267 deltat
= tstart_writing
- tnow
+
268 (1.0 * (tnow
- tstart_writing
)) / blockswritten
* tapesize
;
269 (void)snprintf(msgbuf
, sizeof(msgbuf
),
270 "%3.2f%% done at %ld KB/s, finished in %d:%02d\n",
271 (blockswritten
* 100.0) / tapesize
,
272 (long)((iswap64(spcl
.c_tapea
) - tapea_volume
) /
273 (tnow
- tstart_volume
)),
274 (int)(deltat
/ 3600), (int)((deltat
% 3600) / 60));
275 write(STDERR_FILENO
, msgbuf
, strlen(msgbuf
));
283 int64_t lastfirstrec
;
285 int siz
= (char *)nextblock
- (char *)slp
->req
;
287 slp
->req
[trecno
].count
= 0; /* Sentinel */
289 if (atomic_write(slp
->fd
, (char *)slp
->req
, siz
) != siz
)
290 quit("error writing command pipe: %s\n", strerror(errno
));
291 slp
->sent
= 1; /* we sent a request, read the response later */
293 lastfirstrec
= slp
->firstrec
;
295 if (++slp
>= &slaves
[SLAVES
])
298 /* Read results back from next slave */
300 if (atomic_read(slp
->fd
, (char *)&got
, sizeof got
)
302 perror(" DUMP: error reading command pipe in master");
307 /* Check for end of tape */
308 if (got
< writesize
) {
309 msg("End of tape detected\n");
312 * Drain the results, don't care what the values were.
313 * If we read them here then trewind won't...
315 for (i
= 0; i
< SLAVES
; i
++) {
316 if (slaves
[i
].sent
) {
317 if (atomic_read(slaves
[i
].fd
,
318 (char *)&got
, sizeof got
)
320 perror(" DUMP: error reading command pipe in master");
334 if (iswap32(spcl
.c_type
) != TS_END
) {
335 for (i
= 0; i
< iswap32(spcl
.c_count
); i
++)
336 if (spcl
.c_addr
[i
] != 0)
339 slp
->count
= lastspclrec
+ blks
+ 1 - iswap32(spcl
.c_tapea
);
340 slp
->tapea
= iswap64(spcl
.c_tapea
);
341 slp
->firstrec
= lastfirstrec
+ ntrec
;
343 nextblock
= slp
->tblock
;
346 blockswritten
+= ntrec
;
347 blocksthisvol
+= ntrec
;
348 if (!pipeout
&& !unlimited
&& (blocksperfile
?
349 (blocksthisvol
>= blocksperfile
) : (asize
> tsize
))) {
362 for (f
= 0; f
< SLAVES
; f
++) {
364 * Drain the results, but unlike EOT we DO (or should) care
365 * what the return values were, since if we detect EOT after
366 * we think we've written the last blocks to the tape anyway,
367 * we have to replay those blocks with rollforward.
369 * fixme: punt for now.
371 if (slaves
[f
].sent
) {
372 if (atomic_read(slaves
[f
].fd
, (char *)&got
, sizeof got
)
374 perror(" DUMP: error reading command pipe in master");
378 if (got
!= writesize
) {
379 msg("EOT detected in last 2 tape records!\n");
380 msg("Use a longer tape, decrease the size estimate\n");
381 quit("or use no size estimate at all.\n");
384 (void) close(slaves
[f
].fd
);
386 while (wait((int *)NULL
) >= 0) /* wait for any signals from slaves */
392 msg("Closing %s\n", tape
);
397 while (rmtopen(tape
, 0, 0) < 0)
399 if (eflag
&& eject
) {
400 msg("Ejecting %s\n", tape
);
401 (void) rmtioctl(MTOFFL
, 0);
407 (void) close(tapefd
);
408 while ((f
= open(tape
, 0)) < 0)
410 if (eflag
&& eject
) {
413 msg("Ejecting %s\n", tape
);
416 (void) ioctl(f
, MTIOCTOP
, &offl
);
431 msg("Change Volumes: Mount volume #%d\n", tapeno
+1);
432 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
435 for (i
= 0; i
< lflag
/ 10; i
++) { /* wait lflag seconds */
437 if (rmtopen(tape
, 0, 0) >= 0) {
442 if ((f
= open(tape
, 0)) >= 0) {
451 while (!query("Is the new volume mounted and ready to go?"))
452 if (query("Do you want to abort?")) {
461 struct req
*p
, *q
, *prev
;
463 int i
, size
, savedtapea
, got
;
464 union u_spcl
*ntb
, *otb
;
465 tslp
= &slaves
[SLAVES
];
466 ntb
= (union u_spcl
*)tslp
->tblock
[1];
469 * Each of the N slaves should have requests that need to
470 * be replayed on the next tape. Use the extra slave buffers
471 * (slaves[SLAVES]) to construct request lists to be sent to
472 * each slave in turn.
474 for (i
= 0; i
< SLAVES
; i
++) {
476 otb
= (union u_spcl
*)slp
->tblock
;
479 * For each request in the current slave, copy it to tslp.
483 for (p
= slp
->req
; p
->count
> 0; p
+= p
->count
) {
486 *ntb
++ = *otb
++; /* copy the datablock also */
491 quit("rollforward: protocol botch");
503 nextblock
= tslp
->tblock
;
504 savedtapea
= iswap32(spcl
.c_tapea
);
505 spcl
.c_tapea
= iswap32(slp
->tapea
);
507 spcl
.c_tapea
= iswap32(savedtapea
);
508 lastspclrec
= savedtapea
- 1;
510 size
= (char *)ntb
- (char *)q
;
511 if (atomic_write(slp
->fd
, (char *)q
, size
) != size
) {
512 perror(" DUMP: error writing command pipe");
516 if (++slp
>= &slaves
[SLAVES
])
521 if (prev
->dblk
!= 0) {
523 * If the last one was a disk block, make the
524 * first of this one be the last bit of that disk
527 q
->dblk
= prev
->dblk
+
528 prev
->count
* (TP_BSIZE
/ DEV_BSIZE
);
529 ntb
= (union u_spcl
*)tslp
->tblock
;
532 * It wasn't a disk block. Copy the data to its
533 * new location in the buffer.
536 *((union u_spcl
*)tslp
->tblock
) = *ntb
;
537 ntb
= (union u_spcl
*)tslp
->tblock
[1];
541 nextblock
= slp
->tblock
;
547 * Clear the first slaves' response. One hopes that it
548 * worked ok, otherwise the tape is much too short!
551 if (atomic_read(slp
->fd
, (char *)&got
, sizeof got
)
553 perror(" DUMP: error reading command pipe in master");
558 if (got
!= writesize
) {
559 quit("EOT detected at start of the tape!\n");
565 * We implement taking and restoring checkpoints on the tape level.
566 * When each tape is opened, a new process is created by forking; this
567 * saves all of the necessary context in the parent. The child
568 * continues the dump; the parent waits around, saving the context.
569 * If the child returns X_REWRITE, then it had problems writing that tape;
570 * this causes the parent to fork again, duplicating the context, and
571 * everything continues as if nothing had happened.
574 startnewtape(int top
)
581 sig_t interrupt_save
;
583 interrupt_save
= signal(SIGINT
, SIG_IGN
);
584 parentpid
= getpid();
585 tapea_volume
= iswap32(spcl
.c_tapea
);
586 (void)time(&tstart_volume
);
589 (void)signal(SIGINT
, interrupt_save
);
591 * All signals are inherited...
595 msg("Context save fork fails in parent %d\n", parentpid
);
601 * save the context by waiting
602 * until the child doing all of the work returns.
603 * don't catch the interrupt
605 signal(SIGINT
, SIG_IGN
);
606 signal(SIGINFO
, SIG_IGN
); /* only want child's stats */
608 msg("Tape: %d; parent process: %d child process %d\n",
609 tapeno
+1, parentpid
, childpid
);
611 while ((waitforpid
= wait(&status
)) != childpid
)
612 msg("Parent %d waiting for child %d has another child %d return\n",
613 parentpid
, childpid
, waitforpid
);
615 msg("Child %d returns LOB status %o\n",
616 childpid
, status
&0xFF);
618 status
= (status
>> 8) & 0xFF;
622 msg("Child %d finishes X_FINOK\n", childpid
);
625 msg("Child %d finishes X_ABORT\n", childpid
);
628 msg("Child %d finishes X_REWRITE\n", childpid
);
631 msg("Child %d finishes unknown %d\n",
642 goto restore_check_point
;
644 msg("Bad return code from dump: %d\n", status
);
648 } else { /* we are the child; just continue */
649 signal(SIGINFO
, statussig
); /* now want child's stats */
651 sleep(4); /* allow time for parent's message to get out */
652 msg("Child on Tape %d has parent %d, my pid = %d\n",
653 tapeno
+1, parentpid
, getpid());
656 * If we have a name like "/dev/rst0,/dev/rst1",
657 * use the name before the comma first, and save
658 * the remaining names for subsequent volumes.
660 tapeno
++; /* current tape sequence */
661 if (nexttape
|| strchr(tape
, ',')) {
662 if (nexttape
&& *nexttape
)
664 if ((p
= strchr(tape
, ',')) != NULL
) {
669 msg("Dumping volume %d on %s\n", tapeno
, tape
);
672 while ((tapefd
= (host
? rmtopen(tape
, 2, 1) :
673 pipeout
? 1 : open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
675 while ((tapefd
= (pipeout
? 1 :
676 open(tape
, O_WRONLY
|O_CREAT
, 0666))) < 0)
679 msg("Cannot open output \"%s\".\n", tape
);
680 if (!query("Do you want to retry the open?"))
684 enslave(); /* Share open tape file descriptor with slaves */
689 newtape
++; /* new tape signal */
690 spcl
.c_count
= iswap32(slp
->count
);
692 * measure firstrec in TP_BSIZE units since restore doesn't
693 * know the correct ntrec value...
695 spcl
.c_firstrec
= iswap32(slp
->firstrec
);
696 spcl
.c_volume
= iswap32(iswap32(spcl
.c_volume
) + 1);
697 spcl
.c_type
= iswap32(TS_TAPE
);
699 spcl
.c_flags
= iswap32(iswap32(spcl
.c_flags
)
701 writeheader((ino_t
)slp
->inode
);
703 spcl
.c_flags
= iswap32(iswap32(spcl
.c_flags
) &
705 msg("Volume %d started at: %s", tapeno
, ctime(&tstart_volume
));
707 msg("Volume %d begins with blocks from inode %d\n",
713 dumpabort(int signo __unused
)
716 if (master
!= 0 && master
!= getpid())
717 /* Signals master to call dumpabort */
718 (void) kill(master
, SIGTERM
);
724 msg("The ENTIRE dump is aborted.\n");
737 msg("pid = %d exits with status %d\n", getpid(), status
);
743 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
746 proceed(int signo __unused
)
759 signal(SIGTERM
, dumpabort
); /* Slave sends SIGTERM on dumpabort() */
760 signal(SIGPIPE
, sigpipe
);
761 signal(SIGUSR1
, tperror
); /* Slave sends SIGUSR1 on tape errors */
762 signal(SIGUSR2
, proceed
); /* Slave sends SIGUSR2 to next slave */
764 for (i
= 0; i
< SLAVES
; i
++) {
765 if (i
== slp
- &slaves
[0]) {
771 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, cmd
) < 0 ||
772 (slaves
[i
].pid
= fork()) < 0)
773 quit("too many slaves, %d (recompile smaller): %s\n",
776 slaves
[i
].fd
= cmd
[1];
778 if (slaves
[i
].pid
== 0) { /* Slave starts up here */
779 for (j
= 0; j
<= i
; j
++)
780 (void) close(slaves
[j
].fd
);
781 signal(SIGINT
, SIG_IGN
); /* Master handles this */
782 signal(SIGINFO
, SIG_IGN
);
788 for (i
= 0; i
< SLAVES
; i
++)
789 (void) atomic_write(slaves
[i
].fd
,
790 (char *) &slaves
[(i
+ 1) % SLAVES
].pid
,
791 sizeof slaves
[0].pid
);
801 for (i
= 0; i
< SLAVES
; i
++)
802 if (slaves
[i
].pid
> 0) {
803 (void) kill(slaves
[i
].pid
, SIGKILL
);
809 * Synchronization - each process has a lockfile, and shares file
810 * descriptors to the following process's lockfile. When our write
811 * completes, we release our lock on the following process's lock-
812 * file, allowing the following process to lock it and proceed. We
813 * get the lock back for the next cycle by swapping descriptors.
816 doslave(int cmd
, int slave_number __unused
)
818 int nread
, nextslave
, size
, wrote
, eot_count
, werror
;
819 sigset_t nsigset
, osigset
;
823 * Need our own seek pointer.
825 (void) close(diskfd
);
826 if ((diskfd
= open(disk_dev
, O_RDONLY
)) < 0)
827 quit("slave couldn't reopen disk: %s\n", strerror(errno
));
830 * Need the pid of the next slave in the loop...
832 if ((nread
= atomic_read(cmd
, (char *)&nextslave
, sizeof nextslave
))
833 != sizeof nextslave
) {
834 quit("master/slave protocol botched - didn't get pid of next slave.\n");
838 * Get list of blocks to dump, read the blocks into tape buffer
840 while ((nread
= atomic_read(cmd
, (char *)slp
->req
, reqsiz
)) == reqsiz
) {
841 struct req
*p
= slp
->req
;
843 for (trecno
= 0; trecno
< ntrec
;
844 trecno
+= p
->count
, p
+= p
->count
) {
846 bread(p
->dblk
, slp
->tblock
[trecno
],
847 p
->count
* TP_BSIZE
);
849 if (p
->count
!= 1 || atomic_read(cmd
,
850 (char *)slp
->tblock
[trecno
],
851 TP_BSIZE
) != TP_BSIZE
)
852 quit("master/slave protocol botched.\n");
856 sigemptyset(&nsigset
);
857 sigaddset(&nsigset
, SIGUSR2
);
858 sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
860 sigsuspend(&osigset
);
862 sigprocmask(SIG_SETMASK
, &osigset
, NULL
);
864 /* Try to write the data... */
869 while (eot_count
< 10 && size
< writesize
) {
872 wrote
= rmtwrite(slp
->tblock
[0]+size
,
876 wrote
= write(tapefd
, slp
->tblock
[0]+size
,
880 fprintf(stderr
, "slave %d wrote %d werror %d\n",
881 slave_number
, wrote
, werror
);
891 if (size
!= writesize
)
893 "slave %d only wrote %d out of %d bytes and gave up.\n",
894 slave_number
, size
, writesize
);
898 * Handle ENOSPC as an EOT condition.
900 if (wrote
< 0 && werror
== ENOSPC
) {
909 (void) kill(master
, SIGUSR1
);
910 sigemptyset(&nsigset
);
912 sigsuspend(&nsigset
);
915 * pass size of write back to master
918 (void) atomic_write(cmd
, (char *)&size
, sizeof size
);
922 * If partial write, don't want next slave to go.
923 * Also jolts him awake.
925 (void) kill(nextslave
, SIGUSR2
);
929 quit("error reading command pipe: %s\n", strerror(errno
));
933 * Since a read from a pipe may not return all we asked for,
934 * loop until the count is satisfied (or error).
937 atomic_read(int fd
, char *buf
, int count
)
939 ssize_t got
, need
= count
;
941 while ((got
= read(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
943 return (got
< 0 ? got
: count
- need
);
947 * Since a write may not write all we ask if we get a signal,
948 * loop until the count is satisfied (or error).
951 atomic_write(int fd
, char *buf
, int count
)
953 ssize_t got
, need
= count
;
955 while ((got
= write(fd
, buf
, need
)) > 0 && (need
-= got
) > 0)
957 return (got
< 0 ? got
: count
- need
);