Standardize License information
[openafs.git] / src / bu_utils / fms.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>                          /* for machine/host info */
11 #undef  IN
12 #include <stdio.h>
13 #include <errno.h>
14 #include <sys/types.h>                          /* for mtio.h */
15 #include <afs/cmd.h>
16 #include <afs/procmgmt.h>
17 #include <afs/usd.h>
18
19 /* structure for writing data to tape */
20 typedef struct tapeDataBuffer
21 {
22     struct tapeDataBuffer       *tdb_next;
23     char                        *tdb_buffer;
24 } tapeDataBufferT;
25 typedef tapeDataBufferT *tapeDataBufferP;
26
27 /* globals */
28 char *tapeDevice = 0;                           /* device pathname */
29 afs_int32 eotEnabled = 1;
30
31 /* prototypes */
32 int fileMark(usd_handle_t hTape);
33 int fileMarkSize(char *tapeDevice);
34 void tt_fileMarkSize(struct cmd_syndesc *as, char *arock);
35
36 #define ERROR(evalue)                                           \
37         {                                                       \
38             code = evalue;                                      \
39             goto error_exit;                                    \
40         }
41
42 #define MAXV    100
43
44 #ifndef AFS_NT40_ENV
45 #include "AFS_component_version_number.c"
46 #endif
47
48 void quitFms(int);
49
50 main(argc, argv)
51      int    argc;
52      char **argv;
53 {
54     struct sigaction intaction, oldaction;
55     struct cmd_syndesc *cptr;
56
57     bzero((char *)&intaction, sizeof(intaction));
58     intaction.sa_handler = (int (*)())quitFms;
59
60     sigaction(SIGINT, &intaction, &oldaction);
61
62     cptr = cmd_CreateSyntax((char *) 0, tt_fileMarkSize, 0, 
63                             "write a tape full of file marks");
64     cmd_AddParm(cptr, "-tape", CMD_SINGLE, CMD_REQUIRED, "tape special file");
65
66     cmd_Dispatch(argc, argv);
67 }
68
69 void tt_fileMarkSize(as, arock)
70      struct cmd_syndesc *as;
71      char *arock;
72 {
73     char *tapeDevice;
74
75     tapeDevice = as->parms[0].items->data;
76
77     fileMarkSize(tapeDevice);
78 }
79
80
81 fileMarkSize(tapeDevice)
82      char *tapeDevice;
83 {
84     afs_uint32 nFileMarks, nBlocks, nbfTape;
85     double  tpSize, fmSize;
86     afs_uint32 bufferSize = 16384;
87     usd_handle_t hTape;
88     FILE *logFile;
89     int count = 0;
90     afs_uint32 countr;
91     afs_int32 code = 0;
92
93     afs_int32 rewindTape();
94
95     code = usd_Open(tapeDevice,
96                      (USD_OPEN_RDWR | USD_OPEN_WLOCK), 0777, &hTape);
97     if ( code )
98     {
99         printf("Can't open tape device %s\n", tapeDevice);
100         fflush(stdout);
101         exit(1);
102     }
103
104     logFile = fopen("fms.log", "w+");
105     if ( logFile == NULL )
106     {
107         printf("Can't open log file\n");
108         fflush(stdout);
109         exit(1);
110     }
111     fprintf(logFile, "fms test started\n");
112     fflush(logFile);
113
114     code = rewindTape(hTape);
115     if ( code )
116     {
117         fprintf(logFile, "Can't rewind tape\n");
118         fflush(logFile);
119         ERROR(code);
120     }
121
122     /* measure capacity of tape */
123     nbfTape = 0;
124     countr  = 0;
125     while ( 1 )
126     {
127         code = dataBlock(hTape, bufferSize);
128         nbfTape++;
129         count++;
130         countr++;
131         if ( code )
132             break;
133
134         if ( count >= 5 ) 
135         {
136             count = 0;
137             printf("\rwrote block: %d", nbfTape);
138         }
139
140     }
141
142     fprintf(logFile, "wrote %d blocks\n", nbfTape);
143     fflush(logFile);
144     printf("\rwrote %d blocks\n", nbfTape);
145     printf("Finished data capacity test - rewinding\n");
146     /* reset the tape device */
147     code = USD_CLOSE(hTape);
148     if (code) 
149     {
150         fprintf(logFile, "Can't close tape device at end of pass 1\n");
151         fflush(logFile);
152         printf("Can't close tape device %s\n", tapeDevice);
153         goto error_exit;
154     }
155     code = usd_Open(tapeDevice,
156                     (USD_OPEN_RDWR | USD_OPEN_WLOCK), 0777, &hTape);
157     if ( code )
158     {
159         fprintf(logFile, "Can't open tape device for pass 2\n");
160         fflush(logFile);
161         printf("Can't open tape device %s\n", tapeDevice);
162         goto error_exit;
163     }
164
165     code = rewindTape(hTape);
166     if ( code )
167     {
168         fprintf(logFile, "Can't rewind tape\n");
169         fflush(logFile);
170         ERROR(code);
171     }
172
173     /* now measure file mark size */
174     nFileMarks = 0;
175     nBlocks    = 0;
176     count      = 0;
177     countr     = 0;
178     while ( 1 )
179     {
180         code = dataBlock(hTape, bufferSize);
181         nBlocks++;
182         if ( code )
183             break;
184         code = fileMark(hTape);
185         nFileMarks++;
186         if ( code )
187             break;
188         count++;
189         countr++;
190
191         if ( count >= 2 )
192         {
193             count = 0;
194             printf("\rwrote %d blocks, %d filemarks", 
195                    nBlocks, nFileMarks);
196         }
197
198     }
199     printf("\nFinished filemark test\n");
200     tpSize = (double)nbfTape*(double)bufferSize;
201     fmSize = (((double)nbfTape - (double)nBlocks)*(double)bufferSize)/(double)nFileMarks;
202     printf("Tape capacity is %.0f bytes\n", tpSize);
203     printf("File marks are %.0f bytes\n",   fmSize);
204     fprintf(logFile, "Tape capacity is %.0f bytes\n", tpSize);
205     fprintf(logFile, "File marks are %.0f bytes\n",   fmSize);
206     fflush(logFile);
207     fclose(logFile);
208 error_exit:
209     USD_CLOSE(hTape);
210     return(code);
211 }
212
213 void quitFms(int sig)
214 {
215     exit(0);
216 }
217
218
219 /* --------------------------
220  * device handling routines
221  * --------------------------
222  */
223
224 /* rewindTape() - rewinds tape to beginning */
225 afs_int32
226 rewindTape(usd_handle_t hTape)
227 {
228   usd_tapeop_t tapeop;
229   int rcode;
230
231   tapeop.tp_op = USDTAPE_REW;
232   tapeop.tp_count = 1;
233   rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
234   return rcode;
235 }
236
237 /* write an EOF marker */
238 int fileMark(usd_handle_t hTape)
239 {
240   usd_tapeop_t tapeop;
241   int rcode;
242
243   tapeop.tp_op = USDTAPE_WEOF;
244   tapeop.tp_count = 1;
245   rcode = USD_IOCTL(hTape, USD_IOCTL_TAPEOPERATION, (void *)&tapeop);
246   return rcode;
247 }
248
249 /* dataBlock
250  *      write a block of data on tape
251  * entry:
252  *      blocksize - size of block in bytes
253  */
254
255 dataBlock(usd_handle_t hTape, afs_int32 reqSize)
256 {
257     static char *dB_buffer = 0;
258     static afs_int32 dB_buffersize = 0;
259     static int dB_count = 0;
260     int *ptr;
261     afs_int32 code = 0, xferd;
262
263     /* dbBuffersize is only valid when dB_buffer is non-zero */
264
265     if ( (dB_buffer != 0)
266     &&   (dB_buffersize != reqSize )
267        )
268     {
269         free(dB_buffer);
270         dB_buffer = 0;
271     }
272
273     if (dB_buffer == 0 )
274     {
275         dB_buffer = (char *) malloc(reqSize);
276         if ( dB_buffer == 0 )
277             ERROR(-1);
278         dB_buffersize = reqSize;
279         bzero(dB_buffer, dB_buffersize);
280     }
281
282     ptr = (int *) dB_buffer;
283     *ptr = dB_count++;
284
285     code = USD_WRITE(hTape, dB_buffer, dB_buffersize, &xferd);
286     if (code || xferd != dB_buffersize)
287         ERROR(-1);
288
289 error_exit:
290     return(code);
291 }
292
293
294
295
296
297
298