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>
19 #include <netinet/in.h>
21 #include <sys/types.h>
29 #include <afs/com_err.h>
32 #include "error_macros.h"
37 #define FILE_MAGIC 1000000007 /* s/w file mark */
38 #define FILE_BEGIN 0 /* byte field in file mark */
39 #define FILE_FMEND 1 /* byte field in file mark */
40 #define FILE_EOD -1 /* byte field in file mark */
41 #define TAPE_MAGIC 1100000009 /* tape label block */
42 #define BLOCK_MAGIC 1100000005 /* file data block */
43 #ifdef AFS_PTHREAD_ENV
45 #define SLEEP(s) sleep(s)
47 #define POLL() IOMGR_Poll()
48 #define SLEEP(s) IOMGR_Sleep(s)
53 * 1) filemarks and block marks have the magic,bytes fields reversed. This
54 * is undoubtedly a bug. Also note that the two structures have
55 * inconsistent types, overlaying int and afs_int32.
56 * 2) When a volume is dumped, if the volume is locked, the dump will produce
57 * an anomalous tape format of the form:
61 * The design of the original butm code means that this cannot be
62 * handled correctly. The code is modified so that ReadFileData
63 * returns BUTM_ENDVOLUME if it encounters the s/w filemark.
66 /* data organization on tape:
67 * all writes are in blocks of BUTM_BLOCKSIZE (= sizeof(blockMark) + BUTM_BLKSIZE)
68 * blockMark contains a magic number and counts of real data bytes
69 * written out in the block.
71 * each file is preceeded by a fileMark, which acts as the file
72 * delimiter. A file consists of contigous data blocks. TM does
73 * understand or interrpet the data in data blocks.
75 * The tape begins with a tape label and ends with EOF file markers
76 * in succession (2 or 4 of them ).
80 struct fileMark { /* in network byte order */
87 struct butm_tapeLabel label;
91 usd_handle_t fid; /* file id of simulated tape */
92 afs_int32 mountId; /* current mountId */
93 afs_int32 reading; /* read file operation in progress */
94 afs_int32 writing; /* write file operation in progress */
97 static struct configuration {
98 char tapedir[64]; /* directory to create "tapes" */
99 afs_int32 mountId; /* for detecting simultaneous mounts */
100 afs_uint32 tapeSize; /* size of simulated tapes */
101 afs_uint32 fileMarkSize; /* size of file mark, bytes */
102 afs_int32 portOffset; /* port + portOffset is used by TC to listen */
105 static char *whoami = "file_tm";
106 char tapeBlock[BUTM_BLOCKSIZE]; /* Tape buffer for reads and writes */
108 #define BLOCK_LABEL 0 /* read/write a tape label */
109 #define BLOCK_FMBEGIN 1 /* read/write a begin FileMark */
110 #define BLOCK_DATA 2 /* read/write a data block */
111 #define BLOCK_FMEND 3 /* read/write an end FileMark */
112 #define BLOCK_EOD 4 /* read/write an EOD FileMark */
113 #define BLOCK_EOF 5 /* tape block is a HW EOF mark (usually error) */
114 #define BLOCK_UNKNOWN 6 /* tape block is unknwon (error) */
119 #define READS (((struct progress *)(info->tmRock))->reading)
120 #define WRITES (((struct progress *)(info->tmRock))->writing)
122 /* ----------------------------------------------------------------------
123 * These routines use the usd library to perform tape operations.
124 * ForkIoctl, ForkOpen, ForkClose, ForwardSpace, BackwardSpace, WriteEOF,
125 * PrepareAccess(nt), ShutdownAccess(nt)
127 * Return Values: USD functions return 0 if successful or errno if failed.
129 /* ------------------ USD Interface Functions Begin ------------------------ */
132 * On Unix, fork a child process to perform an IOCTL call. This avoids
133 * blocking the entire process during a tape operation
137 /* Unix version of function */
140 ForkIoctl(usd_handle_t fd, int op, int count)
142 int rc; /* return code from system calls */
143 int i; /* loop index */
144 int pid; /* process ID of child process */
145 int status; /* exit status of child process */
146 int ioctl_rc; /* return code from ioctl call */
147 int pipefd[2]; /* pipe for child return status */
148 int forkflag; /* flag set when ready to fork */
149 usd_tapeop_t tapeop; /* tape operation specification */
152 #ifdef AFS_PTHREAD_ENV
153 forkflag = 0; /* No need to fork if using pthreads */
158 /* initialize tape command structure */
160 tapeop.tp_count = count;
162 /* create pipe for getting return code from child */
166 printf("butm: Can't open pipe for IOCTL process. Error %d\n",
177 printf("butm: Can't fork IOCTL process. Error %d\n", errno);
183 if (!forkflag) { /* problem starting child process */
184 /* do the ioctl anyway, it will probably work */
185 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
186 } else if (pid == 0) { /* child process */
187 /* close all unneccessary file descriptors */
188 /* note: as painful as it is, we have to reach under the covers of
189 * the usd package to implement this functionality.
191 unixfd = (int)(fd->handle);
193 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
194 if (i != unixfd && i != pipefd[1]) {
199 /* do the ioctl call */
200 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
202 /* send the return code back to the parent */
203 write(pipefd[1], &ioctl_rc, sizeof(int));
206 } else { /* parent process */
210 /* read the result from the child process */
211 rc = read(pipefd[0], &ioctl_rc, sizeof(int));
212 if (rc != sizeof(int)) {
213 /* tape is now in unknown state */
214 printf("butm: Can't determine IOCTL child status. Error %d\n",
221 /* get the completion status from the child process */
222 rc = waitpid(pid, &status, 0);
223 while (rc < 0 && errno == EINTR) {
224 rc = waitpid(pid, &status, 0);
227 printf("butm: Can't determine IOCTL child status. Error %d\n",
229 } else if (status != 0) {
231 ("butm: Unexpected IOCTL process status 0x%04x . Error %d\n",
240 /* NT version of function */
243 ForkIoctl(usd_handle_t fd, int op, int count)
247 /* Issue requested tape control */
249 tapeop.tp_count = count;
251 return (USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop));
253 #endif /* !AFS_NT40_ENV */
257 * On Unix, fork a child process to attempt to open the drive. We want to make
258 * certain there is tape in the drive before trying to open the device
259 * in the main process
263 /* Unix version of function. */
266 ForkOpen(char *device)
268 int rc; /* return code from system calls */
269 int i; /* loop index */
270 int pid; /* process ID of child process */
271 int status; /* exit status of child process */
272 int open_rc; /* return code from open */
273 int pipefd[2]; /* pipe for child return status */
274 int forkflag; /* flag set when ready to fork */
275 usd_handle_t fd; /* handle returned from open */
277 #ifdef AFS_PTHREAD_ENV
278 forkflag = 0; /* No need to fork if using pthreads */
283 /* create pipe for getting return code from child */
287 printf("butm: Cannot create pipe for OPEN process. Error %d\n",
298 printf("butm: Cannot create child process for OPEN. Error %d\n",
304 if (!forkflag) { /* problem starting child process */
306 *return success, the caller will discover any problems
307 * when it opens the device.
310 } else if (pid == 0) { /* child process */
311 /* close all unneccessary file descriptors */
312 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
313 if (i != pipefd[1]) {
319 open_rc = usd_Open(device, USD_OPEN_RDONLY, 0, &fd);
325 /* send the return code back to the parent */
326 write(pipefd[1], &open_rc, sizeof(open_rc));
329 } else { /* parent process */
334 /* read the result from the child process */
335 rc = read(pipefd[0], &open_rc, sizeof(open_rc));
336 if (rc != sizeof(open_rc)) {
337 /* this is not a problem since we will reopen the device anyway */
338 printf("butm: No response from OPEN process. Error %d\n", errno);
344 /* get the completion status from the child process */
345 rc = waitpid(pid, &status, 0);
346 while (rc < 0 && errno == EINTR) {
347 rc = waitpid(pid, &status, 0);
350 printf("butm: Cannot get status of OPEN process. Error %d\n",
352 } else if (status != 0) {
354 ("butm: Unexpected OPEN process exit status 0x%04x. Error %d \n",
363 /* NT version of function. */
366 ForkOpen(char *device)
370 #endif /* AFS_NT40_ENV */
373 * On Unix, fork a child process to close the drive. If the drive rewinds
374 * on close it could cause the process to block.
378 /* Unix version of function */
381 ForkClose(usd_handle_t fd)
383 int rc; /* return code from system calls */
384 int i; /* loop index */
385 int pid; /* process ID of child process */
386 int status; /* exit status of child process */
387 int close_rc, parent_close_rc; /* return codes from close */
388 int pipefd[2]; /* pipe for child return status */
389 int ctlpipe[2]; /* pipe for message to child */
390 int forkflag; /* flag set when ready to fork */
393 #ifdef AFS_PTHREAD_ENV
394 forkflag = 0; /* No need to fork if using pthreads */
399 /* create pipe for getting return code from child */
403 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
409 /* create pipe for notifying child when to close */
415 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
428 printf("butm: Cannot create CLOSE child process. Error %d\n",
434 if (!forkflag) { /* problem starting child process */
435 close_rc = USD_CLOSE(fd);
436 parent_close_rc = close_rc;
437 } else if (pid == 0) { /* child process */
438 /* close all unneccessary file descriptors */
439 /* note: as painful as it is, we have to reach under the covers of
440 * the usd package to implement this functionality.
442 unixfd = (int)(fd->handle);
444 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
445 if (i != unixfd && i != ctlpipe[0] && i != pipefd[1]) {
450 /* the parent writes the control pipe after it closes the device */
451 read(ctlpipe[0], &close_rc, sizeof(int));
455 close_rc = USD_CLOSE(fd);
457 /* send the return code back to the parent */
458 write(pipefd[1], &close_rc, sizeof(int));
461 } else { /* parent process */
468 * close the device, this should have no effect as long as the
469 * child has not closed
472 parent_close_rc = USD_CLOSE(fd);
474 /* notify the child to do its close */
475 rc = write(ctlpipe[1], &close_rc, sizeof(int)); /* just send garbage */
476 if (rc != sizeof(int)) {
477 printf("butm: Error communicating with CLOSE process. Error %d\n",
482 /* read the result from the child process */
483 rc = read(pipefd[0], &close_rc, sizeof(int));
484 if (rc != sizeof(int)) {
485 /* logging is enough, since we wrote a file mark the */
486 /* return code from the close doesn't really matter */
487 printf("butm: No response from CLOSE process. Error %d\n",
494 /* get the completion status from the child process */
495 rc = waitpid(pid, &status, 0);
496 while (rc < 0 && errno == EINTR) {
497 rc = waitpid(pid, &status, 0);
500 printf("butm: Cannot get status of CLOSE process. Error %d\n",
502 } else if (status != 0) {
504 ("butm: Unexpected exit status 0x04x from CLOSE process. Error %d\n",
508 /* if either process received an error, then return an error */
509 if (parent_close_rc < 0) {
510 close_rc = parent_close_rc;
518 /* NT version of function */
521 ForkClose(usd_handle_t fd)
523 return (USD_CLOSE(fd));
525 #endif /* AFS_NT40_ENV */
527 /* Forward space file */
529 ForwardSpace(usd_handle_t fid, int count)
536 return (ForkIoctl(fid, USDTAPE_FSF, count));
540 /* Backward space file */
542 BackwardSpace(usd_handle_t fid, int count)
549 return (ForkIoctl(fid, USDTAPE_BSF, count));
553 /* write end of file mark */
555 WriteEOF(usd_handle_t fid, int count)
562 return (ForkIoctl(fid, USDTAPE_WEOF, count));
568 Rewind(usd_handle_t fid)
571 afs_hyper_t startOff, stopOff;
574 return (USD_SEEK(fid, startOff, SEEK_SET, &stopOff));
576 return (ForkIoctl(fid, USDTAPE_REW, 0));
580 /* prepare tape drive for access */
582 PrepareAccess(usd_handle_t fid)
587 (void)ForkIoctl(fid, USDTAPE_PREPARE, 0);
589 /* NT won't rewind tape when it is opened */
591 #endif /* AFS_NT40_ENV */
595 /* decommission tape drive after all accesses complete */
597 ShutdownAccess(usd_handle_t fid)
601 (void)ForkIoctl(fid, USDTAPE_SHUTDOWN, 0);
603 #endif /* AFS_NT40_ENV */
607 /* -------------------- USD Interface Functions End ----------------------- */
609 /* =====================================================================
611 * ===================================================================== */
614 * add the supplied no. of bytes to the byte count of information placed
617 * dataSize - bytes used on the tape
620 incSize(info, dataSize)
621 struct butm_tapeInfo *info;
624 info->nBytes += dataSize;
625 info->kBytes += (info->nBytes / 1024);
626 info->nBytes %= 1024;
630 * IF YOU ADD/CHANGE THE ifdefs, BE SURE
631 * TO ALSO MAKE THE SAME CHANGES IN bu_utils/fms.c.
633 * add the supplied no. of bytes to the byte count of data placed
634 * on the tape. Also check for reaching 2GB limit and reset the
635 * pointer if necessary. This allows us to use >2GB tapes.
637 * fid - file id for the tape.
638 * dataSize - bytes used on the tape
641 incPosition(info, fid, dataSize)
642 struct butm_tapeInfo *info;
648 /* Add this to the amount of data written to the tape */
649 incSize(info, dataSize);
651 info->posCount += dataSize;
653 if (info->posCount >= 2147467264) { /* 2GB - 16K */
655 #if (defined(AFS_SUN_ENV) || defined(AFS_DEC_ENV))
658 USD_IOCTL(fid, USD_IOCTL_SETSIZE, &off);
665 * This accounts for tape drives with a block size different from variable or 16K
666 * blocks and only reads that block size.
668 afs_int32 TapeBlockSize;
670 readData(fid, data, totalSize, errorP)
676 afs_int32 toread; /* Number of bytes to read */
677 afs_int32 rSize; /* Total bytes read so far */
678 afs_int32 tSize; /* Temporary size */
679 afs_int32 rc; /* return code */
681 toread = totalSize; /* First, try to read all the data */
683 rc = USD_READ(fid, &data[0], toread, &rSize);
688 if (rSize == 0) /* reached EOF */
691 if (rSize != TapeBlockSize) { /* Tape block size has changed */
692 TapeBlockSize = rSize;
693 printf("Tape blocks read in %d Byte chunks.\n", TapeBlockSize);
696 /* Read the rest of the data in */
697 while (rSize < totalSize) {
699 ((totalSize - rSize) <
700 TapeBlockSize ? (totalSize - rSize) : TapeBlockSize);
701 rc = USD_READ(fid, &data[rSize], toread, &tSize);
710 if (rSize > totalSize)
711 printf("readData - Read > 16K data block - continuing.\n");
717 SeekFile(info, count)
718 struct butm_tapeInfo *info;
723 int cpid, status, rcpid, stat;
729 p = (struct progress *)info->tmRock;
731 if (isafile) { /* no reason for seeking through a file */
732 p->reading = p->writing = 0;
739 error = ForwardSpace(p->fid, count);
741 error = BackwardSpace(p->fid, -count);
746 info->status |= BUTM_STATUS_SEEKERROR;
747 ERROR_EXIT(BUTM_IOCTL);
750 info->position += count;
751 incSize(info, (count * config.fileMarkSize));
752 p = (struct progress *)info->tmRock;
753 p->reading = p->writing = 0;
761 /* Step to the next filemark if we are not at one already */
764 struct butm_tapeInfo *info;
768 if (!READS && !WRITES)
771 code = SeekFile(info, 1);
776 WriteTapeBlock(info, buffer, length, blockType)
777 struct butm_tapeInfo *info;
778 char *buffer; /* assumed to be 16384 bytes with data in it */
779 afs_int32 length; /* amount data in buffer */
782 afs_int32 code = 0, rc = 0;
784 struct tapeLabel *label;
785 struct fileMark *fmark;
786 struct blockMark *bmark;
790 p = (struct progress *)info->tmRock;
792 if (blockType == BLOCK_DATA) { /* Data Block */
795 bmark = (struct blockMark *)buffer;
796 memset(bmark, 0, sizeof(struct blockMark));
797 bmark->magic = htonl(BLOCK_MAGIC);
798 bmark->count = htonl(length);
799 } else if (blockType == BLOCK_FMBEGIN) { /* Filemark - begin */
800 fmark = (struct fileMark *)buffer;
801 fmark->magic = htonl(FILE_MAGIC);
802 fmark->nBytes = htonl(FILE_BEGIN);
803 } else if (blockType == BLOCK_FMEND) { /* Filemark - end */
804 fmark = (struct fileMark *)buffer;
805 fmark->magic = htonl(FILE_MAGIC);
806 fmark->nBytes = htonl(FILE_FMEND);
807 } else if (blockType == BLOCK_LABEL) { /* Label */
808 label = (struct tapeLabel *)buffer;
809 label->magic = htonl(TAPE_MAGIC);
810 } else if (blockType == BLOCK_EOD) { /* Filemark - EOD mark */
811 fmark = (struct fileMark *)buffer;
812 fmark->magic = htonl(FILE_MAGIC);
813 fmark->nBytes = htonl(FILE_EOD);
816 /* Write the tape block */
817 /* -------------------- */
818 rc = USD_WRITE(p->fid, buffer, BUTM_BLOCKSIZE, &wsize);
819 if ((rc == 0) && (wsize > 0)) {
820 incPosition(info, p->fid, wsize); /* record whats written */
824 if (wsize != BUTM_BLOCKSIZE) {
825 info->status |= BUTM_STATUS_WRITEERROR;
830 ERROR_EXIT(BUTM_EOT);
835 /* Write trailing EOF marker for some block types */
836 /* ---------------------------------------------- */
837 if ((blockType == BLOCK_FMEND) || (blockType == BLOCK_LABEL)
838 || (blockType == BLOCK_EOD)) {
841 error = WriteEOF(p->fid, 1);
844 info->status |= BUTM_STATUS_WRITEERROR;
845 ERROR_EXIT(BUTM_IOCTL);
848 incSize(info, config.fileMarkSize);
861 ReadTapeBlock(info, buffer, blockType)
862 struct butm_tapeInfo *info;
863 char *buffer; /* assumed to be 16384 bytes */
864 afs_int32 *blockType;
867 afs_int32 rsize, fmtype;
868 struct tapeLabel *label;
869 struct fileMark *fmark;
870 struct blockMark *bmark;
873 *blockType = BLOCK_UNKNOWN;
875 p = (struct progress *)info->tmRock;
877 memset(buffer, 0, BUTM_BLOCKSIZE);
878 label = (struct tapeLabel *)buffer;
879 fmark = (struct fileMark *)buffer;
880 bmark = (struct blockMark *)buffer;
882 rsize = readData(p->fid, buffer, BUTM_BLOCKSIZE, &info->error);
884 incPosition(info, p->fid, rsize);
888 if (rsize == 0) { /* Read a HW EOF Marker? OK */
889 *blockType = BLOCK_EOF;
890 incSize(info, config.fileMarkSize); /* Size of filemark */
892 info->position++; /* bump position */
893 p->reading = 0; /* No reads since EOF */
896 else if (rsize != BUTM_BLOCKSIZE) { /* Didn't Read a full block */
897 info->status |= BUTM_STATUS_READERROR;
898 ERROR_EXIT((rsize < 0) ? BUTM_IO : BUTM_EOT);
901 else if (ntohl(bmark->magic) == BLOCK_MAGIC) { /* Data block? */
902 *blockType = BLOCK_DATA;
905 else if (ntohl(fmark->magic) == FILE_MAGIC) { /* Read a filemark? */
906 fmtype = ntohl(fmark->nBytes);
908 if (fmtype == FILE_BEGIN) { /* filemark begin */
909 *blockType = BLOCK_FMBEGIN;
910 } else if (fmtype == FILE_FMEND) { /* filemark end */
911 *blockType = BLOCK_FMEND;
912 code = SeekFile(info, 1);
913 } else if (fmtype == FILE_EOD) { /* EOD mark */
914 *blockType = BLOCK_EOD;
915 info->status |= BUTM_STATUS_EOD;
916 code = SeekFile(info, 1);
920 else if (ntohl(label->magic) == TAPE_MAGIC) { /* Read a tape label? */
921 *blockType = BLOCK_LABEL;
922 code = SeekFile(info, 1);
933 * check version numbers and permissions in the info structure
938 struct butm_tapeInfo *info;
939 int write; /* write operation requested */
944 return (BUTM_BADARGUMENT);
946 /* Check version number in info structure */
947 if (info->structVersion != BUTM_MAJORVERSION)
948 return BUTM_OLDINTERFACE;
950 /* Check if a tape is mounted */
951 if (((p = (struct progress *)info->tmRock) == 0) || (p->fid == 0))
954 /* If writing check if there is write access */
955 if (write && (info->flags & BUTM_FLAGS_READONLY))
956 return BUTM_READONLY;
963 struct butm_tapeInfo *info;
966 int cpid, status, rcpid, stat;
971 p = (struct progress *)info->tmRock;
975 error = Rewind(p->fid);
980 info->status |= BUTM_STATUS_SEEKERROR;
981 ERROR_EXIT(BUTM_IOCTL);
984 info->position = (isafile ? 0 : 1);
985 info->kBytes = info->nBytes = 0;
986 info->nFiles = info->nRecords = 0;
987 p->reading = p->writing = 0;
995 /* =====================================================================
997 * ===================================================================== */
1000 file_Mount(info, tape)
1001 struct butm_tapeInfo *info;
1007 int cpid, status, rcpid, xflags;
1008 afs_int32 code = 0, error = 0, rc = 0;
1011 printf("butm: Mount tape drive\n");
1017 ERROR_EXIT(BUTM_BADARGUMENT);
1018 if (info->structVersion != BUTM_MAJORVERSION)
1019 ERROR_EXIT(BUTM_OLDINTERFACE);
1021 ERROR_EXIT(BUTM_PARALLELMOUNTS);
1022 if (strlen(tape) >= sizeof(info->name))
1023 ERROR_EXIT(BUTM_BADARGUMENT);
1025 strcpy(info->name, tape);
1027 strcpy(filename, config.tapedir); /* the name of the tape device */
1028 info->position = (isafile ? 0 : 1);
1029 info->kBytes = info->nBytes = 0;
1030 info->nRecords = info->nFiles = 0;
1031 info->recordSize = 0;
1032 info->tapeSize = config.tapeSize;
1033 info->coefBytes = 1;
1034 info->coefRecords = 0;
1035 info->coefFiles = sizeof(struct fileMark);
1036 info->simultaneousTapes = 1;
1039 info->flags = BUTM_FLAGS_SEQUENTIAL;
1043 xflags |= USD_OPEN_CREATE;
1046 * try to open in a child process first so nothing will
1047 * time out should the process block because the device
1051 if (ForkOpen(filename)) {
1052 ERROR_EXIT(BUTM_MOUNTFAIL);
1056 /* Now go ahead and open the tape drive for real */
1057 rc = usd_Open(filename, (USD_OPEN_RDWR | USD_OPEN_WLOCK | xflags), 0777,
1059 if (rc != 0) { /* try for lesser access */
1060 rc = usd_Open(filename, (USD_OPEN_RDONLY | USD_OPEN_RLOCK), 0, &fid);
1064 ERROR_EXIT(BUTM_MOUNTFAIL);
1066 info->flags |= BUTM_FLAGS_READONLY;
1069 (void)PrepareAccess(fid); /* for NT */
1071 p = (struct progress *)malloc(sizeof(*p));
1072 info->tmRock = (char *)p;
1074 p->mountId = config.mountId = time(0);
1075 p->reading = p->writing = 0;
1077 TapeBlockSize = BUTM_BLOCKSIZE; /* Initialize */
1081 info->error = error;
1087 struct butm_tapeInfo *info;
1090 int cpid, status, rcpid, stat;
1091 afs_int32 code = 0, error = 0, cd;
1097 printf("butm: Unmount tape drive\n");
1102 code = check(info, READ_OP);
1106 p = (struct progress *)info->tmRock;
1108 (void)ShutdownAccess(p->fid); /* for NT */
1110 /* close the device */
1111 if (error = ForkClose(p->fid)) {
1112 printf("butm: Tape close failed. Error %d\n", errno);
1118 code = BUTM_DISMOUNTFAIL;
1119 info->status |= BUTM_STATUS_TAPEERROR;
1123 info->tmRock = 0; /* mark it as closed - even if error on close */
1129 info->error = error;
1134 * write the header on a tape
1136 * info - handle on tape unit
1137 * label - label information. This label is not copied onto the tape.
1138 * If supplied, various fields are copied from this label to
1139 * the actual tape label written on the tape.
1143 file_WriteLabel(info, label, rewind)
1144 struct butm_tapeInfo *info;
1145 struct butm_tapeLabel *label;
1150 struct tapeLabel *tlabel;
1152 afs_hyper_t off; /* offset */
1155 printf("butm: Write tape label\n");
1160 code = check(info, WRITE_OP);
1164 ERROR_EXIT(BUTM_BADARGUMENT);
1165 if (label->structVersion != CUR_TAPE_VERSION)
1166 ERROR_EXIT(BUTM_OLDINTERFACE);
1168 if (rewind) { /* Not appending, so rewind */
1169 code = rewindFile(info);
1174 p = (struct progress *)info->tmRock;
1176 code = USD_IOCTL(p->fid, USD_IOCTL_SETSIZE, &off);
1178 ERROR_EXIT(BUTM_POSITION);
1181 if (READS || WRITES)
1182 ERROR_EXIT(BUTM_BADOP);
1185 /* Copy the label into the tape block
1186 * ---------------------------------- */
1187 memset(tapeBlock, 0, BUTM_BLOCKSIZE);
1189 if (!label->creationTime)
1190 label->creationTime = time(0);
1192 tlabel = (struct tapeLabel *)tapeBlock;
1193 memcpy(&tlabel->label, label, sizeof(struct butm_tapeLabel));
1194 tlabel->label.structVersion = htonl(CUR_TAPE_VERSION);
1195 tlabel->label.creationTime = htonl(tlabel->label.creationTime);
1196 tlabel->label.expirationDate = htonl(tlabel->label.expirationDate);
1197 tlabel->label.size = htonl(tlabel->label.size);
1198 tlabel->label.useCount = htonl(tlabel->label.useCount);
1199 tlabel->label.dumpid = htonl(tlabel->label.dumpid);
1202 * write the tape label - For appends, the write may need to skip
1203 * over 1 or 2 EOF marks that were written when tape was closed after
1204 * the last dump. Plus, some AIX tape drives require we try forwarding
1205 * over the last EOF and take an error before we can write the new label.
1206 * ---------------------------------------------------------------------- */
1207 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1208 if (!isafile && !rewind && (code == BUTM_IO))
1209 do { /* do if write failed */
1210 fcode = SeekFile(info, 1); /* skip over the EOF */
1212 break; /* leave if error */
1215 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1216 if (code != BUTM_IO)
1217 break; /* continue if write failed */
1219 fcode = SeekFile(info, 1); /* skip over the EOF */
1220 if (fcode) { /* retry 1 write if couldn't skip */
1222 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1228 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1229 if (code != BUTM_IO)
1230 break; /* continue if write failed */
1232 fcode = SeekFile(info, 1); /* skip over the EOF */
1233 if (fcode) { /* retry 1 write if couldn't skip */
1235 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1242 /* clear the write error status a failed WriteTapeBlock may have produced */
1244 info->status &= ~BUTM_STATUS_WRITEERROR;
1251 file_ReadLabel(info, label, rewind)
1252 struct butm_tapeInfo *info;
1253 struct butm_tapeLabel *label;
1256 struct tapeLabel *tlabel;
1258 afs_int32 blockType;
1261 printf("butm: Read tape label\n");
1266 code = check(info, READ_OP);
1269 if (READS || WRITES)
1270 ERROR_EXIT(BUTM_BADOP);
1273 code = rewindFile(info);
1275 ERROR_EXIT(code); /* status is set so return */
1279 * When appended labels were written, either 1 or 2 EOF marks may
1280 * have had to be skipped. When reading a label, these EOF marks
1281 * must also be skipped. When an EOF is read, 0 bytes are returned
1282 * (refer to the write calls in file_WriteLabel routine).
1283 * ---------------------------------------------------------------- */
1284 code = ReadTapeBlock(info, tapeBlock, &blockType);
1285 if (!isafile && !rewind && (blockType == BLOCK_EOF))
1287 code = ReadTapeBlock(info, tapeBlock, &blockType);
1288 if (blockType != BLOCK_EOF)
1289 break; /* didn't read an EOF */
1291 code = ReadTapeBlock(info, tapeBlock, &blockType);
1294 if (blockType == BLOCK_UNKNOWN)
1295 ERROR_EXIT(BUTM_NOLABEL);
1298 if (blockType != BLOCK_LABEL)
1299 ERROR_EXIT(BUTM_BADBLOCK);
1304 tlabel = (struct tapeLabel *)tapeBlock;
1305 memcpy(label, &tlabel->label, sizeof(struct butm_tapeLabel));
1306 label->structVersion = ntohl(label->structVersion);
1307 label->creationTime = ntohl(label->creationTime);
1308 label->expirationDate = ntohl(label->expirationDate);
1309 label->size = ntohl(label->size);
1310 label->dumpid = ntohl(label->dumpid);
1311 label->useCount = ntohl(label->useCount);
1313 info->tapeSize = label->size; /* use size from label */
1321 file_WriteFileBegin(info)
1322 struct butm_tapeInfo *info;
1325 afs_int32 error = 0;
1328 printf("butm: Write filemark begin\n");
1332 code = check(info, WRITE_OP);
1336 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMBEGIN);
1347 file_ReadFileBegin(info)
1348 struct butm_tapeInfo *info;
1350 struct fileMark mark;
1352 afs_int32 blockType;
1355 printf("butm: Read filemark begin\n");
1360 code = check(info, READ_OP);
1363 if (READS || WRITES)
1364 ERROR_EXIT(BUTM_BADOP);
1366 code = ReadTapeBlock(info, tapeBlock, &blockType);
1370 if (blockType != BLOCK_FMBEGIN) {
1371 if (blockType == BLOCK_EOD)
1372 ERROR_EXIT(BUTM_EOD); /* EODump label */
1373 if (blockType == BLOCK_LABEL)
1374 ERROR_EXIT(BUTM_LABEL); /* Tape label */
1375 ERROR_EXIT(BUTM_BADBLOCK); /* Other */
1382 /* Writes data out in block sizes of 16KB. Does destroy the data.
1383 * Assumes the data buffer has a space reserved at beginning for a blockMark.
1386 file_WriteFileData(info, data, blocks, len)
1387 struct butm_tapeInfo *info;
1395 char *bstart; /* Where block starts for a 16K block */
1396 char *dstart; /* Where data starts for a 16K block */
1399 printf("butm: Write tape data - %u bytes\n", len);
1404 code = check(info, WRITE_OP);
1407 if (!data || (len < 0))
1408 ERROR_EXIT(BUTM_BADARGUMENT);
1409 if (READS || !WRITES)
1410 ERROR_EXIT(BUTM_BADOP);
1412 b = 0; /* start at block 0 */
1414 dstart = &data[b * BUTM_BLKSIZE];
1415 bstart = dstart - sizeof(struct blockMark);
1417 if (len < BUTM_BLKSIZE) {
1418 memset(&dstart[len], 0, BUTM_BLKSIZE - len);
1421 length = BUTM_BLKSIZE;
1424 code = WriteTapeBlock(info, bstart, length, BLOCK_DATA);
1428 /* If there are more blocks, step to next block */
1429 /* Otherwise, copy the data to beginning of last block */
1431 if (b < (blocks - 1))
1434 memcpy(&dstart[0], &dstart[BUTM_BLKSIZE], len);
1441 /* file_ReadFileData
1442 * Read a data block from tape.
1444 * info - tape info structure, c.f. fid
1445 * data - ptr to buffer for data
1446 * len - size of data buffer
1448 * nBytes - no. of data bytes read.
1452 file_ReadFileData(info, data, len, nBytes)
1453 struct butm_tapeInfo *info;
1458 struct blockMark *bmark;
1460 afs_int32 blockType;
1463 printf("butm: Read tape data - %u bytes\n", len);
1469 ERROR_EXIT(BUTM_BADARGUMENT);
1472 code = check(info, READ_OP);
1475 if (!data || (len < 0) || (len > BUTM_BLKSIZE))
1476 ERROR_EXIT(BUTM_BADARGUMENT);
1477 if (!READS || WRITES)
1478 ERROR_EXIT(BUTM_BADOP);
1480 data -= sizeof(struct blockMark);
1481 code = ReadTapeBlock(info, data, &blockType);
1485 if (blockType != BLOCK_DATA) {
1486 if (blockType == BLOCK_EOF)
1487 ERROR_EXIT(BUTM_EOF);
1488 if (blockType == BLOCK_FMEND)
1489 ERROR_EXIT(BUTM_ENDVOLUME);
1490 ERROR_EXIT(BUTM_BADBLOCK);
1493 bmark = (struct blockMark *)data;
1494 *nBytes = ntohl(bmark->count); /* Size of data in buf */
1501 file_WriteFileEnd(info)
1502 struct butm_tapeInfo *info;
1507 printf("butm: Write filemark end\n");
1512 code = check(info, WRITE_OP);
1515 if (READS || !WRITES)
1516 ERROR_EXIT(BUTM_BADOP);
1518 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMEND);
1524 /* Read a SW filemark, verify that it is a SW filemark, then skip to the next
1525 * HW filemark. If the read of the SW filemark shows it's an EOF, then
1526 * ignore that the SW filemark is not there and return 0 (found the SW filemark
1527 * missing with some 3.1 dumps).
1530 file_ReadFileEnd(info)
1531 struct butm_tapeInfo *info;
1533 struct fileMark mark;
1535 afs_int32 blockType;
1538 printf("butm: Read filemark end\n");
1543 code = check(info, READ_OP);
1546 if (!READS || WRITES)
1547 ERROR_EXIT(BUTM_BADOP);
1549 info->status &= ~BUTM_STATUS_EOF;
1551 code = ReadTapeBlock(info, tapeBlock, &blockType);
1555 if ((blockType != BLOCK_FMEND) && (blockType != BLOCK_EOF))
1556 ERROR_EXIT(BUTM_BADBLOCK);
1563 * Write the end-of-dump marker.
1566 file_WriteEODump(info)
1567 struct butm_tapeInfo *info;
1572 printf("butm: Write filemark EOD\n");
1577 code = check(info, WRITE_OP);
1580 if (READS || WRITES)
1581 ERROR_EXIT(BUTM_BADOP);
1583 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_EOD);
1587 info->status |= BUTM_STATUS_EOD;
1594 file_Seek(info, position)
1595 struct butm_tapeInfo *info;
1601 afs_hyper_t startOff, stopOff; /* for normal file(non-tape) seeks */
1604 printf("butm: Seek to the tape position %d\n", position);
1608 code = check(info, READ_OP);
1613 p = (struct progress *)info->tmRock;
1614 posit = (position * BUTM_BLOCKSIZE);
1616 hset64(startOff, 0, posit);
1617 w = USD_SEEK(p->fid, startOff, SEEK_SET, &stopOff);
1620 if (hcmp(startOff, stopOff) != 0)
1621 ERROR_EXIT(BUTM_POSITION);
1623 p->reading = p->writing = 0;
1624 info->position = position;
1626 /* Don't position backwards if we are in-between FMs */
1627 if ((READS || WRITES) && ((position - info->position) <= 0))
1628 ERROR_EXIT(BUTM_BADOP);
1630 code = SeekFile(info, (position - info->position));
1640 * Seek to the EODump (end-of-dump) after the given position. This is
1641 * the position after the EOF filemark immediately after the EODump mark.
1642 * This is for tapes of version 4 or greater.
1645 file_SeekEODump(info, position)
1646 struct butm_tapeInfo *info;
1649 struct fileMark mark;
1651 afs_int32 blockType;
1654 afs_hyper_t startOff, stopOff; /* file seek offsets */
1657 printf("butm: Seek to end-of-dump\n");
1660 code = check(info, READ_OP);
1663 if (READS || WRITES)
1664 ERROR_EXIT(BUTM_BADOP);
1667 p = (struct progress *)info->tmRock;
1668 hset64(startOff, 0, 0);
1669 w = USD_SEEK(p->fid, startOff, SEEK_END, &stopOff);
1673 ERROR_EXIT(BUTM_POSITION);
1675 if (hgetlo(stopOff) % BUTM_BLOCKSIZE)
1676 ERROR_EXIT(BUTM_POSITION);
1677 info->position = (hgetlo(stopOff) / BUTM_BLOCKSIZE);
1679 /* Seek to the desired position */
1680 code = SeekFile(info, (position - info->position) + 1);
1685 * Search until the filemark is an EODump filemark.
1686 * Skip over volumes only.
1689 code = ReadTapeBlock(info, tapeBlock, &blockType);
1693 if (blockType == BLOCK_EOD)
1695 if (blockType != BLOCK_FMBEGIN)
1696 ERROR_EXIT(BUTM_BADBLOCK);
1698 code = SeekFile(info, 1); /* Step forward to next volume */
1710 file_SetSize(info, size)
1711 struct butm_tapeInfo *info;
1715 printf("butm: Set size of tape\n");
1719 info->tapeSize = config.tapeSize;
1721 info->tapeSize = size;
1726 file_GetSize(info, size)
1727 struct butm_tapeInfo *info;
1731 printf("butm: Get size of tape\n");
1734 *size = info->tapeSize;
1738 /* =====================================================================
1739 * Startup/configuration routines.
1740 * ===================================================================== */
1743 file_Configure(file)
1744 struct tapeConfig *file;
1747 com_err(whoami, BUTM_BADCONFIG, "device not specified");
1748 return BUTM_BADCONFIG;
1751 config.tapeSize = file->capacity;
1752 config.fileMarkSize = file->fileMarkSize;
1753 config.portOffset = file->portOffset;
1754 strcpy(config.tapedir, file->device);
1756 /* Tape must be large enough to at least fit a label */
1757 if (config.tapeSize <= 0) {
1758 com_err(whoami, BUTM_BADCONFIG, "Tape size bogus: %d Kbytes",
1760 return BUTM_BADCONFIG;
1763 if (strlen(config.tapedir) == 0) {
1764 com_err(whoami, BUTM_BADCONFIG, "no tape device specified");
1765 return BUTM_BADCONFIG;
1772 /* This procedure instantiates a tape module of type file_tm. */
1774 butm_file_Instantiate(info, file)
1775 struct butm_tapeInfo *info;
1776 struct tapeConfig *file;
1778 extern int debugLevel;
1781 if (debugLevel > 98)
1782 printf("butm: Instantiate butc\n");
1785 ERROR_EXIT(BUTM_BADARGUMENT);
1786 if (info->structVersion != BUTM_MAJORVERSION)
1787 ERROR_EXIT(BUTM_OLDINTERFACE);
1789 memset(info, 0, sizeof(struct butm_tapeInfo));
1790 info->structVersion = BUTM_MAJORVERSION;
1791 info->ops.mount = file_Mount;
1792 info->ops.dismount = file_Dismount;
1793 info->ops.create = file_WriteLabel;
1794 info->ops.readLabel = file_ReadLabel;
1795 info->ops.seek = file_Seek;
1796 info->ops.seekEODump = file_SeekEODump;
1797 info->ops.readFileBegin = file_ReadFileBegin;
1798 info->ops.readFileData = file_ReadFileData;
1799 info->ops.readFileEnd = file_ReadFileEnd;
1800 info->ops.writeFileBegin = file_WriteFileBegin;
1801 info->ops.writeFileData = file_WriteFileData;
1802 info->ops.writeFileEnd = file_WriteFileEnd;
1803 info->ops.writeEOT = file_WriteEODump;
1804 info->ops.setSize = file_SetSize;
1805 info->ops.getSize = file_GetSize;
1806 info->debug = ((debugLevel > 98) ? 1 : 0);
1808 code = file_Configure(file);