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>
30 #include <afs/com_err.h>
33 #include "error_macros.h"
36 typedef off64_t osi_lloff_t;
37 #else /* O_LARGEFILE */
38 #ifdef AFS_HAVE_LLSEEK
39 typedef offset_t osi_lloff_t;
40 #else /* AFS_HAVE_LLSEEK */
41 typedef off_t osi_lloff_t;
42 #endif /* AFS_HAVE_LLSEEK */
43 #endif /* O_LARGEFILE */
47 #define FILE_MAGIC 1000000007 /* s/w file mark */
48 #define FILE_BEGIN 0 /* byte field in file mark */
49 #define FILE_FMEND 1 /* byte field in file mark */
50 #define FILE_EOD -1 /* byte field in file mark */
51 #define TAPE_MAGIC 1100000009 /* tape label block */
52 #define BLOCK_MAGIC 1100000005 /* file data block */
53 #ifdef AFS_PTHREAD_ENV
55 #define SLEEP(s) sleep(s)
57 #define POLL() IOMGR_Poll()
58 #define SLEEP(s) IOMGR_Sleep(s)
63 * 1) filemarks and block marks have the magic,bytes fields reversed. This
64 * is undoubtedly a bug. Also note that the two structures have
65 * inconsistent types, overlaying int and afs_int32.
66 * 2) When a volume is dumped, if the volume is locked, the dump will produce
67 * an anomalous tape format of the form:
71 * The design of the original butm code means that this cannot be
72 * handled correctly. The code is modified so that ReadFileData
73 * returns BUTM_ENDVOLUME if it encounters the s/w filemark.
76 /* data organization on tape:
77 * all writes are in blocks of BUTM_BLOCKSIZE (= sizeof(blockMark) + BUTM_BLKSIZE)
78 * blockMark contains a magic number and counts of real data bytes
79 * written out in the block.
81 * each file is preceeded by a fileMark, which acts as the file
82 * delimiter. A file consists of contigous data blocks. TM does
83 * understand or interrpet the data in data blocks.
85 * The tape begins with a tape label and ends with EOF file markers
86 * in succession (2 or 4 of them ).
90 struct fileMark { /* in network byte order */
97 struct butm_tapeLabel label;
101 usd_handle_t fid; /* file id of simulated tape */
102 afs_int32 mountId; /* current mountId */
103 afs_int32 reading; /* read file operation in progress */
104 afs_int32 writing; /* write file operation in progress */
107 static struct configuration {
108 char tapedir[64]; /* directory to create "tapes" */
109 afs_int32 mountId; /* for detecting simultaneous mounts */
110 afs_uint32 tapeSize; /* size of simulated tapes */
111 afs_uint32 fileMarkSize; /* size of file mark, bytes */
112 afs_int32 portOffset; /* port + portOffset is used by TC to listen */
115 static char *whoami = "file_tm";
116 char tapeBlock[BUTM_BLOCKSIZE]; /* Tape buffer for reads and writes */
118 #define BLOCK_LABEL 0 /* read/write a tape label */
119 #define BLOCK_FMBEGIN 1 /* read/write a begin FileMark */
120 #define BLOCK_DATA 2 /* read/write a data block */
121 #define BLOCK_FMEND 3 /* read/write an end FileMark */
122 #define BLOCK_EOD 4 /* read/write an EOD FileMark */
123 #define BLOCK_EOF 5 /* tape block is a HW EOF mark (usually error) */
124 #define BLOCK_UNKNOWN 6 /* tape block is unknwon (error) */
129 #define READS (((struct progress *)(info->tmRock))->reading)
130 #define WRITES (((struct progress *)(info->tmRock))->writing)
132 /* ----------------------------------------------------------------------
133 * These routines use the usd library to perform tape operations.
134 * ForkIoctl, ForkOpen, ForkClose, ForwardSpace, BackwardSpace, WriteEOF,
135 * PrepareAccess(nt), ShutdownAccess(nt)
137 * Return Values: USD functions return 0 if successful or errno if failed.
139 /* ------------------ USD Interface Functions Begin ------------------------ */
142 * On Unix, fork a child process to perform an IOCTL call. This avoids
143 * blocking the entire process during a tape operation
147 /* Unix version of function */
150 ForkIoctl(usd_handle_t fd, int op, int count)
152 int rc; /* return code from system calls */
153 int i; /* loop index */
154 int pid; /* process ID of child process */
155 int status; /* exit status of child process */
156 int ioctl_rc; /* return code from ioctl call */
157 int pipefd[2]; /* pipe for child return status */
158 int forkflag; /* flag set when ready to fork */
159 usd_tapeop_t tapeop; /* tape operation specification */
162 #ifdef AFS_PTHREAD_ENV
163 forkflag = 0; /* No need to fork if using pthreads */
168 /* initialize tape command structure */
170 tapeop.tp_count = count;
172 /* create pipe for getting return code from child */
176 printf("butm: Can't open pipe for IOCTL process. Error %d\n",
187 printf("butm: Can't fork IOCTL process. Error %d\n", errno);
193 if (!forkflag) { /* problem starting child process */
194 /* do the ioctl anyway, it will probably work */
195 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
196 } else if (pid == 0) { /* child process */
197 /* close all unneccessary file descriptors */
198 /* note: as painful as it is, we have to reach under the covers of
199 * the usd package to implement this functionality.
201 unixfd = (int)(fd->handle);
203 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
204 if (i != unixfd && i != pipefd[1]) {
209 /* do the ioctl call */
210 ioctl_rc = USD_IOCTL(fd, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
212 /* send the return code back to the parent */
213 write(pipefd[1], &ioctl_rc, sizeof(int));
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);
335 /* send the return code back to the parent */
336 write(pipefd[1], &open_rc, sizeof(open_rc));
339 } else { /* parent process */
344 /* read the result from the child process */
345 rc = read(pipefd[0], &open_rc, sizeof(open_rc));
346 if (rc != sizeof(open_rc)) {
347 /* this is not a problem since we will reopen the device anyway */
348 printf("butm: No response from OPEN process. Error %d\n", errno);
354 /* get the completion status from the child process */
355 rc = waitpid(pid, &status, 0);
356 while (rc < 0 && errno == EINTR) {
357 rc = waitpid(pid, &status, 0);
360 printf("butm: Cannot get status of OPEN process. Error %d\n",
362 } else if (status != 0) {
364 ("butm: Unexpected OPEN process exit status 0x%04x. Error %d \n",
373 /* NT version of function. */
376 ForkOpen(char *device)
380 #endif /* AFS_NT40_ENV */
383 * On Unix, fork a child process to close the drive. If the drive rewinds
384 * on close it could cause the process to block.
388 /* Unix version of function */
391 ForkClose(usd_handle_t fd)
393 int rc; /* return code from system calls */
394 int i; /* loop index */
395 int pid; /* process ID of child process */
396 int status; /* exit status of child process */
397 int close_rc, parent_close_rc; /* return codes from close */
398 int pipefd[2]; /* pipe for child return status */
399 int ctlpipe[2]; /* pipe for message to child */
400 int forkflag; /* flag set when ready to fork */
403 #ifdef AFS_PTHREAD_ENV
404 forkflag = 0; /* No need to fork if using pthreads */
409 /* create pipe for getting return code from child */
413 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
419 /* create pipe for notifying child when to close */
425 printf("butm: Cannot create pipe for CLOSE process. Error %d\n",
438 printf("butm: Cannot create CLOSE child process. Error %d\n",
444 if (!forkflag) { /* problem starting child process */
445 close_rc = USD_CLOSE(fd);
446 parent_close_rc = close_rc;
447 } else if (pid == 0) { /* child process */
448 /* close all unneccessary file descriptors */
449 /* note: as painful as it is, we have to reach under the covers of
450 * the usd package to implement this functionality.
452 unixfd = (int)(fd->handle);
454 for (i = 3; i < _POSIX_OPEN_MAX; i++) {
455 if (i != unixfd && i != ctlpipe[0] && i != pipefd[1]) {
460 /* the parent writes the control pipe after it closes the device */
461 read(ctlpipe[0], &close_rc, sizeof(int));
465 close_rc = USD_CLOSE(fd);
467 /* send the return code back to the parent */
468 write(pipefd[1], &close_rc, sizeof(int));
471 } else { /* parent process */
478 * close the device, this should have no effect as long as the
479 * child has not closed
482 parent_close_rc = USD_CLOSE(fd);
484 /* notify the child to do its close */
485 rc = write(ctlpipe[1], &close_rc, sizeof(int)); /* just send garbage */
486 if (rc != sizeof(int)) {
487 printf("butm: Error communicating with CLOSE process. Error %d\n",
492 /* read the result from the child process */
493 rc = read(pipefd[0], &close_rc, sizeof(int));
494 if (rc != sizeof(int)) {
495 /* logging is enough, since we wrote a file mark the */
496 /* return code from the close doesn't really matter */
497 printf("butm: No response from CLOSE process. Error %d\n",
504 /* get the completion status from the child process */
505 rc = waitpid(pid, &status, 0);
506 while (rc < 0 && errno == EINTR) {
507 rc = waitpid(pid, &status, 0);
510 printf("butm: Cannot get status of CLOSE process. Error %d\n",
512 } else if (status != 0) {
514 ("butm: Unexpected exit status 0x04x from CLOSE process. Error %d\n",
518 /* if either process received an error, then return an error */
519 if (parent_close_rc < 0) {
520 close_rc = parent_close_rc;
528 /* NT version of function */
531 ForkClose(usd_handle_t fd)
533 return (USD_CLOSE(fd));
535 #endif /* AFS_NT40_ENV */
537 /* Forward space file */
539 ForwardSpace(usd_handle_t fid, int count)
546 return (ForkIoctl(fid, USDTAPE_FSF, count));
550 /* Backward space file */
552 BackwardSpace(usd_handle_t fid, int count)
559 return (ForkIoctl(fid, USDTAPE_BSF, count));
563 /* write end of file mark */
565 WriteEOF(usd_handle_t fid, int count)
572 return (ForkIoctl(fid, USDTAPE_WEOF, count));
578 Rewind(usd_handle_t fid)
581 afs_hyper_t startOff, stopOff;
584 return (USD_SEEK(fid, startOff, SEEK_SET, &stopOff));
586 return (ForkIoctl(fid, USDTAPE_REW, 0));
590 /* prepare tape drive for access */
592 PrepareAccess(usd_handle_t fid)
597 (void)ForkIoctl(fid, USDTAPE_PREPARE, 0);
599 /* NT won't rewind tape when it is opened */
601 #endif /* AFS_NT40_ENV */
605 /* decommission tape drive after all accesses complete */
607 ShutdownAccess(usd_handle_t fid)
611 (void)ForkIoctl(fid, USDTAPE_SHUTDOWN, 0);
613 #endif /* AFS_NT40_ENV */
617 /* -------------------- USD Interface Functions End ----------------------- */
619 /* =====================================================================
621 * ===================================================================== */
624 * add the supplied no. of bytes to the byte count of information placed
627 * dataSize - bytes used on the tape
630 incSize(info, dataSize)
631 struct butm_tapeInfo *info;
634 info->nBytes += dataSize;
635 info->kBytes += (info->nBytes / 1024);
636 info->nBytes %= 1024;
640 * IF YOU ADD/CHANGE THE ifdefs, BE SURE
641 * TO ALSO MAKE THE SAME CHANGES IN bu_utils/fms.c.
643 * add the supplied no. of bytes to the byte count of data placed
644 * on the tape. Also check for reaching 2GB limit and reset the
645 * pointer if necessary. This allows us to use >2GB tapes.
647 * fid - file id for the tape.
648 * dataSize - bytes used on the tape
651 incPosition(info, fid, dataSize)
652 struct butm_tapeInfo *info;
656 /* Add this to the amount of data written to the tape */
657 incSize(info, dataSize);
659 info->posCount += dataSize;
661 if (info->posCount >= 2147467264) { /* 2GB - 16K */
663 #if (defined(AFS_SUN_ENV) || defined(AFS_LINUX24_ENV))
667 USD_IOCTL(fid, USD_IOCTL_SETSIZE, &off);
674 * This accounts for tape drives with a block size different from variable or 16K
675 * blocks and only reads that block size.
677 afs_int32 TapeBlockSize;
679 readData(fid, data, totalSize, errorP)
685 afs_int32 toread; /* Number of bytes to read */
686 afs_int32 rSize; /* Total bytes read so far */
687 afs_int32 tSize; /* Temporary size */
688 afs_int32 rc; /* return code */
690 toread = totalSize; /* First, try to read all the data */
692 rc = USD_READ(fid, &data[0], toread, &rSize);
697 if (rSize == 0) /* reached EOF */
700 if (rSize != TapeBlockSize) { /* Tape block size has changed */
701 TapeBlockSize = rSize;
702 printf("Tape blocks read in %d Byte chunks.\n", TapeBlockSize);
705 /* Read the rest of the data in */
706 while (rSize < totalSize) {
708 ((totalSize - rSize) <
709 TapeBlockSize ? (totalSize - rSize) : TapeBlockSize);
710 rc = USD_READ(fid, &data[rSize], toread, &tSize);
719 if (rSize > totalSize)
720 printf("readData - Read > 16K data block - continuing.\n");
726 SeekFile(info, count)
727 struct butm_tapeInfo *info;
736 p = (struct progress *)info->tmRock;
738 if (isafile) { /* no reason for seeking through a file */
739 p->reading = p->writing = 0;
746 error = ForwardSpace(p->fid, count);
748 error = BackwardSpace(p->fid, -count);
753 info->status |= BUTM_STATUS_SEEKERROR;
754 ERROR_EXIT(BUTM_IOCTL);
757 info->position += count;
758 incSize(info, (count * config.fileMarkSize));
759 p = (struct progress *)info->tmRock;
760 p->reading = p->writing = 0;
768 /* Step to the next filemark if we are not at one already */
771 struct butm_tapeInfo *info;
775 if (!READS && !WRITES)
778 code = SeekFile(info, 1);
783 WriteTapeBlock(info, buffer, length, blockType)
784 struct butm_tapeInfo *info;
785 char *buffer; /* assumed to be 16384 bytes with data in it */
786 afs_int32 length; /* amount data in buffer */
789 afs_int32 code = 0, rc = 0;
791 struct tapeLabel *label;
792 struct fileMark *fmark;
793 struct blockMark *bmark;
797 p = (struct progress *)info->tmRock;
799 if (blockType == BLOCK_DATA) { /* Data Block */
802 bmark = (struct blockMark *)buffer;
803 memset(bmark, 0, sizeof(struct blockMark));
804 bmark->magic = htonl(BLOCK_MAGIC);
805 bmark->count = htonl(length);
806 } else if (blockType == BLOCK_FMBEGIN) { /* Filemark - begin */
807 fmark = (struct fileMark *)buffer;
808 fmark->magic = htonl(FILE_MAGIC);
809 fmark->nBytes = htonl(FILE_BEGIN);
810 } else if (blockType == BLOCK_FMEND) { /* Filemark - end */
811 fmark = (struct fileMark *)buffer;
812 fmark->magic = htonl(FILE_MAGIC);
813 fmark->nBytes = htonl(FILE_FMEND);
814 } else if (blockType == BLOCK_LABEL) { /* Label */
815 label = (struct tapeLabel *)buffer;
816 label->magic = htonl(TAPE_MAGIC);
817 } else if (blockType == BLOCK_EOD) { /* Filemark - EOD mark */
818 fmark = (struct fileMark *)buffer;
819 fmark->magic = htonl(FILE_MAGIC);
820 fmark->nBytes = htonl(FILE_EOD);
823 /* Write the tape block */
824 /* -------------------- */
825 rc = USD_WRITE(p->fid, buffer, BUTM_BLOCKSIZE, &wsize);
826 if ((rc == 0) && (wsize > 0)) {
827 incPosition(info, p->fid, wsize); /* record whats written */
831 if (wsize != BUTM_BLOCKSIZE) {
832 info->status |= BUTM_STATUS_WRITEERROR;
837 ERROR_EXIT(BUTM_EOT);
842 /* Write trailing EOF marker for some block types */
843 /* ---------------------------------------------- */
844 if ((blockType == BLOCK_FMEND) || (blockType == BLOCK_LABEL)
845 || (blockType == BLOCK_EOD)) {
848 error = WriteEOF(p->fid, 1);
851 info->status |= BUTM_STATUS_WRITEERROR;
852 ERROR_EXIT(BUTM_IOCTL);
855 incSize(info, config.fileMarkSize);
868 ReadTapeBlock(info, buffer, blockType)
869 struct butm_tapeInfo *info;
870 char *buffer; /* assumed to be 16384 bytes */
871 afs_int32 *blockType;
874 afs_int32 rsize, fmtype;
875 struct tapeLabel *label;
876 struct fileMark *fmark;
877 struct blockMark *bmark;
880 *blockType = BLOCK_UNKNOWN;
882 p = (struct progress *)info->tmRock;
884 memset(buffer, 0, BUTM_BLOCKSIZE);
885 label = (struct tapeLabel *)buffer;
886 fmark = (struct fileMark *)buffer;
887 bmark = (struct blockMark *)buffer;
889 rsize = readData(p->fid, buffer, BUTM_BLOCKSIZE, &info->error);
891 incPosition(info, p->fid, rsize);
895 if (rsize == 0) { /* Read a HW EOF Marker? OK */
896 *blockType = BLOCK_EOF;
897 incSize(info, config.fileMarkSize); /* Size of filemark */
899 info->position++; /* bump position */
900 p->reading = 0; /* No reads since EOF */
903 else if (rsize != BUTM_BLOCKSIZE) { /* Didn't Read a full block */
904 info->status |= BUTM_STATUS_READERROR;
905 ERROR_EXIT((rsize < 0) ? BUTM_IO : BUTM_EOT);
908 else if (ntohl(bmark->magic) == BLOCK_MAGIC) { /* Data block? */
909 *blockType = BLOCK_DATA;
912 else if (ntohl(fmark->magic) == FILE_MAGIC) { /* Read a filemark? */
913 fmtype = ntohl(fmark->nBytes);
915 if (fmtype == FILE_BEGIN) { /* filemark begin */
916 *blockType = BLOCK_FMBEGIN;
917 } else if (fmtype == FILE_FMEND) { /* filemark end */
918 *blockType = BLOCK_FMEND;
919 code = SeekFile(info, 1);
920 } else if (fmtype == FILE_EOD) { /* EOD mark */
921 *blockType = BLOCK_EOD;
922 info->status |= BUTM_STATUS_EOD;
923 code = SeekFile(info, 1);
927 else if (ntohl(label->magic) == TAPE_MAGIC) { /* Read a tape label? */
928 *blockType = BLOCK_LABEL;
929 code = SeekFile(info, 1);
940 * check version numbers and permissions in the info structure
945 struct butm_tapeInfo *info;
946 int write; /* write operation requested */
951 return (BUTM_BADARGUMENT);
953 /* Check version number in info structure */
954 if (info->structVersion != BUTM_MAJORVERSION)
955 return BUTM_OLDINTERFACE;
957 /* Check if a tape is mounted */
958 if (((p = (struct progress *)info->tmRock) == 0) || (p->fid == 0))
961 /* If writing check if there is write access */
962 if (write && (info->flags & BUTM_FLAGS_READONLY))
963 return BUTM_READONLY;
970 struct butm_tapeInfo *info;
976 p = (struct progress *)info->tmRock;
980 error = Rewind(p->fid);
985 info->status |= BUTM_STATUS_SEEKERROR;
986 ERROR_EXIT(BUTM_IOCTL);
989 info->position = (isafile ? 0 : 1);
990 info->kBytes = info->nBytes = 0;
991 info->nFiles = info->nRecords = 0;
992 p->reading = p->writing = 0;
1000 /* =====================================================================
1002 * ===================================================================== */
1005 file_Mount(info, tape)
1006 struct butm_tapeInfo *info;
1013 afs_int32 code = 0, error = 0, rc = 0;
1016 printf("butm: Mount tape drive\n");
1022 ERROR_EXIT(BUTM_BADARGUMENT);
1023 if (info->structVersion != BUTM_MAJORVERSION)
1024 ERROR_EXIT(BUTM_OLDINTERFACE);
1026 ERROR_EXIT(BUTM_PARALLELMOUNTS);
1027 if (strlen(tape) >= sizeof(info->name))
1028 ERROR_EXIT(BUTM_BADARGUMENT);
1030 strcpy(info->name, tape);
1032 strcpy(filename, config.tapedir); /* the name of the tape device */
1033 info->position = (isafile ? 0 : 1);
1034 info->kBytes = info->nBytes = 0;
1035 info->nRecords = info->nFiles = 0;
1036 info->recordSize = 0;
1037 info->tapeSize = config.tapeSize;
1038 info->coefBytes = 1;
1039 info->coefRecords = 0;
1040 info->coefFiles = sizeof(struct fileMark);
1041 info->simultaneousTapes = 1;
1044 info->flags = BUTM_FLAGS_SEQUENTIAL;
1048 xflags |= USD_OPEN_CREATE;
1051 * try to open in a child process first so nothing will
1052 * time out should the process block because the device
1056 if (ForkOpen(filename)) {
1057 ERROR_EXIT(BUTM_MOUNTFAIL);
1061 /* Now go ahead and open the tape drive for real */
1062 rc = usd_Open(filename, (USD_OPEN_RDWR | USD_OPEN_WLOCK | xflags), 0777,
1064 if (rc != 0) { /* try for lesser access */
1065 rc = usd_Open(filename, (USD_OPEN_RDONLY | USD_OPEN_RLOCK), 0, &fid);
1069 ERROR_EXIT(BUTM_MOUNTFAIL);
1071 info->flags |= BUTM_FLAGS_READONLY;
1074 (void)PrepareAccess(fid); /* for NT */
1076 p = (struct progress *)malloc(sizeof(*p));
1077 info->tmRock = (char *)p;
1079 p->mountId = config.mountId = time(0);
1080 p->reading = p->writing = 0;
1082 TapeBlockSize = BUTM_BLOCKSIZE; /* Initialize */
1086 info->error = error;
1092 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(info, label, rewind)
1145 struct butm_tapeInfo *info;
1146 struct butm_tapeLabel *label;
1151 struct tapeLabel *tlabel;
1153 afs_hyper_t off; /* offset */
1156 printf("butm: Write tape label\n");
1161 code = check(info, WRITE_OP);
1165 ERROR_EXIT(BUTM_BADARGUMENT);
1166 if (label->structVersion != CUR_TAPE_VERSION)
1167 ERROR_EXIT(BUTM_OLDINTERFACE);
1169 if (rewind) { /* Not appending, so rewind */
1170 code = rewindFile(info);
1175 p = (struct progress *)info->tmRock;
1177 code = USD_IOCTL(p->fid, USD_IOCTL_SETSIZE, &off);
1179 ERROR_EXIT(BUTM_POSITION);
1182 if (READS || WRITES)
1183 ERROR_EXIT(BUTM_BADOP);
1186 /* Copy the label into the tape block
1187 * ---------------------------------- */
1188 memset(tapeBlock, 0, BUTM_BLOCKSIZE);
1190 if (!label->creationTime)
1191 label->creationTime = time(0);
1193 tlabel = (struct tapeLabel *)tapeBlock;
1194 memcpy(&tlabel->label, label, sizeof(struct butm_tapeLabel));
1195 tlabel->label.structVersion = htonl(CUR_TAPE_VERSION);
1196 tlabel->label.creationTime = htonl(tlabel->label.creationTime);
1197 tlabel->label.expirationDate = htonl(tlabel->label.expirationDate);
1198 tlabel->label.size = htonl(tlabel->label.size);
1199 tlabel->label.useCount = htonl(tlabel->label.useCount);
1200 tlabel->label.dumpid = htonl(tlabel->label.dumpid);
1203 * write the tape label - For appends, the write may need to skip
1204 * over 1 or 2 EOF marks that were written when tape was closed after
1205 * the last dump. Plus, some AIX tape drives require we try forwarding
1206 * over the last EOF and take an error before we can write the new label.
1207 * ---------------------------------------------------------------------- */
1208 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1209 if (!isafile && !rewind && (code == BUTM_IO))
1210 do { /* do if write failed */
1211 fcode = SeekFile(info, 1); /* skip over the EOF */
1213 break; /* leave if error */
1216 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1217 if (code != BUTM_IO)
1218 break; /* continue if write failed */
1220 fcode = SeekFile(info, 1); /* skip over the EOF */
1221 if (fcode) { /* retry 1 write if couldn't skip */
1223 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1229 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1230 if (code != BUTM_IO)
1231 break; /* continue if write failed */
1233 fcode = SeekFile(info, 1); /* skip over the EOF */
1234 if (fcode) { /* retry 1 write if couldn't skip */
1236 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1243 /* clear the write error status a failed WriteTapeBlock may have produced */
1245 info->status &= ~BUTM_STATUS_WRITEERROR;
1252 file_ReadLabel(info, label, rewind)
1253 struct butm_tapeInfo *info;
1254 struct butm_tapeLabel *label;
1257 struct tapeLabel *tlabel;
1259 afs_int32 blockType;
1262 printf("butm: Read tape label\n");
1267 code = check(info, READ_OP);
1270 if (READS || WRITES)
1271 ERROR_EXIT(BUTM_BADOP);
1274 code = rewindFile(info);
1276 ERROR_EXIT(code); /* status is set so return */
1280 * When appended labels were written, either 1 or 2 EOF marks may
1281 * have had to be skipped. When reading a label, these EOF marks
1282 * must also be skipped. When an EOF is read, 0 bytes are returned
1283 * (refer to the write calls in file_WriteLabel routine).
1284 * ---------------------------------------------------------------- */
1285 code = ReadTapeBlock(info, tapeBlock, &blockType);
1286 if (!isafile && !rewind && (blockType == BLOCK_EOF))
1288 code = ReadTapeBlock(info, tapeBlock, &blockType);
1289 if (blockType != BLOCK_EOF)
1290 break; /* didn't read an EOF */
1292 code = ReadTapeBlock(info, tapeBlock, &blockType);
1295 if (blockType == BLOCK_UNKNOWN)
1296 ERROR_EXIT(BUTM_NOLABEL);
1299 if (blockType != BLOCK_LABEL)
1300 ERROR_EXIT(BUTM_BADBLOCK);
1305 tlabel = (struct tapeLabel *)tapeBlock;
1306 memcpy(label, &tlabel->label, sizeof(struct butm_tapeLabel));
1307 label->structVersion = ntohl(label->structVersion);
1308 label->creationTime = ntohl(label->creationTime);
1309 label->expirationDate = ntohl(label->expirationDate);
1310 label->size = ntohl(label->size);
1311 label->dumpid = ntohl(label->dumpid);
1312 label->useCount = ntohl(label->useCount);
1314 info->tapeSize = label->size; /* use size from label */
1322 file_WriteFileBegin(info)
1323 struct butm_tapeInfo *info;
1326 afs_int32 error = 0;
1329 printf("butm: Write filemark begin\n");
1333 code = check(info, WRITE_OP);
1337 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMBEGIN);
1348 file_ReadFileBegin(info)
1349 struct butm_tapeInfo *info;
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;
1534 afs_int32 blockType;
1537 printf("butm: Read filemark end\n");
1542 code = check(info, READ_OP);
1545 if (!READS || WRITES)
1546 ERROR_EXIT(BUTM_BADOP);
1548 info->status &= ~BUTM_STATUS_EOF;
1550 code = ReadTapeBlock(info, tapeBlock, &blockType);
1554 if ((blockType != BLOCK_FMEND) && (blockType != BLOCK_EOF))
1555 ERROR_EXIT(BUTM_BADBLOCK);
1562 * Write the end-of-dump marker.
1565 file_WriteEODump(info)
1566 struct butm_tapeInfo *info;
1571 printf("butm: Write filemark EOD\n");
1576 code = check(info, WRITE_OP);
1579 if (READS || WRITES)
1580 ERROR_EXIT(BUTM_BADOP);
1582 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_EOD);
1586 info->status |= BUTM_STATUS_EOD;
1593 file_Seek(info, position)
1594 struct butm_tapeInfo *info;
1602 afs_hyper_t startOff, stopOff; /* for normal file(non-tape) seeks */
1605 printf("butm: Seek to the tape position %d\n", position);
1609 code = check(info, READ_OP);
1614 p = (struct progress *)info->tmRock;
1615 posit = (osi_lloff_t) position *(osi_lloff_t) BUTM_BLOCKSIZE;
1617 /* Not really necessary to do it this way, should be fixed */
1620 d = (posit & 0xffffffff);
1625 hset64(startOff, c, d);
1627 w = USD_SEEK(p->fid, startOff, SEEK_SET, &stopOff);
1630 if (hcmp(startOff, stopOff) != 0)
1631 ERROR_EXIT(BUTM_POSITION);
1633 p->reading = p->writing = 0;
1634 info->position = position;
1636 /* Don't position backwards if we are in-between FMs */
1637 if ((READS || WRITES) && ((position - info->position) <= 0))
1638 ERROR_EXIT(BUTM_BADOP);
1640 code = SeekFile(info, (position - info->position));
1650 * Seek to the EODump (end-of-dump) after the given position. This is
1651 * the position after the EOF filemark immediately after the EODump mark.
1652 * This is for tapes of version 4 or greater.
1655 file_SeekEODump(info, position)
1656 struct butm_tapeInfo *info;
1660 afs_int32 blockType;
1663 afs_hyper_t startOff, stopOff; /* file seek offsets */
1666 printf("butm: Seek to end-of-dump\n");
1669 code = check(info, READ_OP);
1672 if (READS || WRITES)
1673 ERROR_EXIT(BUTM_BADOP);
1676 p = (struct progress *)info->tmRock;
1677 hset64(startOff, 0, 0);
1678 w = USD_SEEK(p->fid, startOff, SEEK_END, &stopOff);
1681 ERROR_EXIT(BUTM_POSITION);
1684 if (hgetlo(stopOff) % BUTM_BLOCKSIZE)
1685 ERROR_EXIT(BUTM_POSITION);
1686 info->position = (hgetlo(stopOff) / BUTM_BLOCKSIZE);
1688 /* Seek to the desired position */
1689 code = SeekFile(info, (position - info->position) + 1);
1694 * Search until the filemark is an EODump filemark.
1695 * Skip over volumes only.
1698 code = ReadTapeBlock(info, tapeBlock, &blockType);
1702 if (blockType == BLOCK_EOD)
1704 if (blockType != BLOCK_FMBEGIN)
1705 ERROR_EXIT(BUTM_BADBLOCK);
1707 code = SeekFile(info, 1); /* Step forward to next volume */
1719 file_SetSize(info, size)
1720 struct butm_tapeInfo *info;
1724 printf("butm: Set size of tape\n");
1728 info->tapeSize = config.tapeSize;
1730 info->tapeSize = size;
1735 file_GetSize(info, size)
1736 struct butm_tapeInfo *info;
1740 printf("butm: Get size of tape\n");
1743 *size = info->tapeSize;
1747 /* =====================================================================
1748 * Startup/configuration routines.
1749 * ===================================================================== */
1752 file_Configure(file)
1753 struct tapeConfig *file;
1756 afs_com_err(whoami, BUTM_BADCONFIG, "device not specified");
1757 return BUTM_BADCONFIG;
1760 config.tapeSize = file->capacity;
1761 config.fileMarkSize = file->fileMarkSize;
1762 config.portOffset = file->portOffset;
1763 strcpy(config.tapedir, file->device);
1765 /* Tape must be large enough to at least fit a label */
1766 if (config.tapeSize <= 0) {
1767 afs_com_err(whoami, BUTM_BADCONFIG, "Tape size bogus: %d Kbytes",
1769 return BUTM_BADCONFIG;
1772 if (strlen(config.tapedir) == 0) {
1773 afs_com_err(whoami, BUTM_BADCONFIG, "no tape device specified");
1774 return BUTM_BADCONFIG;
1781 /* This procedure instantiates a tape module of type file_tm. */
1783 butm_file_Instantiate(info, file)
1784 struct butm_tapeInfo *info;
1785 struct tapeConfig *file;
1787 extern int debugLevel;
1790 if (debugLevel > 98)
1791 printf("butm: Instantiate butc\n");
1794 ERROR_EXIT(BUTM_BADARGUMENT);
1795 if (info->structVersion != BUTM_MAJORVERSION)
1796 ERROR_EXIT(BUTM_OLDINTERFACE);
1798 memset(info, 0, sizeof(struct butm_tapeInfo));
1799 info->structVersion = BUTM_MAJORVERSION;
1800 info->ops.mount = file_Mount;
1801 info->ops.dismount = file_Dismount;
1802 info->ops.create = file_WriteLabel;
1803 info->ops.readLabel = file_ReadLabel;
1804 info->ops.seek = file_Seek;
1805 info->ops.seekEODump = file_SeekEODump;
1806 info->ops.readFileBegin = file_ReadFileBegin;
1807 info->ops.readFileData = file_ReadFileData;
1808 info->ops.readFileEnd = file_ReadFileEnd;
1809 info->ops.writeFileBegin = file_WriteFileBegin;
1810 info->ops.writeFileData = file_WriteFileData;
1811 info->ops.writeFileEnd = file_WriteFileEnd;
1812 info->ops.writeEOT = file_WriteEODump;
1813 info->ops.setSize = file_SetSize;
1814 info->ops.getSize = file_GetSize;
1815 info->debug = ((debugLevel > 98) ? 1 : 0);
1817 code = file_Configure(file);