/
generic_tape_device.cc
1494 lines (1344 loc) · 36.8 KB
/
generic_tape_device.cc
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2014-2014 Planets Communications B.V.
Copyright (C) 2014-2014 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, MM
*
* Extracted from other source files Marco van Wieringen, June 2014
*/
/**
* @file
* Generic Tape API device abstraction.
*/
#include "include/bareos.h"
#include "stored/stored.h"
#include "generic_tape_device.h"
#include "stored/autochanger.h"
#include "lib/scsi_lli.h"
namespace storagedaemon {
/**
* Open a tape device
*/
void generic_tape_device::OpenDevice(DeviceControlRecord *dcr, int omode)
{
file_size = 0;
int timeout = max_open_wait;
#if !defined(HAVE_WIN32)
struct mtop mt_com;
utime_t start_time = time(NULL);
#endif
mount(dcr, 1); /* do mount if required */
Dmsg0(100, "Open dev: device is tape\n");
GetAutochangerLoadedSlot(dcr);
open_mode = omode;
set_mode(omode);
if (timeout < 1) {
timeout = 1;
}
errno = 0;
Dmsg2(100, "Try open %s mode=%s\n", prt_name, mode_to_str(omode));
#if defined(HAVE_WIN32)
/*
* Windows Code
*/
if ((fd_ = d_open(dev_name, oflags, 0)) < 0) {
dev_errno = errno;
}
#else
/*
* UNIX Code
*
* If busy retry each second for max_open_wait seconds
*/
for ( ;; ) {
/*
* Try non-blocking open
*/
fd_ = d_open(dev_name, oflags | O_NONBLOCK, 0);
if (fd_ < 0) {
BErrNo be;
dev_errno = errno;
Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n",
prt_name, omode, oflags, errno, be.bstrerror());
} else {
/*
* Tape open, now rewind it
*/
Dmsg0(100, "Rewind after open\n");
mt_com.mt_op = MTREW;
mt_com.mt_count = 1;
/*
* Rewind only if dev is a tape
*/
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
BErrNo be;
dev_errno = errno; /* set error status from rewind */
d_close(fd_);
ClearOpened();
Dmsg2(100, "Rewind error on %s close: ERR=%s\n", prt_name, be.bstrerror(dev_errno));
/*
* If we get busy, device is probably rewinding, try again
*/
if (dev_errno != EBUSY) {
break; /* error -- no medium */
}
} else {
/*
* Got fd and rewind worked, so we must have medium in drive
*/
d_close(fd_);
fd_ = d_open(dev_name, oflags, 0); /* open normally */
if (fd_ < 0) {
BErrNo be;
dev_errno = errno;
Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n",
prt_name, omode, oflags, errno, be.bstrerror());
break;
}
dev_errno = 0;
LockDoor();
SetOsDeviceParameters(dcr); /* do system dependent stuff */
break; /* Successfully opened and rewound */
}
}
Bmicrosleep(5, 0);
/*
* Exceed wait time ?
*/
if (time(NULL) - start_time >= max_open_wait) {
break; /* yes, get out */
}
}
#endif
if (!IsOpen()) {
BErrNo be;
Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"),
prt_name, be.bstrerror(dev_errno));
Dmsg1(100, "%s", errmsg);
}
Dmsg1(100, "open dev: tape %d opened\n", fd_);
}
/**
* Position device to end of medium (end of data)
*
* Returns: true on succes
* false on error
*/
bool generic_tape_device::eod(DeviceControlRecord *dcr)
{
struct mtop mt_com;
bool ok = true;
int32_t os_file;
if (fd_ < 0) {
dev_errno = EBADF;
Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), prt_name);
return false;
}
#if defined (__digital__) && defined (__unix__)
return fsf(VolCatInfo.VolCatFiles);
#endif
Dmsg0(100, "Enter eod\n");
if (AtEot()) {
return true;
}
ClearEof(); /* remove EOF flag */
block_num = file = 0;
file_size = 0;
file_addr = 0;
#ifdef MTEOM
if (HasCap(CAP_FASTFSF) && !HasCap(CAP_EOM)) {
Dmsg0(100,"Using FAST FSF for EOM\n");
/*
* If unknown position, rewind
*/
if (GetOsTapeFile() < 0) {
if (!rewind(NULL)) {
Dmsg0(100, "Rewind error\n");
return false;
}
}
mt_com.mt_op = MTFSF;
/*
* ***FIXME*** fix code to handle case that INT16_MAX is not large enough.
*/
mt_com.mt_count = INT16_MAX; /* use big positive number */
if (mt_com.mt_count < 0) {
mt_com.mt_count = INT16_MAX; /* brain damaged system */
}
}
if (HasCap(CAP_MTIOCGET) && (HasCap(CAP_FASTFSF) || HasCap(CAP_EOM))) {
if (HasCap(CAP_EOM)) {
Dmsg0(100,"Using EOM for EOM\n");
mt_com.mt_op = MTEOM;
mt_com.mt_count = 1;
}
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
BErrNo be;
clrerror(mt_com.mt_op);
Dmsg1(50, "ioctl error: %s\n", be.bstrerror());
UpdatePos(dcr);
Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
Dmsg0(100, errmsg);
return false;
}
os_file = GetOsTapeFile();
if (os_file < 0) {
BErrNo be;
clrerror(-1);
Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
Dmsg0(100, errmsg);
return false;
}
Dmsg1(100, "EOD file=%d\n", os_file);
SetAteof();
file = os_file;
} else {
#endif
/*
* Rewind then use FSF until EOT reached
*/
if (!rewind(NULL)) {
Dmsg0(100, "Rewind error.\n");
return false;
}
/*
* Move file by file to the end of the tape
*/
int file_num;
for (file_num=file; !AtEot(); file_num++) {
Dmsg0(200, "eod: doing fsf 1\n");
if (!fsf(1)) {
Dmsg0(100, "fsf error.\n");
return false;
}
/*
* Avoid infinite loop by ensuring we advance.
*/
if (!AtEot() && file_num == (int)file) {
Dmsg1(100, "fsf did not advance from file %d\n", file_num);
SetAteof();
os_file = GetOsTapeFile();
if (os_file >= 0) {
Dmsg2(100, "Adjust file from %d to %d\n", file_num, os_file);
file = os_file;
}
break;
}
}
#ifdef MTEOM
}
#endif
/*
* Some drivers leave us after second EOF when doing MTEOM, so we must backup
* so that appending overwrites the second EOF.
*/
if (HasCap(CAP_BSFATEOM)) {
/*
* Backup over EOF
*/
ok = bsf(1);
/*
* If BSF worked and fileno is known (not -1), set file
*/
os_file = GetOsTapeFile();
if (os_file >= 0) {
Dmsg2(100, "BSFATEOF adjust file from %d to %d\n", file , os_file);
file = os_file;
} else {
file++; /* wing it -- not correct on all OSes */
}
} else {
UpdatePos(dcr); /* update position */
}
Dmsg1(200, "EOD dev->file=%d\n", file);
return ok;
}
/**
* Called to indicate that we have just read an EOF from the device.
*/
void generic_tape_device::SetAteof()
{
SetEof();
file++;
file_addr = 0;
file_size = 0;
block_num = 0;
}
/**
* Called to indicate we are now at the end of the volume, and writing is not possible.
*/
void generic_tape_device::SetAteot()
{
/*
* Make volume effectively read-only
*/
SetBit(ST_EOF, state);
SetBit(ST_EOT, state);
SetBit(ST_WEOT, state);
ClearAppend();
}
/**
* Rewind device and put it offline
*
* Returns: true on success
* false on failure
*/
bool generic_tape_device::offline()
{
struct mtop mt_com;
/*
* Remove EOF/EOT flags.
*/
ClearBit(ST_APPENDREADY, state);
ClearBit(ST_READREADY, state);
ClearBit(ST_EOT, state);
ClearBit(ST_EOF, state);
ClearBit(ST_WEOT, state);
block_num = file = 0;
file_size = 0;
file_addr = 0;
UnlockDoor();
mt_com.mt_op = MTOFFL;
mt_com.mt_count = 1;
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
return false;
}
Dmsg1(100, "Offlined device %s\n", prt_name);
return true;
}
/**
* Write an end of file on the device
*
* Returns: true on success
* false on failure
*/
bool generic_tape_device::weof(int num)
{
struct mtop mt_com;
int status;
Dmsg1(129, "=== weof_dev=%s\n", prt_name);
if (!IsOpen()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to weof_dev. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
file_size = 0;
if (!CanAppend()) {
Mmsg0(errmsg, _("Attempt to WEOF on non-appendable Volume\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
ClearEof();
ClearEot();
mt_com.mt_op = MTWEOF;
mt_com.mt_count = num;
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status == 0) {
block_num = 0;
file += num;
file_addr = 0;
} else {
BErrNo be;
clrerror(mt_com.mt_op);
if (status == -1) {
Mmsg2(errmsg, _("ioctl MTWEOF error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
}
}
return status == 0;
}
/**
* Foward space a file
*
* Returns: true on success
* false on failure
*/
bool generic_tape_device::fsf(int num)
{
int32_t os_file = 0;
struct mtop mt_com;
int status = 0;
if (!IsOpen()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to fsf. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
if (AtEot()) {
dev_errno = 0;
Mmsg1(errmsg, _("Device %s at End of Tape.\n"), prt_name);
return false;
}
if (AtEof()) {
Dmsg0(200, "ST_EOF set on entry to FSF\n");
}
Dmsg0(100, "fsf\n");
block_num = 0;
/*
* If Fast forward space file is set, then we
* use MTFSF to forward space and MTIOCGET
* to get the file position. We assume that
* the SCSI driver will ensure that we do not
* forward space past the end of the medium.
*/
if (HasCap(CAP_FSF) && HasCap(CAP_MTIOCGET) && HasCap(CAP_FASTFSF)) {
int my_errno = 0;
mt_com.mt_op = MTFSF;
mt_com.mt_count = num;
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status < 0) {
my_errno = errno; /* save errno */
} else if ((os_file=GetOsTapeFile()) < 0) {
my_errno = errno; /* save errno */
}
if (my_errno != 0) {
BErrNo be;
SetEot();
Dmsg0(200, "Set ST_EOT\n");
clrerror(mt_com.mt_op);
Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"), prt_name, be.bstrerror(my_errno));
Dmsg1(200, "%s", errmsg);
return false;
}
Dmsg1(200, "fsf file=%d\n", os_file);
SetAteof();
file = os_file;
return true;
/*
* Here if CAP_FSF is set, and virtually all drives
* these days support it, we read a record, then forward
* space one file. Using this procedure, which is slow,
* is the only way we can be sure that we don't read
* two consecutive EOF marks, which means End of Data.
*/
} else if (HasCap(CAP_FSF)) {
POOLMEM *rbuf;
int rbuf_len;
Dmsg0(200, "FSF has cap_fsf\n");
if (max_block_size == 0) {
rbuf_len = DEFAULT_BLOCK_SIZE;
} else {
rbuf_len = max_block_size;
}
rbuf = GetMemory(rbuf_len);
mt_com.mt_op = MTFSF;
mt_com.mt_count = 1;
while (num-- && !AtEot()) {
Dmsg0(100, "Doing read before fsf\n");
if ((status = this->read((char *)rbuf, rbuf_len)) < 0) {
if (errno == ENOMEM) { /* tape record exceeds buf len */
status = rbuf_len; /* This is OK */
/*
* On IBM drives, they return ENOSPC at EOM instead of EOF status
*/
} else if (AtEof() && errno == ENOSPC) {
status = 0;
} else if (HasCap(CAP_IOERRATEOM) && AtEof() && errno == EIO) {
if (HasCap(CAP_IBMLINTAPE)) {
Dmsg0(100, "Got EIO on read, checking lin_tape sense data\n");
if (CheckScsiAtEod(fd_)) {
Dmsg0(100, "Sense data confirms it's EOD\n");
status = 0;
} else {
Dmsg0(100, "Not at EOD, might be a real error. Check sense trace from lin_taped logs.\n");
SetEot();
clrerror(-1);
Mmsg1(errmsg, _("read error on %s. ERR=Input/Output error.\n"), prt_name);
break;
}
} else {
Dmsg0(100, "Got EIO on read, assuming that's due to EOD\n");
status = 0;
}
} else {
BErrNo be;
SetEot();
clrerror(-1);
Dmsg2(100, "Set ST_EOT read errno=%d. ERR=%s\n", dev_errno, be.bstrerror());
Mmsg2(errmsg, _("read error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
Dmsg1(100, "%s", errmsg);
break;
}
}
if (status == 0) { /* EOF */
Dmsg1(100, "End of File mark from read. File=%d\n", file+1);
/*
* Two reads of zero means end of tape
*/
if (AtEof()) {
SetEot();
Dmsg0(100, "Set ST_EOT\n");
break;
} else {
SetAteof();
continue;
}
} else { /* Got data */
ClearEot();
ClearEof();
}
Dmsg0(100, "Doing MTFSF\n");
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status < 0) { /* error => EOT */
BErrNo be;
SetEot();
Dmsg0(100, "Set ST_EOT\n");
clrerror(mt_com.mt_op);
Mmsg2(errmsg, _("ioctl MTFSF error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
Dmsg0(100, "Got < 0 for MTFSF\n");
Dmsg1(100, "%s", errmsg);
} else {
SetAteof();
}
}
FreeMemory(rbuf);
/*
* No FSF, so use FSR to simulate it
*/
} else {
Dmsg0(200, "Doing FSR for FSF\n");
while (num-- && !AtEot()) {
fsr(INT32_MAX); /* returns -1 on EOF or EOT */
}
if (AtEot()) {
dev_errno = 0;
Mmsg1(errmsg, _("Device %s at End of Tape.\n"), prt_name);
status = -1;
} else {
status = 0;
}
}
Dmsg1(200, "Return %d from FSF\n", status);
if (AtEof()) {
Dmsg0(200, "ST_EOF set on exit FSF\n");
}
if (AtEot()) {
Dmsg0(200, "ST_EOT set on exit FSF\n");
}
Dmsg1(200, "Return from FSF file=%d\n", file);
return status == 0;
}
/**
* Backward space a file
*
* Returns: false on failure
* true on success
*/
bool generic_tape_device::bsf(int num)
{
struct mtop mt_com;
int status;
if (!IsOpen()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to bsf. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
Dmsg0(100, "bsf\n");
ClearEot();
ClearEof();
file -= num;
file_addr = 0;
file_size = 0;
mt_com.mt_op = MTBSF;
mt_com.mt_count = num;
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status < 0) {
BErrNo be;
clrerror(mt_com.mt_op);
Mmsg2(errmsg, _("ioctl MTBSF error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
}
return status == 0;
}
static inline bool DevGetOsPos(Device *dev, struct mtget *mt_stat)
{
Dmsg0(100, "DevGetOsPos\n");
return dev->HasCap(CAP_MTIOCGET) &&
dev->d_ioctl(dev->fd(), MTIOCGET, (char *)mt_stat) == 0 &&
mt_stat->mt_fileno >= 0;
}
/**
* Foward space num records
*
* Returns: false on failure
* true on success
*/
bool generic_tape_device::fsr(int num)
{
struct mtop mt_com;
int status;
if (!IsOpen()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to fsr. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
if (!HasCap(CAP_FSR)) {
Mmsg1(errmsg, _("ioctl MTFSR not permitted on %s.\n"), prt_name);
return false;
}
Dmsg1(100, "fsr %d\n", num);
mt_com.mt_op = MTFSR;
mt_com.mt_count = num;
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status == 0) {
ClearEof();
block_num += num;
} else {
BErrNo be;
struct mtget mt_stat;
clrerror(mt_com.mt_op);
Dmsg1(100, "FSF fail: ERR=%s\n", be.bstrerror());
if (DevGetOsPos(this, &mt_stat)) {
Dmsg4(100, "Adjust from %d:%d to %d:%d\n", file,
block_num, mt_stat.mt_fileno, mt_stat.mt_blkno);
file = mt_stat.mt_fileno;
block_num = mt_stat.mt_blkno;
} else {
if (AtEof()) {
SetEot();
} else {
SetAteof();
}
}
Mmsg3(errmsg, _("ioctl MTFSR %d error on %s. ERR=%s.\n"), num, prt_name, be.bstrerror());
}
return status == 0;
}
/**
* Backward space a record
*
* Returns: false on failure
* true on success
*/
bool generic_tape_device::bsr(int num)
{
struct mtop mt_com;
int status;
if (!IsOpen()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to bsr_dev. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
if (!HasCap(CAP_BSR)) {
Mmsg1(errmsg, _("ioctl MTBSR not permitted on %s.\n"), prt_name);
return false;
}
Dmsg0(100, "bsr_dev\n");
block_num -= num;
ClearEof();
ClearEot();
mt_com.mt_op = MTBSR;
mt_com.mt_count = num;
status = d_ioctl(fd_, MTIOCTOP, (char *)&mt_com);
if (status < 0) {
BErrNo be;
clrerror(mt_com.mt_op);
Mmsg2(errmsg, _("ioctl MTBSR error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
}
return status == 0;
}
/**
* Load medium in device
*
* Returns: true on success
* false on failure
*/
bool generic_tape_device::LoadDev()
{
#ifdef MTLOAD
struct mtop mt_com;
#endif
if (fd_ < 0) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad call to LoadDev. Device not open\n"));
Emsg0(M_FATAL, 0, errmsg);
return false;
}
#ifndef MTLOAD
Dmsg0(200, "stored: MTLOAD command not available\n");
BErrNo be;
dev_errno = ENOTTY; /* function not available */
Mmsg2(errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
return false;
#else
block_num = file = 0;
file_size = 0;
file_addr = 0;
mt_com.mt_op = MTLOAD;
mt_com.mt_count = 1;
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
BErrNo be;
dev_errno = errno;
Mmsg2(errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"), prt_name, be.bstrerror());
return false;
}
return true;
#endif
}
void generic_tape_device::LockDoor()
{
#ifdef MTLOCK
struct mtop mt_com;
mt_com.mt_op = MTLOCK;
mt_com.mt_count = 1;
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
clrerror(mt_com.mt_op);
}
#endif
}
void generic_tape_device::UnlockDoor()
{
#ifdef MTUNLOCK
struct mtop mt_com;
mt_com.mt_op = MTUNLOCK;
mt_com.mt_count = 1;
if (d_ioctl(fd_, MTIOCTOP, (char *)&mt_com) < 0) {
clrerror(mt_com.mt_op);
}
#endif
}
#if defined(MTIOCLRERR)
/**
* Found on Solaris
*/
static inline void OsClrerror(Device *dev)
{
if (dev->d_ioctl(dev->fd(), MTIOCLRERR) < 0) {
dev->clrerror(MTIOCLRERR);
}
Dmsg0(200, "Did MTIOCLRERR\n");
}
#elif defined(MTIOCERRSTAT)
/**
* Typically on FreeBSD
*/
static inline void OsClrerror(Device *dev)
{
BErrNo be;
union mterrstat mt_errstat;
/*
* Read and clear SCSI error status
*/
Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev->dev_errno, be.bstrerror(dev->dev_errno));
if (dev->d_ioctl(dev->fd(), MTIOCERRSTAT, (char *)&mt_errstat) < 0) {
dev->clrerror(MTIOCERRSTAT);
}
}
#elif defined(MTCSE)
/**
* Clear Subsystem Exception TRU64
*/
static inline void OsClrerror(Device *dev)
{
struct mtop mt_com;
/*
* Clear any error condition on the tape
*/
mt_com.mt_op = MTCSE;
mt_com.mt_count = 1;
if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
dev->clrerror(mt_com.mt_op);
}
Dmsg0(200, "Did MTCSE\n");
}
#else
static inline void OsClrerror(Device *dev)
{
}
#endif
/**
* If implemented in system, clear the tape error status.
*/
void generic_tape_device::clrerror(int func)
{
const char *msg = NULL;
char buf[100];
dev_errno = errno; /* save errno */
if (errno == EIO) {
VolCatInfo.VolCatErrors++;
}
if (errno == ENOTTY || errno == ENOSYS) { /* Function not implemented */
switch (func) {
case -1:
break; /* ignore message printed later */
case MTWEOF:
msg = "WTWEOF";
ClearCap(CAP_EOF); /* turn off feature */
break;
#ifdef MTEOM
case MTEOM:
msg = "WTEOM";
ClearCap(CAP_EOM); /* turn off feature */
break;
#endif
case MTFSF:
msg = "MTFSF";
ClearCap(CAP_FSF); /* turn off feature */
break;
case MTBSF:
msg = "MTBSF";
ClearCap(CAP_BSF); /* turn off feature */
break;
case MTFSR:
msg = "MTFSR";
ClearCap(CAP_FSR); /* turn off feature */
break;
case MTBSR:
msg = "MTBSR";
ClearCap(CAP_BSR); /* turn off feature */
break;
case MTREW:
msg = "MTREW";
break;
#ifdef MTSETBLK
case MTSETBLK:
msg = "MTSETBLK";
break;
#endif
#ifdef MTSETDRVBUFFER
case MTSETDRVBUFFER:
msg = "MTSETDRVBUFFER";
break;
#endif
#ifdef MTRESET
case MTRESET:
msg = "MTRESET";
break;
#endif
#ifdef MTSETBSIZ
case MTSETBSIZ:
msg = "MTSETBSIZ";
break;
#endif
#ifdef MTSRSZ
case MTSRSZ:
msg = "MTSRSZ";
break;
#endif
#ifdef MTLOAD
case MTLOAD:
msg = "MTLOAD";
break;
#endif
#ifdef MTLOCK
case MTLOCK:
msg = "MTLOCK";
break;
#endif
#ifdef MTUNLOCK
case MTUNLOCK:
msg = "MTUNLOCK";
break;
#endif
case MTOFFL:
msg = "MTOFFL";
break;
#ifdef MTIOCLRERR
case MTIOCLRERR:
msg = "MTIOCLRERR";
break;
#endif
#ifdef MTIOCERRSTAT
case MTIOCERRSTAT:
msg = "MTIOCERRSTAT";
break;
#endif
#ifdef MTCSE
case MTCSE:
msg = "MTCSE";
break;
#endif
default:
Bsnprintf(buf, sizeof(buf), _("unknown func code %d"), func);
msg = buf;
break;
}
if (msg != NULL) {
dev_errno = ENOSYS;
Mmsg1(errmsg, _("I/O function \"%s\" not supported on this device.\n"), msg);
Emsg0(M_ERROR, 0, errmsg);
}
}
/*
* Now we try different methods of clearing the error status on the drive
* so that it is not locked for further operations.
*/
/*
* On some systems such as NetBSD, this clears all errors
*/
GetOsTapeFile();
/*
* OS specific clear function.
*/
OsClrerror(this);
}
void generic_tape_device::SetOsDeviceParameters(DeviceControlRecord *dcr)
{
Device *dev = dcr->dev;
if (bstrcmp(dev->dev_name, "/dev/null")) {
return; /* no use trying to set /dev/null */
}
#if defined(HAVE_LINUX_OS) || defined(HAVE_WIN32)
struct mtop mt_com;