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 0x%04x 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
631 incSize(struct butm_tapeInfo *info, afs_uint32 dataSize)
633 info->nBytes += dataSize;
634 info->kBytes += (info->nBytes / 1024);
635 info->nBytes %= 1024;
639 * IF YOU ADD/CHANGE THE ifdefs, BE SURE
640 * TO ALSO MAKE THE SAME CHANGES IN bu_utils/fms.c.
642 * add the supplied no. of bytes to the byte count of data placed
643 * on the tape. Also check for reaching 2GB limit and reset the
644 * pointer if necessary. This allows us to use >2GB tapes.
646 * fid - file id for the tape.
647 * dataSize - bytes used on the tape
651 incPosition(struct butm_tapeInfo *info, usd_handle_t fid, afs_uint32 dataSize)
653 /* Add this to the amount of data written to the tape */
654 incSize(info, dataSize);
656 info->posCount += dataSize;
658 if (info->posCount >= 2147467264) { /* 2GB - 16K */
660 #if (defined(AFS_SUN_ENV) || defined(AFS_LINUX24_ENV))
664 USD_IOCTL(fid, USD_IOCTL_SETSIZE, &off);
671 * This accounts for tape drives with a block size different from variable or 16K
672 * blocks and only reads that block size.
674 afs_int32 TapeBlockSize;
676 readData(usd_handle_t fid, char *data, afs_int32 totalSize, afs_int32 *errorP)
678 afs_int32 toread; /* Number of bytes to read */
679 afs_int32 rSize; /* Total bytes read so far */
680 afs_int32 tSize; /* Temporary size */
681 afs_int32 rc; /* return code */
683 toread = totalSize; /* First, try to read all the data */
685 rc = USD_READ(fid, &data[0], toread, &rSize);
690 if (rSize == 0) /* reached EOF */
693 if (rSize != TapeBlockSize) { /* Tape block size has changed */
694 TapeBlockSize = rSize;
695 printf("Tape blocks read in %d Byte chunks.\n", TapeBlockSize);
698 /* Read the rest of the data in */
699 while (rSize < totalSize) {
701 ((totalSize - rSize) <
702 TapeBlockSize ? (totalSize - rSize) : TapeBlockSize);
703 rc = USD_READ(fid, &data[rSize], toread, &tSize);
712 if (rSize > totalSize)
713 printf("readData - Read > 16K data block - continuing.\n");
719 SeekFile(struct butm_tapeInfo *info, int count)
727 p = (struct progress *)info->tmRock;
729 if (isafile) { /* no reason for seeking through a file */
730 p->reading = p->writing = 0;
737 error = ForwardSpace(p->fid, count);
739 error = BackwardSpace(p->fid, -count);
744 info->status |= BUTM_STATUS_SEEKERROR;
745 ERROR_EXIT(BUTM_IOCTL);
748 info->position += count;
749 incSize(info, (count * config.fileMarkSize));
750 p = (struct progress *)info->tmRock;
751 p->reading = p->writing = 0;
759 /* Step to the next filemark if we are not at one already */
761 NextFile(struct butm_tapeInfo *info)
765 if (!READS && !WRITES)
768 code = SeekFile(info, 1);
773 WriteTapeBlock(struct butm_tapeInfo *info,
774 char *buffer, /* assumed to be 16384 bytes with data in it */
775 afs_int32 length, /* amount data in buffer */
778 afs_int32 code = 0, rc = 0;
780 struct tapeLabel *label;
781 struct fileMark *fmark;
782 struct blockMark *bmark;
786 p = (struct progress *)info->tmRock;
788 if (blockType == BLOCK_DATA) { /* Data Block */
791 bmark = (struct blockMark *)buffer;
792 memset(bmark, 0, sizeof(struct blockMark));
793 bmark->magic = htonl(BLOCK_MAGIC);
794 bmark->count = htonl(length);
795 } else if (blockType == BLOCK_FMBEGIN) { /* Filemark - begin */
796 fmark = (struct fileMark *)buffer;
797 fmark->magic = htonl(FILE_MAGIC);
798 fmark->nBytes = htonl(FILE_BEGIN);
799 } else if (blockType == BLOCK_FMEND) { /* Filemark - end */
800 fmark = (struct fileMark *)buffer;
801 fmark->magic = htonl(FILE_MAGIC);
802 fmark->nBytes = htonl(FILE_FMEND);
803 } else if (blockType == BLOCK_LABEL) { /* Label */
804 label = (struct tapeLabel *)buffer;
805 label->magic = htonl(TAPE_MAGIC);
806 } else if (blockType == BLOCK_EOD) { /* Filemark - EOD mark */
807 fmark = (struct fileMark *)buffer;
808 fmark->magic = htonl(FILE_MAGIC);
809 fmark->nBytes = htonl(FILE_EOD);
812 /* Write the tape block */
813 /* -------------------- */
814 rc = USD_WRITE(p->fid, buffer, BUTM_BLOCKSIZE, &wsize);
815 if ((rc == 0) && (wsize > 0)) {
816 incPosition(info, p->fid, wsize); /* record whats written */
820 if (wsize != BUTM_BLOCKSIZE) {
821 info->status |= BUTM_STATUS_WRITEERROR;
826 ERROR_EXIT(BUTM_EOT);
831 /* Write trailing EOF marker for some block types */
832 /* ---------------------------------------------- */
833 if ((blockType == BLOCK_FMEND) || (blockType == BLOCK_LABEL)
834 || (blockType == BLOCK_EOD)) {
837 error = WriteEOF(p->fid, 1);
840 info->status |= BUTM_STATUS_WRITEERROR;
841 ERROR_EXIT(BUTM_IOCTL);
844 incSize(info, config.fileMarkSize);
857 ReadTapeBlock(struct butm_tapeInfo *info,
858 char *buffer, /* assumed to be 16384 bytes */
859 afs_int32 *blockType)
862 afs_int32 rsize, fmtype;
863 struct tapeLabel *label;
864 struct fileMark *fmark;
865 struct blockMark *bmark;
868 *blockType = BLOCK_UNKNOWN;
870 p = (struct progress *)info->tmRock;
872 memset(buffer, 0, BUTM_BLOCKSIZE);
873 label = (struct tapeLabel *)buffer;
874 fmark = (struct fileMark *)buffer;
875 bmark = (struct blockMark *)buffer;
877 rsize = readData(p->fid, buffer, BUTM_BLOCKSIZE, &info->error);
879 incPosition(info, p->fid, rsize);
883 if (rsize == 0) { /* Read a HW EOF Marker? OK */
884 *blockType = BLOCK_EOF;
885 incSize(info, config.fileMarkSize); /* Size of filemark */
887 info->position++; /* bump position */
888 p->reading = 0; /* No reads since EOF */
891 else if (rsize != BUTM_BLOCKSIZE) { /* Didn't Read a full block */
892 info->status |= BUTM_STATUS_READERROR;
893 ERROR_EXIT((rsize < 0) ? BUTM_IO : BUTM_EOT);
896 else if (ntohl(bmark->magic) == BLOCK_MAGIC) { /* Data block? */
897 *blockType = BLOCK_DATA;
900 else if (ntohl(fmark->magic) == FILE_MAGIC) { /* Read a filemark? */
901 fmtype = ntohl(fmark->nBytes);
903 if (fmtype == FILE_BEGIN) { /* filemark begin */
904 *blockType = BLOCK_FMBEGIN;
905 } else if (fmtype == FILE_FMEND) { /* filemark end */
906 *blockType = BLOCK_FMEND;
907 code = SeekFile(info, 1);
908 } else if (fmtype == FILE_EOD) { /* EOD mark */
909 *blockType = BLOCK_EOD;
910 info->status |= BUTM_STATUS_EOD;
911 code = SeekFile(info, 1);
915 else if (ntohl(label->magic) == TAPE_MAGIC) { /* Read a tape label? */
916 *blockType = BLOCK_LABEL;
917 code = SeekFile(info, 1);
928 * check version numbers and permissions in the info structure
932 check(struct butm_tapeInfo *info,
933 int write) /* write operation requested */
938 return (BUTM_BADARGUMENT);
940 /* Check version number in info structure */
941 if (info->structVersion != BUTM_MAJORVERSION)
942 return BUTM_OLDINTERFACE;
944 /* Check if a tape is mounted */
945 if (((p = (struct progress *)info->tmRock) == 0) || (p->fid == 0))
948 /* If writing check if there is write access */
949 if (write && (info->flags & BUTM_FLAGS_READONLY))
950 return BUTM_READONLY;
956 rewindFile(struct butm_tapeInfo *info)
962 p = (struct progress *)info->tmRock;
966 error = Rewind(p->fid);
971 info->status |= BUTM_STATUS_SEEKERROR;
972 ERROR_EXIT(BUTM_IOCTL);
975 info->position = (isafile ? 0 : 1);
976 info->kBytes = info->nBytes = 0;
977 info->nFiles = info->nRecords = 0;
978 p->reading = p->writing = 0;
986 /* =====================================================================
988 * ===================================================================== */
991 file_Mount(struct butm_tapeInfo *info, char *tape)
997 afs_int32 code = 0, error = 0, rc = 0;
1000 printf("butm: Mount tape drive\n");
1006 ERROR_EXIT(BUTM_BADARGUMENT);
1007 if (info->structVersion != BUTM_MAJORVERSION)
1008 ERROR_EXIT(BUTM_OLDINTERFACE);
1010 ERROR_EXIT(BUTM_PARALLELMOUNTS);
1011 if (strlen(tape) >= sizeof(info->name))
1012 ERROR_EXIT(BUTM_BADARGUMENT);
1014 strcpy(info->name, tape);
1016 strcpy(filename, config.tapedir); /* the name of the tape device */
1017 info->position = (isafile ? 0 : 1);
1018 info->kBytes = info->nBytes = 0;
1019 info->nRecords = info->nFiles = 0;
1020 info->recordSize = 0;
1021 info->tapeSize = config.tapeSize;
1022 info->coefBytes = 1;
1023 info->coefRecords = 0;
1024 info->coefFiles = sizeof(struct fileMark);
1025 info->simultaneousTapes = 1;
1028 info->flags = BUTM_FLAGS_SEQUENTIAL;
1032 xflags |= USD_OPEN_CREATE;
1035 * try to open in a child process first so nothing will
1036 * time out should the process block because the device
1040 if (ForkOpen(filename)) {
1041 ERROR_EXIT(BUTM_MOUNTFAIL);
1045 /* Now go ahead and open the tape drive for real */
1046 rc = usd_Open(filename, (USD_OPEN_RDWR | USD_OPEN_WLOCK | xflags), 0777,
1048 if (rc != 0) { /* try for lesser access */
1049 rc = usd_Open(filename, (USD_OPEN_RDONLY | USD_OPEN_RLOCK), 0, &fid);
1053 ERROR_EXIT(BUTM_MOUNTFAIL);
1055 info->flags |= BUTM_FLAGS_READONLY;
1058 (void)PrepareAccess(fid); /* for NT */
1060 p = (struct progress *)malloc(sizeof(*p));
1061 info->tmRock = (char *)p;
1063 p->mountId = config.mountId = time(0);
1064 p->reading = p->writing = 0;
1066 TapeBlockSize = BUTM_BLOCKSIZE; /* Initialize */
1070 info->error = error;
1075 file_Dismount(struct butm_tapeInfo *info)
1078 afs_int32 code = 0, error = 0;
1081 printf("butm: Unmount tape drive\n");
1086 code = check(info, READ_OP);
1090 p = (struct progress *)info->tmRock;
1092 (void)ShutdownAccess(p->fid); /* for NT */
1094 /* close the device */
1095 if ((error = ForkClose(p->fid))) {
1096 printf("butm: Tape close failed. Error %d\n", errno);
1102 code = BUTM_DISMOUNTFAIL;
1103 info->status |= BUTM_STATUS_TAPEERROR;
1107 info->tmRock = 0; /* mark it as closed - even if error on close */
1113 info->error = error;
1118 * write the header on a tape
1120 * info - handle on tape unit
1121 * label - label information. This label is not copied onto the tape.
1122 * If supplied, various fields are copied from this label to
1123 * the actual tape label written on the tape.
1127 file_WriteLabel(struct butm_tapeInfo *info, struct butm_tapeLabel *label,
1132 struct tapeLabel *tlabel;
1134 afs_hyper_t off; /* offset */
1137 printf("butm: Write tape label\n");
1142 code = check(info, WRITE_OP);
1146 ERROR_EXIT(BUTM_BADARGUMENT);
1147 if (label->structVersion != CUR_TAPE_VERSION)
1148 ERROR_EXIT(BUTM_OLDINTERFACE);
1150 if (rewind) { /* Not appending, so rewind */
1151 code = rewindFile(info);
1156 p = (struct progress *)info->tmRock;
1158 code = USD_IOCTL(p->fid, USD_IOCTL_SETSIZE, &off);
1160 ERROR_EXIT(BUTM_POSITION);
1163 if (READS || WRITES)
1164 ERROR_EXIT(BUTM_BADOP);
1167 /* Copy the label into the tape block
1168 * ---------------------------------- */
1169 memset(tapeBlock, 0, BUTM_BLOCKSIZE);
1171 if (!label->creationTime)
1172 label->creationTime = time(0);
1174 tlabel = (struct tapeLabel *)tapeBlock;
1175 memcpy(&tlabel->label, label, sizeof(struct butm_tapeLabel));
1176 tlabel->label.structVersion = htonl(CUR_TAPE_VERSION);
1177 tlabel->label.creationTime = htonl(tlabel->label.creationTime);
1178 tlabel->label.expirationDate = htonl(tlabel->label.expirationDate);
1179 tlabel->label.size = htonl(tlabel->label.size);
1180 tlabel->label.useCount = htonl(tlabel->label.useCount);
1181 tlabel->label.dumpid = htonl(tlabel->label.dumpid);
1184 * write the tape label - For appends, the write may need to skip
1185 * over 1 or 2 EOF marks that were written when tape was closed after
1186 * the last dump. Plus, some AIX tape drives require we try forwarding
1187 * over the last EOF and take an error before we can write the new label.
1188 * ---------------------------------------------------------------------- */
1189 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1190 if (!isafile && !rewind && (code == BUTM_IO))
1191 do { /* do if write failed */
1192 fcode = SeekFile(info, 1); /* skip over the EOF */
1194 break; /* leave if error */
1197 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1198 if (code != BUTM_IO)
1199 break; /* continue if write failed */
1201 fcode = SeekFile(info, 1); /* skip over the EOF */
1202 if (fcode) { /* retry 1 write if couldn't skip */
1204 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1210 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_LABEL);
1211 if (code != BUTM_IO)
1212 break; /* continue if write failed */
1214 fcode = SeekFile(info, 1); /* skip over the EOF */
1215 if (fcode) { /* retry 1 write if couldn't skip */
1217 WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE,
1224 /* clear the write error status a failed WriteTapeBlock may have produced */
1226 info->status &= ~BUTM_STATUS_WRITEERROR;
1233 file_ReadLabel(struct butm_tapeInfo *info, struct butm_tapeLabel *label,
1236 struct tapeLabel *tlabel;
1238 afs_int32 blockType;
1241 printf("butm: Read tape label\n");
1246 code = check(info, READ_OP);
1249 if (READS || WRITES)
1250 ERROR_EXIT(BUTM_BADOP);
1253 code = rewindFile(info);
1255 ERROR_EXIT(code); /* status is set so return */
1259 * When appended labels were written, either 1 or 2 EOF marks may
1260 * have had to be skipped. When reading a label, these EOF marks
1261 * must also be skipped. When an EOF is read, 0 bytes are returned
1262 * (refer to the write calls in file_WriteLabel routine).
1263 * ---------------------------------------------------------------- */
1264 code = ReadTapeBlock(info, tapeBlock, &blockType);
1265 if (!isafile && !rewind && (blockType == BLOCK_EOF))
1267 code = ReadTapeBlock(info, tapeBlock, &blockType);
1268 if (blockType != BLOCK_EOF)
1269 break; /* didn't read an EOF */
1271 code = ReadTapeBlock(info, tapeBlock, &blockType);
1274 if (blockType == BLOCK_UNKNOWN)
1275 ERROR_EXIT(BUTM_NOLABEL);
1278 if (blockType != BLOCK_LABEL)
1279 ERROR_EXIT(BUTM_BADBLOCK);
1284 tlabel = (struct tapeLabel *)tapeBlock;
1285 memcpy(label, &tlabel->label, sizeof(struct butm_tapeLabel));
1286 label->structVersion = ntohl(label->structVersion);
1287 label->creationTime = ntohl(label->creationTime);
1288 label->expirationDate = ntohl(label->expirationDate);
1289 label->size = ntohl(label->size);
1290 label->dumpid = ntohl(label->dumpid);
1291 label->useCount = ntohl(label->useCount);
1293 info->tapeSize = label->size; /* use size from label */
1301 file_WriteFileBegin(struct butm_tapeInfo *info)
1306 printf("butm: Write filemark begin\n");
1310 code = check(info, WRITE_OP);
1314 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMBEGIN);
1325 file_ReadFileBegin(struct butm_tapeInfo *info)
1328 afs_int32 blockType;
1331 printf("butm: Read filemark begin\n");
1336 code = check(info, READ_OP);
1339 if (READS || WRITES)
1340 ERROR_EXIT(BUTM_BADOP);
1342 code = ReadTapeBlock(info, tapeBlock, &blockType);
1346 if (blockType != BLOCK_FMBEGIN) {
1347 if (blockType == BLOCK_EOD)
1348 ERROR_EXIT(BUTM_EOD); /* EODump label */
1349 if (blockType == BLOCK_LABEL)
1350 ERROR_EXIT(BUTM_LABEL); /* Tape label */
1351 ERROR_EXIT(BUTM_BADBLOCK); /* Other */
1358 /* Writes data out in block sizes of 16KB. Does destroy the data.
1359 * Assumes the data buffer has a space reserved at beginning for a blockMark.
1362 file_WriteFileData(struct butm_tapeInfo *info, char *data, afs_int32 blocks, afs_int32 len)
1367 char *bstart; /* Where block starts for a 16K block */
1368 char *dstart; /* Where data starts for a 16K block */
1371 printf("butm: Write tape data - %u bytes\n", len);
1376 code = check(info, WRITE_OP);
1379 if (!data || (len < 0))
1380 ERROR_EXIT(BUTM_BADARGUMENT);
1381 if (READS || !WRITES)
1382 ERROR_EXIT(BUTM_BADOP);
1384 b = 0; /* start at block 0 */
1386 dstart = &data[b * BUTM_BLKSIZE];
1387 bstart = dstart - sizeof(struct blockMark);
1389 if (len < BUTM_BLKSIZE) {
1390 memset(&dstart[len], 0, BUTM_BLKSIZE - len);
1393 length = BUTM_BLKSIZE;
1396 code = WriteTapeBlock(info, bstart, length, BLOCK_DATA);
1400 /* If there are more blocks, step to next block */
1401 /* Otherwise, copy the data to beginning of last block */
1403 if (b < (blocks - 1))
1406 memcpy(&dstart[0], &dstart[BUTM_BLKSIZE], len);
1413 /* file_ReadFileData
1414 * Read a data block from tape.
1416 * info - tape info structure, c.f. fid
1417 * data - ptr to buffer for data
1418 * len - size of data buffer
1420 * nBytes - no. of data bytes read.
1424 file_ReadFileData(struct butm_tapeInfo *info, char *data, int len, int *nBytes)
1426 struct blockMark *bmark;
1428 afs_int32 blockType;
1431 printf("butm: Read tape data - %u bytes\n", len);
1437 ERROR_EXIT(BUTM_BADARGUMENT);
1440 code = check(info, READ_OP);
1443 if (!data || (len < 0) || (len > BUTM_BLKSIZE))
1444 ERROR_EXIT(BUTM_BADARGUMENT);
1445 if (!READS || WRITES)
1446 ERROR_EXIT(BUTM_BADOP);
1448 data -= sizeof(struct blockMark);
1449 code = ReadTapeBlock(info, data, &blockType);
1453 if (blockType != BLOCK_DATA) {
1454 if (blockType == BLOCK_EOF)
1455 ERROR_EXIT(BUTM_EOF);
1456 if (blockType == BLOCK_FMEND)
1457 ERROR_EXIT(BUTM_ENDVOLUME);
1458 ERROR_EXIT(BUTM_BADBLOCK);
1461 bmark = (struct blockMark *)data;
1462 *nBytes = ntohl(bmark->count); /* Size of data in buf */
1469 file_WriteFileEnd(struct butm_tapeInfo *info)
1474 printf("butm: Write filemark end\n");
1479 code = check(info, WRITE_OP);
1482 if (READS || !WRITES)
1483 ERROR_EXIT(BUTM_BADOP);
1485 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_FMEND);
1491 /* Read a SW filemark, verify that it is a SW filemark, then skip to the next
1492 * HW filemark. If the read of the SW filemark shows it's an EOF, then
1493 * ignore that the SW filemark is not there and return 0 (found the SW filemark
1494 * missing with some 3.1 dumps).
1497 file_ReadFileEnd(struct butm_tapeInfo *info)
1500 afs_int32 blockType;
1503 printf("butm: Read filemark end\n");
1508 code = check(info, READ_OP);
1511 if (!READS || WRITES)
1512 ERROR_EXIT(BUTM_BADOP);
1514 info->status &= ~BUTM_STATUS_EOF;
1516 code = ReadTapeBlock(info, tapeBlock, &blockType);
1520 if ((blockType != BLOCK_FMEND) && (blockType != BLOCK_EOF))
1521 ERROR_EXIT(BUTM_BADBLOCK);
1528 * Write the end-of-dump marker.
1531 file_WriteEODump(struct butm_tapeInfo *info)
1536 printf("butm: Write filemark EOD\n");
1541 code = check(info, WRITE_OP);
1544 if (READS || WRITES)
1545 ERROR_EXIT(BUTM_BADOP);
1547 code = WriteTapeBlock(info, tapeBlock, BUTM_BLOCKSIZE, BLOCK_EOD);
1551 info->status |= BUTM_STATUS_EOD;
1558 file_Seek(struct butm_tapeInfo *info, afs_int32 position)
1565 afs_hyper_t startOff, stopOff; /* for normal file(non-tape) seeks */
1568 printf("butm: Seek to the tape position %d\n", position);
1572 code = check(info, READ_OP);
1577 p = (struct progress *)info->tmRock;
1578 posit = (osi_lloff_t) position *(osi_lloff_t) BUTM_BLOCKSIZE;
1580 /* Not really necessary to do it this way, should be fixed */
1583 d = (posit & 0xffffffff);
1588 hset64(startOff, c, d);
1590 w = USD_SEEK(p->fid, startOff, SEEK_SET, &stopOff);
1593 if (hcmp(startOff, stopOff) != 0)
1594 ERROR_EXIT(BUTM_POSITION);
1596 p->reading = p->writing = 0;
1597 info->position = position;
1599 /* Don't position backwards if we are in-between FMs */
1600 if ((READS || WRITES) && ((position - info->position) <= 0))
1601 ERROR_EXIT(BUTM_BADOP);
1603 code = SeekFile(info, (position - info->position));
1613 * Seek to the EODump (end-of-dump) after the given position. This is
1614 * the position after the EOF filemark immediately after the EODump mark.
1615 * This is for tapes of version 4 or greater.
1618 file_SeekEODump(struct butm_tapeInfo *info, afs_int32 position)
1621 afs_int32 blockType;
1624 afs_hyper_t startOff, stopOff; /* file seek offsets */
1627 printf("butm: Seek to end-of-dump\n");
1630 code = check(info, READ_OP);
1633 if (READS || WRITES)
1634 ERROR_EXIT(BUTM_BADOP);
1637 p = (struct progress *)info->tmRock;
1638 hset64(startOff, 0, 0);
1639 w = USD_SEEK(p->fid, startOff, SEEK_END, &stopOff);
1642 ERROR_EXIT(BUTM_POSITION);
1645 if (hgetlo(stopOff) % BUTM_BLOCKSIZE)
1646 ERROR_EXIT(BUTM_POSITION);
1647 info->position = (hgetlo(stopOff) / BUTM_BLOCKSIZE);
1649 /* Seek to the desired position */
1650 code = SeekFile(info, (position - info->position) + 1);
1655 * Search until the filemark is an EODump filemark.
1656 * Skip over volumes only.
1659 code = ReadTapeBlock(info, tapeBlock, &blockType);
1663 if (blockType == BLOCK_EOD)
1665 if (blockType != BLOCK_FMBEGIN)
1666 ERROR_EXIT(BUTM_BADBLOCK);
1668 code = SeekFile(info, 1); /* Step forward to next volume */
1680 file_SetSize(struct butm_tapeInfo *info, afs_uint32 size)
1683 printf("butm: Set size of tape\n");
1687 info->tapeSize = config.tapeSize;
1689 info->tapeSize = size;
1694 file_GetSize(struct butm_tapeInfo *info, afs_uint32 *size)
1697 printf("butm: Get size of tape\n");
1700 *size = info->tapeSize;
1704 /* =====================================================================
1705 * Startup/configuration routines.
1706 * ===================================================================== */
1709 file_Configure(struct tapeConfig *file)
1712 afs_com_err(whoami, BUTM_BADCONFIG, "device not specified");
1713 return BUTM_BADCONFIG;
1716 config.tapeSize = file->capacity;
1717 config.fileMarkSize = file->fileMarkSize;
1718 config.portOffset = file->portOffset;
1719 strcpy(config.tapedir, file->device);
1721 /* Tape must be large enough to at least fit a label */
1722 if (config.tapeSize <= 0) {
1723 afs_com_err(whoami, BUTM_BADCONFIG, "Tape size bogus: %d Kbytes",
1725 return BUTM_BADCONFIG;
1728 if (strlen(config.tapedir) == 0) {
1729 afs_com_err(whoami, BUTM_BADCONFIG, "no tape device specified");
1730 return BUTM_BADCONFIG;
1737 /* This procedure instantiates a tape module of type file_tm. */
1739 butm_file_Instantiate(struct butm_tapeInfo *info, struct tapeConfig *file)
1741 extern int debugLevel;
1744 if (debugLevel > 98)
1745 printf("butm: Instantiate butc\n");
1748 ERROR_EXIT(BUTM_BADARGUMENT);
1749 if (info->structVersion != BUTM_MAJORVERSION)
1750 ERROR_EXIT(BUTM_OLDINTERFACE);
1752 memset(info, 0, sizeof(struct butm_tapeInfo));
1753 info->structVersion = BUTM_MAJORVERSION;
1754 info->ops.mount = file_Mount;
1755 info->ops.dismount = file_Dismount;
1756 info->ops.create = file_WriteLabel;
1757 info->ops.readLabel = file_ReadLabel;
1758 info->ops.seek = file_Seek;
1759 info->ops.seekEODump = file_SeekEODump;
1760 info->ops.readFileBegin = file_ReadFileBegin;
1761 info->ops.readFileData = file_ReadFileData;
1762 info->ops.readFileEnd = file_ReadFileEnd;
1763 info->ops.writeFileBegin = file_WriteFileBegin;
1764 info->ops.writeFileData = file_WriteFileData;
1765 info->ops.writeFileEnd = file_WriteFileEnd;
1766 info->ops.writeEOT = file_WriteEODump;
1767 info->ops.setSize = file_SetSize;
1768 info->ops.getSize = file_GetSize;
1769 info->debug = ((debugLevel > 98) ? 1 : 0);
1771 code = file_Configure(file);