2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 #ifdef HAVE_SYS_WAIT_H
23 #include <afs/com_err.h>
27 #include "error_macros.h"
28 #include "butm_prototypes.h"
31 typedef off64_t osi_lloff_t;
32 #else /* O_LARGEFILE */
33 #ifdef AFS_HAVE_LLSEEK
34 typedef offset_t osi_lloff_t;
35 #else /* AFS_HAVE_LLSEEK */
36 typedef off_t osi_lloff_t;
37 #endif /* AFS_HAVE_LLSEEK */
38 #endif /* O_LARGEFILE */
42 #define FILE_MAGIC 1000000007 /* s/w file mark */
43 #define FILE_BEGIN 0 /* byte field in file mark */
44 #define FILE_FMEND 1 /* byte field in file mark */
45 #define FILE_EOD -1 /* byte field in file mark */
46 #define TAPE_MAGIC 1100000009 /* tape label block */
47 #define BLOCK_MAGIC 1100000005 /* file data block */
48 #ifdef AFS_PTHREAD_ENV
50 #define SLEEP(s) sleep(s)
52 #define POLL() IOMGR_Poll()
53 #define SLEEP(s) IOMGR_Sleep(s)
58 * 1) filemarks and block marks have the magic,bytes fields reversed. This
59 * is undoubtedly a bug. Also note that the two structures have
60 * inconsistent types, overlaying int and afs_int32.
61 * 2) When a volume is dumped, if the volume is locked, the dump will produce
62 * an anomalous tape format of the form:
66 * The design of the original butm code means that this cannot be
67 * handled correctly. The code is modified so that ReadFileData
68 * returns BUTM_ENDVOLUME if it encounters the s/w filemark.
71 /* data organization on tape:
72 * all writes are in blocks of BUTM_BLOCKSIZE (= sizeof(blockMark) + BUTM_BLKSIZE)
73 * blockMark contains a magic number and counts of real data bytes
74 * written out in the block.
76 * each file is preceeded by a fileMark, which acts as the file
77 * delimiter. A file consists of contigous data blocks. TM does
78 * understand or interrpet the data in data blocks.
80 * The tape begins with a tape label and ends with EOF file markers
81 * in succession (2 or 4 of them ).
85 struct fileMark { /* in network byte order */
92 struct butm_tapeLabel label;
96 usd_handle_t fid; /* file id of simulated tape */
97 afs_int32 mountId; /* current mountId */
98 afs_int32 reading; /* read file operation in progress */
99 afs_int32 writing; /* write file operation in progress */
102 static struct configuration {
103 char tapedir[64]; /* directory to create "tapes" */
104 afs_int32 mountId; /* for detecting simultaneous mounts */
105 afs_uint32 tapeSize; /* size of simulated tapes */
106 afs_uint32 fileMarkSize; /* size of file mark, bytes */
107 afs_int32 portOffset; /* port + portOffset is used by TC to listen */
110 static char *whoami = "file_tm";
111 char tapeBlock[BUTM_BLOCKSIZE]; /* Tape buffer for reads and writes */
113 #define BLOCK_LABEL 0 /* read/write a tape label */
114 #define BLOCK_FMBEGIN 1 /* read/write a begin FileMark */
115 #define BLOCK_DATA 2 /* read/write a data block */
116 #define BLOCK_FMEND 3 /* read/write an end FileMark */
117 #define BLOCK_EOD 4 /* read/write an EOD FileMark */
118 #define BLOCK_EOF 5 /* tape block is a HW EOF mark (usually error) */
119 #define BLOCK_UNKNOWN 6 /* tape block is unknwon (error) */
124 #define READS (((struct progress *)(info->tmRock))->reading)
125 #define WRITES (((struct progress *)(info->tmRock))->writing)
127 /* ----------------------------------------------------------------------
128 * These routines use the usd library to perform tape operations.
129 * ForkIoctl, ForkOpen, ForkClose, ForwardSpace, BackwardSpace, WriteEOF,
130 * PrepareAccess(nt), ShutdownAccess(nt)
132 * Return Values: USD functions return 0 if successful or errno if failed.
134 /* ------------------ USD Interface Functions Begin ------------------------ */
137 * On Unix, fork a child process to perform an IOCTL call. This avoids
138 * blocking the entire process during a tape operation
142 /* Unix version of function */
145 ForkIoctl(usd_handle_t fd, int op, int count)
147 int rc; /* return code from system calls */
148 int i; /* loop index */
149 int pid; /* process ID of child process */
150 int status; /* exit status of child process */
151 int ioctl_rc; /* return code from ioctl call */
152 int pipefd[2]; /* pipe for child return status */
153 int forkflag; /* flag set when ready to fork */
154 usd_tapeop_t tapeop; /* tape operation specification */
157 #ifdef AFS_PTHREAD_ENV
158 forkflag = 0; /* No need to fork if using pthreads */
163 /* initialize tape command structure */
165 tapeop.tp_count = count;
167 /* create pipe for getting return code from child */
171 printf("butm: Can't open pipe for IOCTL process. Error %d\n",
182 printf("butm: Can't fork IOCTL process. Error %d\n", errno);
188 if (!forkflag) { /* problem starting child process */
189 /* do the ioctl anyway, it will probably work */
190 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
191 } else if (pid == 0) { /* child process */
192 /* close all unneccessary file descriptors */
193 /* note: as painful as it is, we have to reach under the covers of
194 * the usd package to implement this functionality.
196 unixfd = (intptr_t)(fd->handle);
198 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
199 if (i != unixfd && i != pipefd[1]) {
204 /* do the ioctl call */
205 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
208 * Send the return code back to the parent.
209 * If this fails, there's nothing we can do, but we must test
210 * it in order to avoid complier warnings on some platforms.
212 if (write(pipefd[1], &ioctl_rc, sizeof(int)) < 0)
216 } else { /* parent process */
220 /* read the result from the child process */
221 rc = read(pipefd[0], &ioctl_rc, sizeof(int));
222 if (rc != sizeof(int)) {
223 /* tape is now in unknown state */
224 printf("butm: Can't determine IOCTL child status. Error %d\n",
231 /* get the completion status from the child process */
232 rc = waitpid(pid, &status, 0);
233 while (rc < 0 && errno == EINTR) {
234 rc = waitpid(pid, &status, 0);
237 printf("butm: Can't determine IOCTL child status. Error %d\n",
239 } else if (status != 0) {
241 ("butm: Unexpected IOCTL process status 0x%04x . Error %d\n",
250 /* NT version of function */
253 ForkIoctl(usd_handle_t fd, int op, int count)
257 /* Issue requested tape control */
259 tapeop.tp_count = count;
261 return (USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop));
263 #endif /* !AFS_NT40_ENV */
267 * On Unix, fork a child process to attempt to open the drive. We want to make
268 * certain there is tape in the drive before trying to open the device
269 * in the main process
273 /* Unix version of function. */
276 ForkOpen(char *device)
278 int rc; /* return code from system calls */
279 int i; /* loop index */
280 int pid; /* process ID of child process */
281 int status; /* exit status of child process */
282 int open_rc; /* return code from open */
283 int pipefd[2]; /* pipe for child return status */
284 int forkflag; /* flag set when ready to fork */
285 usd_handle_t fd; /* handle returned from open */
287 #ifdef AFS_PTHREAD_ENV
288 forkflag = 0; /* No need to fork if using pthreads */
293 /* create pipe for getting return code from child */
297 printf("butm: Cannot create pipe for OPEN process. Error %d\n",
308 printf("butm: Cannot create child process for OPEN. Error %d\n",
314 if (!forkflag) { /* problem starting child process */
316 *return success, the caller will discover any problems
317 * when it opens the device.
320 } else if (pid == 0) { /* child process */
321 /* close all unneccessary file descriptors */
322 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
323 if (i != pipefd[1]) {
329 open_rc = usd_Open(device, USD_OPEN_RDONLY, 0, &fd);
336 * Send the return code back to the parent.
337 * If this fails, there's nothing we can do, but we must test
338 * it in order to avoid complier warnings on some platforms.
340 if (write(pipefd[1], &open_rc, sizeof(open_rc)) < 0)
344 } else { /* parent process */
349 /* read the result from the child process */
350 rc = read(pipefd[0], &open_rc, sizeof(open_rc));
351 if (rc != sizeof(open_rc)) {
352 /* this is not a problem since we will reopen the device anyway */
353 printf("butm: No response from OPEN process. Error %d\n", errno);
359 /* get the completion status from the child process */
360 rc = waitpid(pid, &status, 0);
361 while (rc < 0 && errno == EINTR) {
362 rc = waitpid(pid, &status, 0);
365 printf("butm: Cannot get status of OPEN process. Error %d\n",
367 } else if (status != 0) {
369 ("butm: Unexpected OPEN process exit status 0x%04x. Error %d \n",
378 /* NT version of function. */
381 ForkOpen(char *device)
385 #endif /* AFS_NT40_ENV */
388 * On Unix, fork a child process to close the drive. If the drive rewinds
389 * on close it could cause the process to block.
393 /* Unix version of function */
396 ForkClose(usd_handle_t fd)
398 int rc; /* return code from system calls */
399 int i; /* loop index */
400 int pid; /* process ID of child process */
401 int status; /* exit status of child process */
402 int close_rc, parent_close_rc; /* return codes from close */
403 int pipefd[2]; /* pipe for child return status */
404 int ctlpipe[2]; /* pipe for message to child */
405 int forkflag; /* flag set when ready to fork */
408 #ifdef AFS_PTHREAD_ENV
409 forkflag = 0; /* No need to fork if using pthreads */
414 /* create pipe for getting return code from child */
418 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
424 /* create pipe for notifying child when to close */
430 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
443 printf("butm: Cannot create CLOSE child process. Error %d\n",
449 if (!forkflag) { /* problem starting child process */
450 close_rc = USD_CLOSE(fd);
451 parent_close_rc = close_rc;
452 } else if (pid == 0) { /* child process */
453 /* close all unneccessary file descriptors */
454 /* note: as painful as it is, we have to reach under the covers of
455 * the usd package to implement this functionality.
457 unixfd = (intptr_t)(fd->handle);
459 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
460 if (i != unixfd && i != ctlpipe[0] && i != pipefd[1]) {
466 * The parent writes the control pipe after it closes the device.
467 * We don't actually care about the read; we're just using it to
468 * block until the parent is ready. But we must do something
469 * with the result, to avoid complier warnings on some platforms.
471 if (read(ctlpipe[0], &close_rc, sizeof(int)) < 0)
476 close_rc = USD_CLOSE(fd);
479 * Send the return code back to the parent.
480 * If this fails, there's nothing we can do, but we must test
481 * it in order to avoid complier warnings on some platforms.
483 if (write(pipefd[1], &close_rc, sizeof(int)) < 0)
487 } else { /* parent process */
494 * close the device, this should have no effect as long as the
495 * child has not closed
498 parent_close_rc = USD_CLOSE(fd);
500 /* notify the child to do its close */
501 rc = write(ctlpipe[1], &close_rc, sizeof(int)); /* just send garbage */
502 if (rc != sizeof(int)) {
503 printf("butm: Error communicating with CLOSE process. Error %d\n",
508 /* read the result from the child process */
509 rc = read(pipefd[0], &close_rc, sizeof(int));
510 if (rc != sizeof(int)) {
511 /* logging is enough, since we wrote a file mark the */
512 /* return code from the close doesn't really matter */
513 printf("butm: No response from CLOSE process. Error %d\n",
520 /* get the completion status from the child process */
521 rc = waitpid(pid, &status, 0);
522 while (rc < 0 && errno == EINTR) {
523 rc = waitpid(pid, &status, 0);
526 printf("butm: Cannot get status of CLOSE process. Error %d\n",
528 } else if (status != 0) {
530 ("butm: Unexpected exit status 0x%04x from CLOSE process. Error %d\n",
534 /* if either process received an error, then return an error */
535 if (parent_close_rc < 0) {
536 close_rc = parent_close_rc;
544 /* NT version of function */
547 ForkClose(usd_handle_t fd)
549 return (USD_CLOSE(fd));
551 #endif /* AFS_NT40_ENV */
553 /* Forward space file */
555 ForwardSpace(usd_handle_t fid, int count)
562 return (ForkIoctl(fid, USDTAPE_FSF, count));
566 /* Backward space file */
568 BackwardSpace(usd_handle_t fid, int count)
575 return (ForkIoctl(fid, USDTAPE_BSF, count));
579 /* write end of file mark */
581 WriteEOF(usd_handle_t fid, int count)
588 return (ForkIoctl(fid, USDTAPE_WEOF, count));
594 Rewind(usd_handle_t fid)
599 return (USD_SEEK(fid, 0, SEEK_SET, &stopOff));
601 return (ForkIoctl(fid, USDTAPE_REW, 0));
605 /* prepare tape drive for access */
607 PrepareAccess(usd_handle_t fid)
612 (void)ForkIoctl(fid, USDTAPE_PREPARE, 0);
614 /* NT won't rewind tape when it is opened */
616 #endif /* AFS_NT40_ENV */
620 /* decommission tape drive after all accesses complete */
622 ShutdownAccess(usd_handle_t fid)
626 (void)ForkIoctl(fid, USDTAPE_SHUTDOWN, 0);
628 #endif /* AFS_NT40_ENV */
632 /* -------------------- USD Interface Functions End ----------------------- */
634 /* =====================================================================
636 * ===================================================================== */
639 * add the supplied no. of bytes to the byte count of information placed
642 * dataSize - bytes used on the tape
646 incSize(struct butm_tapeInfo *info, afs_uint32 dataSize)
648 info->nBytes += dataSize;
649 info->kBytes += (info->nBytes / 1024);
650 info->nBytes %= 1024;
654 * IF YOU ADD/CHANGE THE ifdefs, BE SURE
655 * TO ALSO MAKE THE SAME CHANGES IN bu_utils/fms.c.
657 * add the supplied no. of bytes to the byte count of data placed
658 * on the tape. Also check for reaching 2GB limit and reset the
659 * pointer if necessary. This allows us to use >2GB tapes.
661 * fid - file id for the tape.
662 * dataSize - bytes used on the tape
666 incPosition(struct butm_tapeInfo *info, usd_handle_t fid, afs_uint32 dataSize)
668 /* Add this to the amount of data written to the tape */
669 incSize(info, dataSize);
671 info->posCount += dataSize;
673 if (info->posCount >= 2147467264) { /* 2GB - 16K */
675 #if (defined(AFS_SUN_ENV) || defined(AFS_LINUX24_ENV))
680 USD_IOCTL(fid, USD_IOCTL_SETSIZE, &off);
687 * This accounts for tape drives with a block size different from variable or 16K
688 * blocks and only reads that block size.
690 afs_int32 TapeBlockSize;
692 readData(usd_handle_t fid, char *data, afs_uint32 totalSize, afs_int32 *errorP)
694 afs_int32 toread; /* Number of bytes to read */
695 afs_uint32 rSize; /* Total bytes read so far */
696 afs_uint32 tSize; /* Temporary size */
697 afs_int32 rc; /* return code */
699 toread = totalSize; /* First, try to read all the data */
701 rc = USD_READ(fid, &data[0], toread, &rSize);
706 if (rSize == 0) /* reached EOF */
709 if (rSize != TapeBlockSize) { /* Tape block size has changed */
710 TapeBlockSize = rSize;
711 printf("Tape blocks read in %d Byte chunks.\n", TapeBlockSize);
714 /* Read the rest of the data in */
715 while (rSize < totalSize) {
717 ((totalSize - rSize) <
718 TapeBlockSize ? (totalSize - rSize) : TapeBlockSize);
719 rc = USD_READ(fid, &data[rSize], toread, &tSize);
728 if (rSize > totalSize)
729 printf("readData - Read > 16K data block - continuing.\n");
735 SeekFile(struct butm_tapeInfo *info, int count)
743 p = (struct progress *)info->tmRock;
745 if (isafile) { /* no reason for seeking through a file */
746 p->reading = p->writing = 0;
753 error = ForwardSpace(p->fid, count);
755 error = BackwardSpace(p->fid, -count);
760 info->status |= BUTM_STATUS_SEEKERROR;
761 ERROR_EXIT(BUTM_IOCTL);
764 info->position += count;
765 incSize(info, (count * config.fileMarkSize));
766 p = (struct progress *)info->tmRock;
767 p->reading = p->writing = 0;
775 /* Step to the next filemark if we are not at one already */
777 NextFile(struct butm_tapeInfo *info)
781 if (!READS && !WRITES)
784 code = SeekFile(info, 1);
789 WriteTapeBlock(struct butm_tapeInfo *info,
790 char *buffer, /* assumed to be 16384 bytes with data in it */
791 afs_int32 length, /* amount data in buffer */
794 afs_int32 code = 0, rc = 0;
796 struct tapeLabel *label;
797 struct fileMark *fmark;
798 struct blockMark *bmark;
802 p = (struct progress *)info->tmRock;
804 if (blockType == BLOCK_DATA) { /* Data Block */
807 bmark = (struct blockMark *)buffer;
808 memset(bmark, 0, sizeof(struct blockMark));
809 bmark->magic = htonl(BLOCK_MAGIC);
810 bmark->count = htonl(length);
811 } else if (blockType == BLOCK_FMBEGIN) { /* Filemark - begin */
812 fmark = (struct fileMark *)buffer;
813 fmark->magic = htonl(FILE_MAGIC);
814 fmark->nBytes = htonl(FILE_BEGIN);
815 } else if (blockType == BLOCK_FMEND) { /* Filemark - end */
816 fmark = (struct fileMark *)buffer;
817 fmark->magic = htonl(FILE_MAGIC);
818 fmark->nBytes = htonl(FILE_FMEND);
819 } else if (blockType == BLOCK_LABEL) { /* Label */
820 label = (struct tapeLabel *)buffer;
821 label->magic = htonl(TAPE_MAGIC);
822 } else if (blockType == BLOCK_EOD) { /* Filemark - EOD mark */
823 fmark = (struct fileMark *)buffer;
824 fmark->magic = htonl(FILE_MAGIC);
825 fmark->nBytes = htonl(FILE_EOD);
828 /* Write the tape block */
829 /* -------------------- */
830 rc = USD_WRITE(p->fid, buffer, BUTM_BLOCKSIZE, &wsize);
831 if ((rc == 0) && (wsize > 0)) {
832 incPosition(info, p->fid, wsize); /* record whats written */
836 if (wsize != BUTM_BLOCKSIZE) {
837 info->status |= BUTM_STATUS_WRITEERROR;
842 ERROR_EXIT(BUTM_EOT);
847 /* Write trailing EOF marker for some block types */
848 /* ---------------------------------------------- */
849 if ((blockType == BLOCK_FMEND) || (blockType == BLOCK_LABEL)
850 || (blockType == BLOCK_EOD)) {
853 error = WriteEOF(p->fid, 1);
856 info->status |= BUTM_STATUS_WRITEERROR;
857 ERROR_EXIT(BUTM_IOCTL);
860 incSize(info, config.fileMarkSize);
873 ReadTapeBlock(struct butm_tapeInfo *info,
874 char *buffer, /* assumed to be 16384 bytes */
875 afs_int32 *blockType)
878 afs_int32 rsize, fmtype;
879 struct tapeLabel *label;
880 struct fileMark *fmark;
881 struct blockMark *bmark;
884 *blockType = BLOCK_UNKNOWN;
886 p = (struct progress *)info->tmRock;
888 memset(buffer, 0, BUTM_BLOCKSIZE);
889 label = (struct tapeLabel *)buffer;
890 fmark = (struct fileMark *)buffer;
891 bmark = (struct blockMark *)buffer;
893 rsize = readData(p->fid, buffer, BUTM_BLOCKSIZE, &info->error);
895 incPosition(info, p->fid, rsize);
899 if (rsize == 0) { /* Read a HW EOF Marker? OK */
900 *blockType = BLOCK_EOF;
901 incSize(info, config.fileMarkSize); /* Size of filemark */
903 info->position++; /* bump position */
904 p->reading = 0; /* No reads since EOF */
907 else if (rsize != BUTM_BLOCKSIZE) { /* Didn't Read a full block */
908 info->status |= BUTM_STATUS_READERROR;
909 ERROR_EXIT((rsize < 0) ? BUTM_IO : BUTM_EOT);
912 else if (ntohl(bmark->magic) == BLOCK_MAGIC) { /* Data block? */
913 *blockType = BLOCK_DATA;
916 else if (ntohl(fmark->magic) == FILE_MAGIC) { /* Read a filemark? */
917 fmtype = ntohl(fmark->nBytes);
919 if (fmtype == FILE_BEGIN) { /* filemark begin */
920 *blockType = BLOCK_FMBEGIN;
921 } else if (fmtype == FILE_FMEND) { /* filemark end */
922 *blockType = BLOCK_FMEND;
923 code = SeekFile(info, 1);
924 } else if (fmtype == FILE_EOD) { /* EOD mark */
925 *blockType = BLOCK_EOD;
926 info->status |= BUTM_STATUS_EOD;
927 code = SeekFile(info, 1);
931 else if (ntohl(label->magic) == TAPE_MAGIC) { /* Read a tape label? */
932 *blockType = BLOCK_LABEL;
933 code = SeekFile(info, 1);
944 * check version numbers and permissions in the info structure
948 check(struct butm_tapeInfo *info,
949 int write) /* write operation requested */
954 return (BUTM_BADARGUMENT);
956 /* Check version number in info structure */
957 if (info->structVersion != BUTM_MAJORVERSION)
958 return BUTM_OLDINTERFACE;
960 /* Check if a tape is mounted */
961 if (((p = (struct progress *)info->tmRock) == 0) || (p->fid == 0))
964 /* If writing check if there is write access */
965 if (write && (info->flags & BUTM_FLAGS_READONLY))
966 return BUTM_READONLY;
972 rewindFile(struct butm_tapeInfo *info)
978 p = (struct progress *)info->tmRock;
982 error = Rewind(p->fid);
987 info->status |= BUTM_STATUS_SEEKERROR;
988 ERROR_EXIT(BUTM_IOCTL);
991 info->position = (isafile ? 0 : 1);
992 info->kBytes = info->nBytes = 0;
993 info->nFiles = info->nRecords = 0;
994 p->reading = p->writing = 0;
1002 /* =====================================================================
1004 * ===================================================================== */
1007 file_Mount(struct butm_tapeInfo *info, char *tape)
1013 afs_int32 code = 0, error = 0, rc = 0;
1016 ERROR_EXIT(BUTM_BADARGUMENT);
1019 printf("butm: Mount tape drive\n");
1024 if (info->structVersion != BUTM_MAJORVERSION)
1025 ERROR_EXIT(BUTM_OLDINTERFACE);
1027 ERROR_EXIT(BUTM_PARALLELMOUNTS);
1028 if (strlen(tape) >= sizeof(info->name))
1029 ERROR_EXIT(BUTM_BADARGUMENT);
1031 strcpy(info->name, tape);
1033 strcpy(filename, config.tapedir); /* the name of the tape device */
1034 info->position = (isafile ? 0 : 1);
1035 info->kBytes = info->nBytes = 0;
1036 info->nRecords = info->nFiles = 0;
1037 info->recordSize = 0;
1038 info->tapeSize = config.tapeSize;
1039 info->coefBytes = 1;
1040 info->coefRecords = 0;
1041 info->coefFiles = sizeof(struct fileMark);
1042 info->simultaneousTapes = 1;
1045 info->flags = BUTM_FLAGS_SEQUENTIAL;
1049 xflags |= USD_OPEN_CREATE;
1052 * try to open in a child process first so nothing will
1053 * time out should the process block because the device
1057 if (ForkOpen(filename)) {
1058 ERROR_EXIT(BUTM_MOUNTFAIL);
1062 /* Now go ahead and open the tape drive for real */
1063 rc = usd_Open(filename, (USD_OPEN_RDWR | USD_OPEN_WLOCK | xflags), 0777,
1065 if (rc != 0) { /* try for lesser access */
1066 rc = usd_Open(filename, (USD_OPEN_RDONLY | USD_OPEN_RLOCK), 0, &fid);
1070 ERROR_EXIT(BUTM_MOUNTFAIL);
1072 info->flags |= BUTM_FLAGS_READONLY;
1075 (void)PrepareAccess(fid); /* for NT */
1077 p = malloc(sizeof(*p));
1078 info->tmRock = (char *)p;
1080 p->mountId = config.mountId = time(0);
1081 p->reading = p->writing = 0;
1083 TapeBlockSize = BUTM_BLOCKSIZE; /* Initialize */
1087 info->error = error;
1092 file_Dismount(struct butm_tapeInfo *info)
1095 afs_int32 code = 0, error = 0;
1098 printf("butm: Unmount tape drive\n");
1103 code = check(info, READ_OP);
1107 p = (struct progress *)info->tmRock;
1109 (void)ShutdownAccess(p->fid); /* for NT */
1111 /* close the device */
1112 if ((error = ForkClose(p->fid))) {
1113 printf("butm: Tape close failed. Error %d\n", errno);
1119 code = BUTM_DISMOUNTFAIL;
1120 info->status |= BUTM_STATUS_TAPEERROR;
1124 info->tmRock = 0; /* mark it as closed - even if error on close */
1130 info->error = error;
1135 * write the header on a tape
1137 * info - handle on tape unit
1138 * label - label information. This label is not copied onto the tape.
1139 * If supplied, various fields are copied from this label to
1140 * the actual tape label written on the tape.
1144 file_WriteLabel(struct butm_tapeInfo *info, struct butm_tapeLabel *label,
1149 struct tapeLabel *tlabel;
1151 afs_int64 off; /* offset */
1154 printf("butm: Write tape label\n");
1159 code = check(info, WRITE_OP);
1163 ERROR_EXIT(BUTM_BADARGUMENT);
1164 if (label->structVersion != CUR_TAPE_VERSION)
1165 ERROR_EXIT(BUTM_OLDINTERFACE);
1167 if (rewind) { /* Not appending, so rewind */
1168 code = rewindFile(info);
1173 p = (struct progress *)info->tmRock;
1175 code = USD_IOCTL(p->fid, USD_IOCTL_SETSIZE, &off);
1177 ERROR_EXIT(BUTM_POSITION);
1180 if (READS || WRITES)
1181 ERROR_EXIT(BUTM_BADOP);
1184 /* Copy the label into the tape block
1185 * ---------------------------------- */
1186 memset(tapeBlock, 0, BUTM_BLOCKSIZE);
1188 if (!label->creationTime)
1189 label->creationTime = time(0);
1191 tlabel = (struct tapeLabel *)tapeBlock;
1192 memcpy(&tlabel->label, label, sizeof(struct butm_tapeLabel));
1193 tlabel->label.structVersion = htonl(CUR_TAPE_VERSION);
1194 tlabel->label.creationTime = htonl(tlabel->label.creationTime);
1195 tlabel->label.expirationDate = htonl(tlabel->label.expirationDate);
1196 tlabel->label.size = htonl(tlabel->label.size);
1197 tlabel->label.useCount = htonl(tlabel->label.useCount);
1198 tlabel->label.dumpid = htonl(tlabel->label.dumpid);
1201 * write the tape label - For appends, the write may need to skip
1202 * over 1 or 2 EOF marks that were written when tape was closed after
1203 * the last dump. Plus, some AIX tape drives require we try forwarding
1204 * over the last EOF and take an error before we can write the new label.
1205 * ---------------------------------------------------------------------- */
1206 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1207 if (!isafile && !rewind && (code == BUTM_IO))
1208 do { /* do if write failed */
1209 fcode = SeekFile(info, 1); /* skip over the EOF */
1211 break; /* leave if error */
1214 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1215 if (code != BUTM_IO)
1216 break; /* continue if write failed */
1218 fcode = SeekFile(info, 1); /* skip over the EOF */
1219 if (fcode) { /* retry 1 write if couldn't skip */
1221 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1227 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1228 if (code != BUTM_IO)
1229 break; /* continue if write failed */
1231 fcode = SeekFile(info, 1); /* skip over the EOF */
1232 if (fcode) { /* retry 1 write if couldn't skip */
1234 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1241 /* clear the write error status a failed WriteTapeBlock may have produced */
1243 info->status &= ~BUTM_STATUS_WRITEERROR;
1250 file_ReadLabel(struct butm_tapeInfo *info, struct butm_tapeLabel *label,
1253 struct tapeLabel *tlabel;
1255 afs_int32 blockType;
1258 printf("butm: Read tape label\n");
1263 code = check(info, READ_OP);
1266 if (READS || WRITES)
1267 ERROR_EXIT(BUTM_BADOP);
1270 code = rewindFile(info);
1272 ERROR_EXIT(code); /* status is set so return */
1276 * When appended labels were written, either 1 or 2 EOF marks may
1277 * have had to be skipped. When reading a label, these EOF marks
1278 * must also be skipped. When an EOF is read, 0 bytes are returned
1279 * (refer to the write calls in file_WriteLabel routine).
1280 * ---------------------------------------------------------------- */
1281 code = ReadTapeBlock(info, tapeBlock, &blockType);
1282 if (!isafile && !rewind && (blockType == BLOCK_EOF))
1284 code = ReadTapeBlock(info, tapeBlock, &blockType);
1285 if (blockType != BLOCK_EOF)
1286 break; /* didn't read an EOF */
1288 code = ReadTapeBlock(info, tapeBlock, &blockType);
1291 if (blockType == BLOCK_UNKNOWN)
1292 ERROR_EXIT(BUTM_NOLABEL);
1295 if (blockType != BLOCK_LABEL)
1296 ERROR_EXIT(BUTM_BADBLOCK);
1301 tlabel = (struct tapeLabel *)tapeBlock;
1302 memcpy(label, &tlabel->label, sizeof(struct butm_tapeLabel));
1303 label->structVersion = ntohl(label->structVersion);
1304 label->creationTime = ntohl(label->creationTime);
1305 label->expirationDate = ntohl(label->expirationDate);
1306 label->size = ntohl(label->size);
1307 label->dumpid = ntohl(label->dumpid);
1308 label->useCount = ntohl(label->useCount);
1310 info->tapeSize = label->size; /* use size from label */
1318 file_WriteFileBegin(struct butm_tapeInfo *info)
1323 printf("butm: Write filemark begin\n");
1327 code = check(info, WRITE_OP);
1331 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMBEGIN);
1342 file_ReadFileBegin(struct butm_tapeInfo *info)
1345 afs_int32 blockType;
1348 printf("butm: Read filemark begin\n");
1353 code = check(info, READ_OP);
1356 if (READS || WRITES)
1357 ERROR_EXIT(BUTM_BADOP);
1359 code = ReadTapeBlock(info, tapeBlock, &blockType);
1363 if (blockType != BLOCK_FMBEGIN) {
1364 if (blockType == BLOCK_EOD)
1365 ERROR_EXIT(BUTM_EOD); /* EODump label */
1366 if (blockType == BLOCK_LABEL)
1367 ERROR_EXIT(BUTM_LABEL); /* Tape label */
1368 ERROR_EXIT(BUTM_BADBLOCK); /* Other */
1375 /* Writes data out in block sizes of 16KB. Does destroy the data.
1376 * Assumes the data buffer has a space reserved at beginning for a blockMark.
1379 file_WriteFileData(struct butm_tapeInfo *info, char *data, afs_int32 blocks, afs_int32 len)
1384 char *bstart; /* Where block starts for a 16K block */
1385 char *dstart; /* Where data starts for a 16K block */
1388 printf("butm: Write tape data - %u bytes\n", len);
1393 code = check(info, WRITE_OP);
1396 if (!data || (len < 0))
1397 ERROR_EXIT(BUTM_BADARGUMENT);
1398 if (READS || !WRITES)
1399 ERROR_EXIT(BUTM_BADOP);
1401 b = 0; /* start at block 0 */
1403 dstart = &data[b * BUTM_BLKSIZE];
1404 bstart = dstart - sizeof(struct blockMark);
1406 if (len < BUTM_BLKSIZE) {
1407 memset(&dstart[len], 0, BUTM_BLKSIZE - len);
1410 length = BUTM_BLKSIZE;
1413 code = WriteTapeBlock(info, bstart, length, BLOCK_DATA);
1417 /* If there are more blocks, step to next block */
1418 /* Otherwise, copy the data to beginning of last block */
1420 if (b < (blocks - 1))
1423 memcpy(&dstart[0], &dstart[BUTM_BLKSIZE], len);
1430 /* file_ReadFileData
1431 * Read a data block from tape.
1433 * info - tape info structure, c.f. fid
1434 * data - ptr to buffer for data
1435 * len - size of data buffer
1437 * nBytes - no. of data bytes read.
1441 file_ReadFileData(struct butm_tapeInfo *info, char *data, int len, int *nBytes)
1443 struct blockMark *bmark;
1445 afs_int32 blockType;
1448 printf("butm: Read tape data - %u bytes\n", len);
1454 ERROR_EXIT(BUTM_BADARGUMENT);
1457 code = check(info, READ_OP);
1460 if (!data || (len < 0) || (len > BUTM_BLKSIZE))
1461 ERROR_EXIT(BUTM_BADARGUMENT);
1462 if (!READS || WRITES)
1463 ERROR_EXIT(BUTM_BADOP);
1465 data -= sizeof(struct blockMark);
1466 code = ReadTapeBlock(info, data, &blockType);
1470 if (blockType != BLOCK_DATA) {
1471 if (blockType == BLOCK_EOF)
1472 ERROR_EXIT(BUTM_EOF);
1473 if (blockType == BLOCK_FMEND)
1474 ERROR_EXIT(BUTM_ENDVOLUME);
1475 ERROR_EXIT(BUTM_BADBLOCK);
1478 bmark = (struct blockMark *)data;
1479 *nBytes = ntohl(bmark->count); /* Size of data in buf */
1486 file_WriteFileEnd(struct butm_tapeInfo *info)
1491 printf("butm: Write filemark end\n");
1496 code = check(info, WRITE_OP);
1499 if (READS || !WRITES)
1500 ERROR_EXIT(BUTM_BADOP);
1502 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMEND);
1508 /* Read a SW filemark, verify that it is a SW filemark, then skip to the next
1509 * HW filemark. If the read of the SW filemark shows it's an EOF, then
1510 * ignore that the SW filemark is not there and return 0 (found the SW filemark
1511 * missing with some 3.1 dumps).
1514 file_ReadFileEnd(struct butm_tapeInfo *info)
1517 afs_int32 blockType;
1520 printf("butm: Read filemark end\n");
1525 code = check(info, READ_OP);
1528 if (!READS || WRITES)
1529 ERROR_EXIT(BUTM_BADOP);
1531 info->status &= ~BUTM_STATUS_EOF;
1533 code = ReadTapeBlock(info, tapeBlock, &blockType);
1537 if ((blockType != BLOCK_FMEND) && (blockType != BLOCK_EOF))
1538 ERROR_EXIT(BUTM_BADBLOCK);
1545 * Write the end-of-dump marker.
1548 file_WriteEODump(struct butm_tapeInfo *info)
1553 printf("butm: Write filemark EOD\n");
1558 code = check(info, WRITE_OP);
1561 if (READS || WRITES)
1562 ERROR_EXIT(BUTM_BADOP);
1564 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_EOD);
1568 info->status |= BUTM_STATUS_EOD;
1575 file_Seek(struct butm_tapeInfo *info, afs_int32 position)
1581 afs_int64 stopOff; /* for normal file(non-tape) seeks */
1584 printf("butm: Seek to the tape position %d\n", position);
1588 code = check(info, READ_OP);
1593 p = (struct progress *)info->tmRock;
1594 posit = (osi_lloff_t) position *(osi_lloff_t) BUTM_BLOCKSIZE;
1596 w = USD_SEEK(p->fid, posit, SEEK_SET, &stopOff);
1599 if (posit != stopOff)
1600 ERROR_EXIT(BUTM_POSITION);
1602 p->reading = p->writing = 0;
1603 info->position = position;
1605 /* Don't position backwards if we are in-between FMs */
1606 if ((READS || WRITES) && ((position - info->position) <= 0))
1607 ERROR_EXIT(BUTM_BADOP);
1609 code = SeekFile(info, (position - info->position));
1619 * Seek to the EODump (end-of-dump) after the given position. This is
1620 * the position after the EOF filemark immediately after the EODump mark.
1621 * This is for tapes of version 4 or greater.
1624 file_SeekEODump(struct butm_tapeInfo *info, afs_int32 position)
1627 afs_int32 blockType;
1630 afs_int64 stopOff; /* file seek offsets */
1633 printf("butm: Seek to end-of-dump\n");
1636 code = check(info, READ_OP);
1639 if (READS || WRITES)
1640 ERROR_EXIT(BUTM_BADOP);
1643 p = (struct progress *)info->tmRock;
1644 w = USD_SEEK(p->fid, 0, SEEK_END, &stopOff);
1647 ERROR_EXIT(BUTM_POSITION);
1650 if (stopOff % BUTM_BLOCKSIZE)
1651 ERROR_EXIT(BUTM_POSITION);
1652 info->position = (stopOff / BUTM_BLOCKSIZE);
1654 /* Seek to the desired position */
1655 code = SeekFile(info, (position - info->position) + 1);
1660 * Search until the filemark is an EODump filemark.
1661 * Skip over volumes only.
1664 code = ReadTapeBlock(info, tapeBlock, &blockType);
1668 if (blockType == BLOCK_EOD)
1670 if (blockType != BLOCK_FMBEGIN)
1671 ERROR_EXIT(BUTM_BADBLOCK);
1673 code = SeekFile(info, 1); /* Step forward to next volume */
1685 file_SetSize(struct butm_tapeInfo *info, afs_uint32 size)
1688 printf("butm: Set size of tape\n");
1692 info->tapeSize = config.tapeSize;
1694 info->tapeSize = size;
1699 file_GetSize(struct butm_tapeInfo *info, afs_uint32 *size)
1702 printf("butm: Get size of tape\n");
1705 *size = info->tapeSize;
1709 /* =====================================================================
1710 * Startup/configuration routines.
1711 * ===================================================================== */
1714 file_Configure(struct tapeConfig *file)
1717 afs_com_err(whoami, BUTM_BADCONFIG, "device not specified");
1718 return BUTM_BADCONFIG;
1721 config.tapeSize = file->capacity;
1722 config.fileMarkSize = file->fileMarkSize;
1723 config.portOffset = file->portOffset;
1724 strcpy(config.tapedir, file->device);
1726 /* Tape must be large enough to at least fit a label */
1727 if (config.tapeSize <= 0) {
1728 afs_com_err(whoami, BUTM_BADCONFIG, "Tape size bogus: %d Kbytes",
1730 return BUTM_BADCONFIG;
1733 if (strlen(config.tapedir) == 0) {
1734 afs_com_err(whoami, BUTM_BADCONFIG, "no tape device specified");
1735 return BUTM_BADCONFIG;
1742 /* This procedure instantiates a tape module of type file_tm. */
1744 butm_file_Instantiate(struct butm_tapeInfo *info, struct tapeConfig *file)
1746 extern int debugLevel;
1749 if (debugLevel > 98)
1750 printf("butm: Instantiate butc\n");
1753 ERROR_EXIT(BUTM_BADARGUMENT);
1754 if (info->structVersion != BUTM_MAJORVERSION)
1755 ERROR_EXIT(BUTM_OLDINTERFACE);
1757 memset(info, 0, sizeof(struct butm_tapeInfo));
1758 info->structVersion = BUTM_MAJORVERSION;
1759 info->ops.mount = file_Mount;
1760 info->ops.dismount = file_Dismount;
1761 info->ops.create = file_WriteLabel;
1762 info->ops.readLabel = file_ReadLabel;
1763 info->ops.seek = file_Seek;
1764 info->ops.seekEODump = file_SeekEODump;
1765 info->ops.readFileBegin = file_ReadFileBegin;
1766 info->ops.readFileData = file_ReadFileData;
1767 info->ops.readFileEnd = file_ReadFileEnd;
1768 info->ops.writeFileBegin = file_WriteFileBegin;
1769 info->ops.writeFileData = file_WriteFileData;
1770 info->ops.writeFileEnd = file_WriteFileEnd;
1771 info->ops.writeEOT = file_WriteEODump;
1772 info->ops.setSize = file_SetSize;
1773 info->ops.getSize = file_GetSize;
1774 info->debug = ((debugLevel > 98) ? 1 : 0);
1776 code = file_Configure(file);