-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdttape.c
More file actions
566 lines (501 loc) · 13.4 KB
/
dttape.c
File metadata and controls
566 lines (501 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#if defined(TAPE)
/****************************************************************************
* *
* COPYRIGHT (c) 1988 - 2017 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dttape.c
* Author: Robin T. Miller
* Date: August 20, 1993
*
* Description:
* This file contains the tape support functions.
*
* Modification History:
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#include "dt.h"
#if defined(_QNX_SOURCE)
# include <sys/qioctl.h>
#elif defined(SCO) || defined(AIX)
# include <sys/ioctl.h>
# include <sys/tape.h>
#else /* !defined(_QNX_SOURCE) && !defined(SCO) */
# include <sys/ioctl.h>
# include <sys/mtio.h>
#endif /* defined(_QNX_SOURCE) */
#if !defined(_QNX_SOURCE) && !defined(SCO) && !defined(AIX)
/************************************************************************
* *
* DoMtOp() Setup & Do a Magtape Operation. *
* *
* Inputs: dip = The device information. *
* cmd = The magtape command to issue. *
* count = The command count (if any). *
* msg = The error message for failures. *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
DoMtOp (dinfo_t *dip, short cmd, daddr_t count, caddr_t msgp)
{
struct mtop Mtop;
struct mtop *mtop = &Mtop;
mtop->mt_op = cmd;
/*
* Setup the 'mt' command count (if any).
*/
if ((mtop->mt_count = count) < 0) {
mtop->mt_count = 1;
}
if (dip->di_debug_flag) {
Printf(dip,
"Issuing '%s', count = %d (%#x) [file #%lu, record #%lu]\n",
msgp, mtop->mt_count, mtop->mt_count,
(dip->di_mode == READ_MODE) ?
(dip->di_files_read + 1) : (dip->di_files_written + 1),
(dip->di_mode == READ_MODE) ?
dip->di_records_read : dip->di_records_written);
}
return (DoIoctl (dip, MTIOCTOP, (caddr_t) mtop, msgp));
}
/************************************************************************
* *
* DoXXXXX() Setup & Do Specific Magtape Operations. *
* *
* Description: *
* These functions provide a simplistic interface for issuing *
* magtape commands from within the program. They all take 'count' *
* as an argument, except for those which do not take a count. *
* *
* Inputs: fd = The file descriptor. *
* count = The command count (if any). *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
DoForwardSpaceFile (dinfo_t *dip, daddr_t count)
{
short cmd = MTFSF;
return (DoMtOp (dip, cmd, count, "forward space file"));
}
int
DoBackwardSpaceFile (dinfo_t *dip, daddr_t count)
{
short cmd = MTBSF;
return (DoMtOp (dip, cmd, count, "backward space file"));
}
int
DoForwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
short cmd = MTFSR;
return (DoMtOp (dip, cmd, count, "forward space record"));
}
int
DoBackwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
short cmd = MTBSR;
return (DoMtOp (dip, cmd, count, "backward space record"));
}
int
DoRewindTape (dinfo_t *dip)
{
short cmd = MTREW;
return (DoMtOp (dip, cmd, (daddr_t) 0, "rewind tape"));
}
int
DoTapeOffline (dinfo_t *dip)
{
short cmd = MTOFFL;
return (DoMtOp (dip, cmd, (daddr_t) 0, "tape offline"));
}
#if !defined(OSFMK) && !defined(HP_UX) && !defined(AIX)
int
DoRetensionTape (dinfo_t *dip)
{
#if defined(FreeBSD) && !defined(MacDarwin)
short cmd = MTRETENS;
#else /* !defined(FreeBSD) || defined(MacDarwin) */
short cmd = MTRETEN;
#endif /* defined(FreeBSD) */
return (DoMtOp (dip, cmd, (daddr_t) 0, "retension tape"));
}
#endif /* !defined(OSFMK) && !defined(HP_UX) && !defined(AIX) */
#if defined(__osf__) /* Really DEC specific. */
int
DoSpaceEndOfData (dinfo_t *dip)
{
short cmd = MTSEOD;
return (DoMtOp (dip, cmd, (daddr_t) 0, "space to end of data"));
}
int
DoEraseTape (dinfo_t *dip)
{
short cmd = MTERASE;
return (DoMtOp (dip, cmd, (daddr_t) 0, "erase tape"));
}
int
DoTapeOnline (dinfo_t *dip)
{
short cmd = MTONLINE;
return (DoMtOp (dip, cmd, (daddr_t) 0, "tape online"));
}
int
DoLoadTape (dinfo_t *dip)
{
short cmd = MTLOAD;
return (DoMtOp (dip, cmd, (daddr_t) 0, "load tape"));
}
int
DoUnloadTape (dinfo_t *dip)
{
short cmd = MTUNLOAD;
return (DoMtOp (dip, cmd, (daddr_t) 0, "unload tape"));
}
#endif /* defined(__osf__) */
#if defined(sun)
int
DoSpaceEndOfData (dinfo_t *dip)
{
short cmd = MTEOM;
return (DoMtOp (dip, cmd, (daddr_t) 0, "space to end of data"));
}
#endif /* defined(sun) */
int
DoWriteFileMark (dinfo_t *dip, daddr_t count)
{
short cmd = MTWEOF;
return (DoMtOp (dip, cmd, count, "write file mark"));
}
/************************************************************************
* *
* DoIoctl() Do An I/O Control Command. *
* *
* Description: *
* This function issues the specified I/O control command to the *
* device driver. *
* *
* Inputs: dip = The device information. *
* cmd = The I/O control command. *
* argp = The command argument to pass. *
* msgp = The message to display on errors. *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
DoIoctl (dinfo_t *dip, int cmd, caddr_t argp, caddr_t msgp)
{
int status;
ENABLE_NOPROG(dip, IOCTL_OP);
status = ioctl (dip->di_fd, cmd, argp);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
dip->di_error = errno;
perror (msgp);
RecordErrorTimes(dip, True);
if ( (dip->di_trigger_control == TRIGGER_ON_ALL) ||
(dip->di_trigger_control == TRIGGER_ON_ERRORS) ) {
ExecuteTrigger(dip, msgp);
}
}
return (status);
}
#elif defined(AIX)
int
DoForwardSpaceFile (dinfo_t *dip, daddr_t count)
{
int cmd = STFSF;
return (DoMtOp (dip, cmd, count, "forward space file"));
}
int
DoBackwardSpaceFile (dinfo_t *dip, daddr_t count)
{
int cmd = STRSF;
return (DoMtOp (dip, cmd, count, "backward space file"));
}
int
DoForwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = STFSR;
return (DoMtOp (dip, cmd, count, "forward space record"));
}
int
DoBackwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = STRSR;
return (DoMtOp (dip, cmd, count, "backward space record"));
}
int
DoRewindTape (dinfo_t *dip)
{
int cmd = STREW;
return (DoMtOp (dip, cmd, 0, "rewind tape"));
}
int
DoRetensionTape (dinfo_t *dip)
{
int cmd = STRETEN;
return (DoMtOp (dip, cmd, 0, "retension tape"));
}
int
DoSpaceEndOfData (dinfo_t *dip)
{
errno = EINVAL;
return (FAILURE);
}
int
DoEraseTape (dinfo_t *dip)
{
int cmd = STERASE;
return (DoMtOp (dip, cmd, 0, "erase tape"));
}
int
DoWriteFileMark (dinfo_t *dip, daddr_t count)
{
int cmd = STWEOF;
return (DoMtOp (dip, cmd, count, "write file mark"));
}
/************************************************************************
* *
* DoMtOp() Setup & Do a Magtape Operation. *
* *
* Inputs: dip = The device information. *
* cmd = The magtape command to issue. *
* count = The command count (if any). *
* msg = The error message for failures. *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
DoMtOp (dinfo_t *dip, short cmd, daddr_t count, caddr_t msgp)
{
struct stop Stop;
struct stop *stop = &Stop;
int status;
stop->st_op = cmd;
/*
* Setup the 'mt' command count (if any).
*/
if ((stop->st_count = count) < 0) {
stop->st_count = 1;
}
if (dip->di_debug_flag) {
Printf(dip,
"Issuing '%s', count = %d (%#x) [file #%lu, record #%lu]\n",
msgp, stop->st_count, stop->st_count,
(dip->di_mode == READ_MODE) ?
(dip->di_files_read + 1) : (dip->di_files_written + 1),
(dip->di_mode == READ_MODE) ?
dip->di_records_read : dip->di_records_written);
}
ENABLE_NOPROG(dip, IOCTL_OP);
status = ioctl (dip->di_fd, STIOCTOP, stop);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
dip->di_error = errno;
perror (msgp);
RecordErrorTimes(dip, True);
if ( (dip->di_trigger_control == TRIGGER_ON_ALL) ||
(dip->di_trigger_control == TRIGGER_ON_ERRORS) ) {
ExecuteTrigger(dip, msgp);
}
}
return (status);
}
#elif defined(_QNX_SOURCE)
/************************************************************************
* *
* DoXXXXX() Setup & Do Specific Magtape Operations. *
* *
* Description: *
* These functions provide a simplistic interface for issuing *
* magtape commands from within the program. They all take 'count' *
* as an argument, except for those which do not take a count. *
* *
* Inputs: dip = The device information. *
* count = The command count (if any). *
* *
* Return Value: *
* Returns 0 / -1 = SUCCESS / FAILURE. *
* *
************************************************************************/
int
DoForwardSpaceFile (dinfo_t *dip, daddr_t count)
{
int cmd = T_SEEK_FM;
return (DoIoctl (dip, cmd, count, "forward space file"));
}
int
DoForwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = T_SKIP_FWD_A_BLOCK;
return (DoIoctl (dip, cmd, count, "forward space record"));
}
int
DoBackwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = T_SKIP_BWD_A_BLOCK;
return (DoIoctl (dip, cmd, count, "backward space record"));
}
int
DoRewindTape (dinfo_t *dip)
{
int cmd = T_BOT;
return (DoIoctl (dip, cmd, 0, "rewind tape"));
}
int
DoRetensionTape (dinfo_t *dip)
{
int cmd = T_RETENSION;
return (DoIoctl (dip, cmd, 0, "retension tape"));
}
int
DoSpaceEndOfData (dinfo_t *dip)
{
int cmd = T_SEEK_EOD;
return (DoIoctl (dip, cmd, 0, "space to end of data"));
}
int
DoEraseTape (dinfo_t *dip)
{
int cmd = T_ERASE;
return (DoIoctl (dip, cmd, 0, "erase tape"));
}
int
DoWriteFileMark (dinfo_t *dip, daddr_t count)
{
int cmd = T_WRITE_FM;
return (DoIoctl (dip, cmd, count, "write file mark"));
}
int
DoIoctl (dinfo_t *dip, int cmd, int count, caddr_t msgp)
{
QIC02_MSG_STRUCT qic02ms;
int status;
if (dip->di_debug_flag) {
Printf (dip, "Issuing '%s', count = %d (%#x)\n",
msgp, count, count);
}
memset(&qic02ms, '\0', sizeof(qic02ms));
qic02ms.Header.Command = (unsigned short)cmd;
while (count--) {
ENABLE_NOPROG(dip, IOCTL_OP);
status = qnx_ioctl (dip->di_fd,
QCTL_RAW_CMD,
&qic02ms, sizeof (QIC02_HEADER_STRUCT),
&qic02ms, sizeof (QIC02_MSG_STRUCT));
DISABLE_NOPROG(dip);
if (status == FAILURE) {
perror (msgp);
RecordErrorTimes(dip, True);
if (dip->di_error_count >= dip->di_error_limit) {
return(FAILURE);
}
}
}
return(status);
}
#elif defined(SCO)
int
DoForwardSpaceFile (dinfo_t *dip, daddr_t count)
{
int cmd = T_SFF;
return (DoIoctl (dip, cmd, count, "forward space file"));
}
int
DoBackwardSpaceFile (dinfo_t *dip, daddr_t count)
{
int cmd = T_SFB;
return (DoIoctl (dip, cmd, count, "backward space file"));
}
int
DoForwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = T_SBF;
return (DoIoctl (dip, cmd, count, "forward space record"));
}
int
DoBackwardSpaceRecord (dinfo_t *dip, daddr_t count)
{
int cmd = T_SBB;
return (DoIoctl (dip, cmd, count, "backward space record"));
}
int
DoRewindTape (dinfo_t *dip)
{
int cmd = T_RWD;
return (DoIoctl (dip, cmd, 0, "rewind tape"));
}
int
DoRetensionTape (dinfo_t *dip)
{
int cmd = T_RETENSION;
return (DoIoctl (dip, cmd, 0, "retension tape"));
}
int
DoSpaceEndOfData (dinfo_t *dip)
{
int cmd = T_EOD;
return (DoIoctl (dip, cmd, 0, "space to end of data"));
}
int
DoEraseTape (dinfo_t *dip)
{
int cmd = T_ERASE;
return (DoIoctl (dip, cmd, 0, "erase tape"));
}
int
DoWriteFileMark (dinfo_t *dip, daddr_t count)
{
int cmd = T_WRFILEM;
return (DoIoctl (dip, cmd, count, "write file mark"));
}
int
DoIoctl (dinfo_t *dip, int cmd, int count, caddr_t msgp)
{
int status;
if (dip->di_debug_flag) {
Printf (dip, "Issuing '%s', count = %d (%#x)\n",
msgp, count, count);
}
ENABLE_NOPROG(dip, IOCTL_OP);
status = ioctl (dip->di_fd, cmd, count);
DISABLE_NOPROG(dip);
if (status == FAILURE) {
perror (msgp);
RecordErrorTimes(dip, True);
}
return(status);
}
#endif /* defined(SCO) */
#endif /* defined(TAPE) */