/
tape-device.c
2356 lines (2014 loc) · 73.1 KB
/
tape-device.c
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
/*
* Copyright (c) 2007-2012 Zmanda, Inc. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
* Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
*/
#include "amanda.h"
#include "pipespawn.h"
#include <string.h> /* memset() */
#include "amutil.h"
#include "device.h"
#ifdef HAVE_SYS_TAPE_H
# include <sys/tape.h>
#endif
#ifdef HAVE_SYS_MTIO_H
# include <sys/mtio.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
/* This is equal to 2*1024*1024*1024 - 16*1024*1024 - 1, but written
explicitly to avoid overflow issues. */
#define RESETOFS_THRESHOLD (0x7effffff)
/* Largest possible block size on SCSI systems. */
#define LARGEST_BLOCK_ESTIMATE (16 * 1024 * 1024)
/*
* Type checking and casting macros
*/
#define TYPE_TAPE_DEVICE (tape_device_get_type())
#define TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice)
#define TAPE_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), tape_device_get_type(), TapeDevice const)
#define TAPE_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), tape_device_get_type(), TapeDeviceClass)
#define IS_TAPE_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), tape_device_get_type ())
#define TAPE_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), tape_device_get_type(), TapeDeviceClass)
GType tape_device_get_type (void);
/*
* Main object structure
*/
typedef struct TapeDevicePrivate_s TapeDevicePrivate;
typedef struct _TapeDevice {
Device __parent__;
/* It should go without saying that all this stuff is
* look-but-don't-touch. */
/* characteristics of the device */
gboolean fsf, bsf, fsr, bsr, eom, bsf_after_eom, broken_gmt_online;
gboolean leom;
gboolean nonblocking_open, fsf_after_filemark;
int final_filemarks;
/* 0 if we opened with O_RDWR; error otherwise. */
gboolean write_open_errno;
int fd;
TapeDevicePrivate * private;
} TapeDevice;
struct TapeDevicePrivate_s {
/* This holds the total number of bytes written to the device,
modulus RESETOFS_THRESHOLD. */
int write_count;
char * device_filename;
gsize read_block_size;
};
/*
* Class definition
*/
typedef struct _TapeDeviceClass TapeDeviceClass;
struct _TapeDeviceClass {
DeviceClass __parent__;
};
void tape_device_register(void);
/* useful callback for tape ops */
void tape_device_set_capabilities(TapeDevice *self,
gboolean fsf, PropertySurety fsf_surety, PropertySource fsf_source,
gboolean fsf_after_filemark, PropertySurety faf_surety, PropertySource faf_source,
gboolean bsf, PropertySurety bsf_surety, PropertySource bsf_source,
gboolean fsr, PropertySurety fsr_surety, PropertySource fsr_source,
gboolean bsr, PropertySurety bsr_surety, PropertySource bsr_source,
gboolean eom, PropertySurety eom_surety, PropertySource eom_source,
gboolean leom, PropertySurety leom_surety, PropertySource leom_source,
gboolean bsf_after_eom, PropertySurety bae_surety, PropertySource bae_source,
guint final_filemarks, PropertySurety ff_surety, PropertySource ff_source);
/* Real Operations (always return FALSE if not implemented) */
gboolean tape_rewind(int fd);
gboolean tape_fsf(int fd, guint count);
gboolean tape_bsf(int fd, guint count);
gboolean tape_fsr(int fd, guint count);
gboolean tape_bsr(int fd, guint count);
gint tape_fileno(int fd);
/* tape_fileno returns tape position file number, or one of these: */
#define TAPE_OP_ERROR -1
#define TAPE_POSITION_UNKNOWN -2
/* Possible (abstracted) results from a system I/O operation. */
typedef enum {
RESULT_SUCCESS,
RESULT_ERROR, /* Undefined error (*errmsg set) */
RESULT_SMALL_BUFFER, /* Tried to read with a buffer that is too
small. */
RESULT_NO_DATA, /* End of File, while reading */
RESULT_NO_SPACE, /* Out of space. Sometimes we don't know if
it was this or I/O error, but this is the
preferred explanation. */
RESULT_MAX
} IoResult;
/* returns a fileno like tape_fileno */
gint tape_eod(int fd);
gboolean tape_weof(int fd, guint8 count);
gboolean tape_setcompression(int fd, gboolean on);
gboolean tape_offl(int fd);
DeviceStatusFlags tape_is_tape_device(int fd);
DeviceStatusFlags get_tape_blocksize(int fd, guint64 *tape_blocksize);
DeviceStatusFlags tape_is_ready(int fd, TapeDevice *t_self);
#define tape_device_read_size(self) \
(((TapeDevice *)(self))->private->read_block_size? \
((TapeDevice *)(self))->private->read_block_size : ((Device *)(self))->block_size)
/*
* Our device-specific properties.
*/
#define PROPERTY_BROKEN_GMT_ONLINE (device_property_broken_gmt_online.ID)
#define PROPERTY_FSF (device_property_fsf.ID)
#define PROPERTY_FSF_AFTER_FILEMARK (device_property_fsf_after_filemark.ID)
#define PROPERTY_BSF (device_property_bsf.ID)
#define PROPERTY_FSR (device_property_fsr.ID)
#define PROPERTY_BSR (device_property_bsr.ID)
#define PROPERTY_EOM (device_property_eom.ID)
#define PROPERTY_BSF_AFTER_EOM (device_property_bsf_after_eom.ID)
#define PROPERTY_NONBLOCKING_OPEN (device_property_nonblocking_open.ID)
#define PROPERTY_FINAL_FILEMARKS (device_property_final_filemarks.ID)
static DevicePropertyBase device_property_broken_gmt_online;
static DevicePropertyBase device_property_fsf;
static DevicePropertyBase device_property_fsf_after_filemark;
static DevicePropertyBase device_property_bsf;
static DevicePropertyBase device_property_fsr;
static DevicePropertyBase device_property_bsr;
static DevicePropertyBase device_property_eom;
static DevicePropertyBase device_property_bsf_after_eom;
static DevicePropertyBase device_property_nonblocking_open;
static DevicePropertyBase device_property_final_filemarks;
static DevicePropertyBase device_property_read_buffer_size; /* old name for READ_BLOCK_SIZE */
/* here are local prototypes */
static void tape_device_init (TapeDevice * o);
static void tape_device_class_init (TapeDeviceClass * c);
static void tape_device_base_init (TapeDeviceClass * c);
static gboolean tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source);
static gboolean tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source);
static gboolean tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source);
static gboolean tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety *surety, PropertySource *source);
static gboolean tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source);
static void tape_device_open_device (Device * self, char * device_name, char * device_type, char * device_node);
static Device * tape_device_factory (char * device_name, char * device_type, char * device_node);
static DeviceStatusFlags tape_device_read_label(Device * self);
static DeviceWriteResult tape_device_write_block(Device * self, guint size,
gpointer data);
static int tape_device_read_block(Device * self, gpointer buf,
int * size_req, int max_block);
static gboolean tape_device_start (Device * self, DeviceAccessMode mode,
char * label, char * timestamp);
static gboolean tape_device_start_file (Device * self, dumpfile_t * ji);
static gboolean tape_device_finish_file (Device * self);
static dumpfile_t * tape_device_seek_file (Device * self, guint file);
static gboolean tape_device_seek_block (Device * self, guint64 block);
static gboolean tape_device_eject (Device * self);
static gboolean tape_device_finish (Device * self);
static gboolean tape_device_check_writable(Device *self);
static IoResult tape_device_robust_read (TapeDevice * self, void * buf,
int * count, char **errmsg);
static IoResult tape_device_robust_write (TapeDevice * self, void * buf, int count, char **errmsg);
static gboolean tape_device_fsf (TapeDevice * self, guint count);
static gboolean tape_device_fsr (TapeDevice * self, guint count);
static gboolean tape_device_bsr (TapeDevice * self, guint count, guint file, guint block);
static gboolean tape_device_eod (TapeDevice * self);
/* pointer to the class of our parent */
static DeviceClass *parent_class = NULL;
GType tape_device_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY(type == 0)) {
static const GTypeInfo info = {
sizeof (TapeDeviceClass),
(GBaseInitFunc) tape_device_base_init,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) tape_device_class_init,
(GClassFinalizeFunc) NULL,
NULL /* class_data */,
sizeof (TapeDevice),
0 /* n_preallocs */,
(GInstanceInitFunc) tape_device_init,
NULL
};
type = g_type_register_static (TYPE_DEVICE, "TapeDevice",
&info, (GTypeFlags)0);
}
return type;
}
static void
tape_device_init (TapeDevice * self) {
Device * d_self;
GValue response;
d_self = DEVICE(self);
bzero(&response, sizeof(response));
self->private = g_new0(TapeDevicePrivate, 1);
/* Clear all fields. */
d_self->block_size = 32768;
d_self->min_block_size = 32768;
d_self->max_block_size = LARGEST_BLOCK_ESTIMATE;
self->broken_gmt_online = FALSE;
self->fd = -1;
/* set all of the feature properties to an unsure default of FALSE */
self->broken_gmt_online = FALSE;
self->fsf = FALSE;
self->bsf = FALSE;
self->fsr = FALSE;
self->bsr = FALSE;
self->eom = FALSE;
self->leom = FALSE;
self->bsf_after_eom = FALSE;
g_value_init(&response, G_TYPE_BOOLEAN);
g_value_set_boolean(&response, FALSE);
device_set_simple_property(d_self, PROPERTY_BROKEN_GMT_ONLINE,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_FSF,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_FSF_AFTER_FILEMARK,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_BSF,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_FSR,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_BSR,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_EOM,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_LEOM,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
device_set_simple_property(d_self, PROPERTY_BSF_AFTER_EOM,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
#ifdef DEFAULT_TAPE_NON_BLOCKING_OPEN
self->nonblocking_open = TRUE;
#else
self->nonblocking_open = FALSE;
#endif
g_value_set_boolean(&response, self->nonblocking_open);
device_set_simple_property(d_self, PROPERTY_NONBLOCKING_OPEN,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
g_value_unset(&response);
self->final_filemarks = 2;
g_value_init(&response, G_TYPE_UINT);
g_value_set_uint(&response, self->final_filemarks);
device_set_simple_property(d_self, PROPERTY_FINAL_FILEMARKS,
&response, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
g_value_unset(&response);
self->private->read_block_size = 0;
g_value_init(&response, G_TYPE_UINT);
g_value_set_uint(&response, self->private->read_block_size);
device_set_simple_property(d_self, PROPERTY_READ_BLOCK_SIZE,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DEFAULT);
g_value_unset(&response);
self->private->write_count = 0;
self->private->device_filename = NULL;
/* Static properites */
g_value_init(&response, CONCURRENCY_PARADIGM_TYPE);
g_value_set_enum(&response, CONCURRENCY_PARADIGM_EXCLUSIVE);
device_set_simple_property(d_self, PROPERTY_CONCURRENCY,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
g_value_init(&response, STREAMING_REQUIREMENT_TYPE);
g_value_set_enum(&response, STREAMING_REQUIREMENT_DESIRED);
device_set_simple_property(d_self, PROPERTY_STREAMING,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
g_value_init(&response, G_TYPE_BOOLEAN);
g_value_set_boolean(&response, TRUE);
device_set_simple_property(d_self, PROPERTY_APPENDABLE,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
g_value_init(&response, G_TYPE_BOOLEAN);
g_value_set_boolean(&response, FALSE);
device_set_simple_property(d_self, PROPERTY_PARTIAL_DELETION,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
g_value_init(&response, G_TYPE_BOOLEAN);
g_value_set_boolean(&response, FALSE);
device_set_simple_property(d_self, PROPERTY_FULL_DELETION,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
g_value_init(&response, MEDIA_ACCESS_MODE_TYPE);
g_value_set_enum(&response, MEDIA_ACCESS_MODE_READ_WRITE);
device_set_simple_property(d_self, PROPERTY_MEDIUM_ACCESS_TYPE,
&response, PROPERTY_SURETY_GOOD, PROPERTY_SOURCE_DETECTED);
g_value_unset(&response);
}
static void tape_device_finalize(GObject * obj_self) {
TapeDevice * self = TAPE_DEVICE(obj_self);
if(G_OBJECT_CLASS(parent_class)->finalize) \
(* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
robust_close(self->fd);
self->fd = -1;
amfree(self->private->device_filename);
amfree(self->private);
}
static void
tape_device_class_init (TapeDeviceClass * c)
{
DeviceClass *device_class = (DeviceClass *)c;
GObjectClass *g_object_class = (GObjectClass *)c;
parent_class = g_type_class_ref (TYPE_DEVICE);
device_class->open_device = tape_device_open_device;
device_class->read_label = tape_device_read_label;
device_class->write_block = tape_device_write_block;
device_class->read_block = tape_device_read_block;
device_class->start = tape_device_start;
device_class->start_file = tape_device_start_file;
device_class->finish_file = tape_device_finish_file;
device_class->seek_file = tape_device_seek_file;
device_class->seek_block = tape_device_seek_block;
device_class->eject = tape_device_eject;
device_class->finish = tape_device_finish;
device_class->check_writable = tape_device_check_writable;
g_object_class->finalize = tape_device_finalize;
}
static void
tape_device_base_init (TapeDeviceClass * c)
{
DeviceClass *device_class = (DeviceClass *)c;
device_class_register_property(device_class, PROPERTY_BROKEN_GMT_ONLINE,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_FSF,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_FSF_AFTER_FILEMARK,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_BSF,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_FSR,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_BSR,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_EOM,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_BSF_AFTER_EOM,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_NONBLOCKING_OPEN,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
device_class_register_property(device_class, PROPERTY_FINAL_FILEMARKS,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_final_filemarks_fn);
/* We don't (yet?) support reading the device's compression state, so not
* gettable. */
device_class_register_property(device_class, PROPERTY_COMPRESSION,
PROPERTY_ACCESS_SET_MASK,
NULL,
tape_device_set_compression_fn);
device_class_register_property(device_class, PROPERTY_READ_BLOCK_SIZE,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
tape_device_get_read_block_size_fn,
tape_device_set_read_block_size_fn);
device_class_register_property(device_class, device_property_read_buffer_size.ID,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
tape_device_get_read_block_size_fn,
tape_device_set_read_block_size_fn);
/* add the ability to set LEOM to FALSE, for testing purposes */
device_class_register_property(device_class, PROPERTY_LEOM,
PROPERTY_ACCESS_GET_MASK | PROPERTY_ACCESS_SET_BEFORE_START,
device_simple_property_get_fn,
tape_device_set_feature_property_fn);
}
static gboolean
tape_device_set_feature_property_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source)
{
TapeDevice *self = TAPE_DEVICE(p_self);
GValue old_val;
gboolean old_bool, new_bool;
PropertySurety old_surety;
PropertySource old_source;
new_bool = g_value_get_boolean(val);
/* get the old source and surety and see if we're willing to make this change */
bzero(&old_val, sizeof(old_val));
if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
old_bool = g_value_get_boolean(&old_val);
if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
if (new_bool != old_bool) {
device_set_error(p_self, g_strdup_printf(_(
"Value for property '%s' was autodetected and cannot be changed"),
base->name),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
} else {
/* pretend we set it, but don't change surety/source */
return TRUE;
}
}
}
/* (note: PROPERTY_* are not constants, so we can't use switch) */
if (base->ID == PROPERTY_BROKEN_GMT_ONLINE)
self->broken_gmt_online = new_bool;
else if (base->ID == PROPERTY_FSF)
self->fsf = new_bool;
else if (base->ID == PROPERTY_FSF_AFTER_FILEMARK)
self->fsf_after_filemark = new_bool;
else if (base->ID == PROPERTY_BSF)
self->bsf = new_bool;
else if (base->ID == PROPERTY_FSR)
self->fsr = new_bool;
else if (base->ID == PROPERTY_BSR)
self->bsr = new_bool;
else if (base->ID == PROPERTY_EOM)
self->eom = new_bool;
else if (base->ID == PROPERTY_BSF_AFTER_EOM)
self->bsf_after_eom = new_bool;
else if (base->ID == PROPERTY_NONBLOCKING_OPEN)
self->nonblocking_open = new_bool;
else if (base->ID == PROPERTY_LEOM)
self->leom = new_bool;
else
return FALSE; /* shouldn't happen */
return device_simple_property_set_fn(p_self, base, val, surety, source);
}
static gboolean
tape_device_set_final_filemarks_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source)
{
TapeDevice *self = TAPE_DEVICE(p_self);
GValue old_val;
gboolean old_int, new_int;
PropertySurety old_surety;
PropertySource old_source;
new_int = g_value_get_uint(val);
/* get the old source and surety and see if we're willing to make this change */
bzero(&old_val, sizeof(old_val));
if (device_get_simple_property(p_self, base->ID, &old_val, &old_surety, &old_source)) {
old_int = g_value_get_uint(&old_val);
if (old_surety == PROPERTY_SURETY_GOOD && old_source == PROPERTY_SOURCE_DETECTED) {
if (new_int != old_int) {
device_set_error(p_self, g_strdup_printf(_(
"Value for property '%s' was autodetected and cannot be changed"),
base->name),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
} else {
/* pretend we set it, but don't change surety/source */
return TRUE;
}
}
}
self->final_filemarks = new_int;
return device_simple_property_set_fn(p_self, base, val, surety, source);
}
static gboolean
tape_device_set_compression_fn(Device *p_self, DevicePropertyBase *base,
GValue *val, PropertySurety surety, PropertySource source)
{
TapeDevice *self = TAPE_DEVICE(p_self);
gboolean request = g_value_get_boolean(val);
/* We allow this property to be set at any time. This is mostly
* because setting compression is a hit-and-miss proposition
* at any time; some drives accept the mode setting but don't
* actually support compression, while others do support
* compression but do it via density settings or some other
* way. Set this property whenever you want, but all we'll do
* is report whether or not the ioctl succeeded. */
if (tape_setcompression(self->fd, request)) {
/* looks good .. let's start the device over, though */
device_clear_volume_details(p_self);
} else {
device_set_error(p_self,
g_strdup("Error setting COMPRESSION property"),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
}
return device_simple_property_set_fn(p_self, base, val, surety, source);
}
static gboolean
tape_device_get_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
GValue *val, PropertySurety *surety, PropertySource *source)
{
/* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
return device_simple_property_get_fn(p_self, &device_property_read_block_size,
val, surety, source);
}
static gboolean
tape_device_set_read_block_size_fn(Device *p_self, DevicePropertyBase *base G_GNUC_UNUSED,
GValue *val, PropertySurety surety, PropertySource source)
{
TapeDevice *self = TAPE_DEVICE(p_self);
guint read_block_size = g_value_get_uint(val);
if (read_block_size != 0 &&
((gsize)read_block_size < p_self->block_size ||
(gsize)read_block_size > p_self->max_block_size)) {
device_set_error(p_self,
g_strdup_printf("Error setting READ-BLOCk-SIZE property to '%u', it must be between %zu and %zu", read_block_size, p_self->block_size, p_self->max_block_size),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
}
self->private->read_block_size = read_block_size;
/* use the READ_BLOCK_SIZE, even if we're invoked to get the old READ_BUFFER_SIZE */
return device_simple_property_set_fn(p_self, &device_property_read_block_size,
val, surety, source);
}
void tape_device_register(void) {
static const char * device_prefix_list[] = { "tape", NULL };
/* First register tape-specific properties */
device_property_fill_and_register(&device_property_broken_gmt_online,
G_TYPE_BOOLEAN, "broken_gmt_online",
"Does this drive support the GMT_ONLINE macro?");
device_property_fill_and_register(&device_property_fsf,
G_TYPE_BOOLEAN, "fsf",
"Does this drive support the MTFSF command?");
device_property_fill_and_register(&device_property_fsf_after_filemark,
G_TYPE_BOOLEAN, "fsf_after_filemark",
"Does this drive needs a FSF if a filemark is already read?");
device_property_fill_and_register(&device_property_bsf,
G_TYPE_BOOLEAN, "bsf",
"Does this drive support the MTBSF command?" );
device_property_fill_and_register(&device_property_fsr,
G_TYPE_BOOLEAN, "fsr",
"Does this drive support the MTFSR command?");
device_property_fill_and_register(&device_property_bsr,
G_TYPE_BOOLEAN, "bsr",
"Does this drive support the MTBSR command?");
device_property_fill_and_register(&device_property_eom,
G_TYPE_BOOLEAN, "eom",
"Does this drive support the MTEOM command?");
device_property_fill_and_register(&device_property_bsf_after_eom,
G_TYPE_BOOLEAN,
"bsf_after_eom",
"Does this drive require an MTBSF after MTEOM in order to append?" );
device_property_fill_and_register(&device_property_nonblocking_open,
G_TYPE_BOOLEAN,
"nonblocking_open",
"Does this drive require a open with O_NONBLOCK?" );
device_property_fill_and_register(&device_property_final_filemarks,
G_TYPE_UINT, "final_filemarks",
"How many filemarks to write after the last tape file?" );
device_property_fill_and_register(&device_property_read_buffer_size,
G_TYPE_UINT, "read_buffer_size",
"(deprecated name for READ_BLOCK_SIZE)");
/* Then the device itself */
register_device(tape_device_factory, device_prefix_list);
}
/* Open the tape device, trying various combinations of O_RDWR and
O_NONBLOCK. Returns -1 and calls device_set_error for errors
On Linux, with O_NONBLOCK, the kernel just checks the state once,
whereas it checks it every second for ST_BLOCK_SECONDS if O_NONBLOCK is
not given. Amanda already have the code to poll, we want open to check
the state only once. */
static int try_open_tape_device(TapeDevice * self, char * device_filename) {
int fd;
int save_errno;
DeviceStatusFlags new_status;
guint64 tape_blocksize;
#ifdef O_NONBLOCK
int nonblocking = 0;
if (self->nonblocking_open) {
nonblocking = O_NONBLOCK;
}
#endif
#ifdef O_NONBLOCK
fd = robust_open(device_filename, O_RDWR | nonblocking, 0);
save_errno = errno;
if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
/* Maybe we don't support O_NONBLOCK for tape devices. */
fd = robust_open(device_filename, O_RDWR, 0);
save_errno = errno;
}
#else
fd = robust_open(device_filename, O_RDWR, 0);
save_errno = errno;
#endif
if (fd >= 0) {
self->write_open_errno = 0;
} else {
if (errno == EACCES || errno == EPERM
#ifdef EROFS
|| errno == EROFS
#endif
) {
/* Device is write-protected. */
self->write_open_errno = errno;
#ifdef O_NONBLOCK
fd = robust_open(device_filename, O_RDONLY | nonblocking, 0);
save_errno = errno;
if (fd < 0 && nonblocking && (save_errno == EWOULDBLOCK || save_errno == EINVAL)) {
fd = robust_open(device_filename, O_RDONLY, 0);
save_errno = errno;
}
#else
fd = robust_open(device_filename, O_RDONLY, 0);
save_errno = errno;
#endif
}
}
#ifdef O_NONBLOCK
/* Clear O_NONBLOCK for operations from now on. */
if (fd >= 0 && nonblocking) {
int r = fcntl(fd, F_GETFL, 0);
if (r < 0) {
device_set_error(DEVICE(self),
g_strdup_printf("Can't fcntl(F_GETFL) on %s: %s", self->private->device_filename, strerror(errno)),
DEVICE_STATUS_DEVICE_BUSY | DEVICE_STATUS_DEVICE_ERROR);
}
r = fcntl(fd, F_SETFL, r & ~O_NONBLOCK);
if (r < 0) {
device_set_error(DEVICE(self),
g_strdup_printf("Can't fcntl(F_SETFL) on %s: %s", self->private->device_filename, strerror(errno)),
DEVICE_STATUS_DEVICE_BUSY | DEVICE_STATUS_DEVICE_ERROR);
}
}
errno = save_errno;
/* function continues after #endif */
#endif /* O_NONBLOCK */
if (fd < 0) {
DeviceStatusFlags status_flag = 0;
if (errno == EBUSY)
status_flag = DEVICE_STATUS_DEVICE_BUSY;
else
status_flag = DEVICE_STATUS_DEVICE_ERROR;
device_set_error(DEVICE(self),
g_strdup_printf(_("Can't open tape device %s: %s"), self->private->device_filename, strerror(errno)),
status_flag);
return -1;
}
/* Check that this is actually a tape device. */
new_status = tape_is_tape_device(fd);
if (new_status & DEVICE_STATUS_DEVICE_ERROR) {
device_set_error(DEVICE(self),
g_strdup_printf(_("File %s is not a tape device"), self->private->device_filename),
new_status);
robust_close(fd);
return -1;
}
if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
device_set_error(DEVICE(self),
g_strdup_printf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
new_status);
robust_close(fd);
return -1;
}
new_status = tape_is_ready(fd, self);
if (new_status & DEVICE_STATUS_VOLUME_MISSING) {
device_set_error(DEVICE(self),
g_strdup_printf(_("Tape device %s is empty"), self->private->device_filename),
new_status);
robust_close(fd);
return -1;
}
if (new_status != DEVICE_STATUS_SUCCESS) {
device_set_error(DEVICE(self),
g_strdup_printf(_("Tape device %s is not ready or is empty"), self->private->device_filename),
new_status);
robust_close(fd);
return -1;
}
new_status = get_tape_blocksize(fd, &tape_blocksize);
if (new_status != DEVICE_STATUS_SUCCESS) {
device_set_error(DEVICE(self),
g_strdup_printf(_("Can't get the blocksize of the device %s"),
self->private->device_filename),
new_status);
robust_close(fd);
return -1;
}
if (tape_blocksize > 0 && tape_blocksize != tape_device_read_size(self)) {
device_set_error(DEVICE(self),
g_strdup_printf(_("Device %s use fixed block size of %lld and tapetype use %lld"),
self->private->device_filename,
(long long)tape_blocksize,
(long long)tape_device_read_size(self)),
DEVICE_STATUS_VOLUME_ERROR|DEVICE_STATUS_DEVICE_ERROR);
robust_close(fd);
return -1;
}
if (tape_blocksize == 0) {
g_debug("Device is in variable block size");
} else {
g_debug("Device is in fixed block size of %lld", (long long)tape_blocksize);
}
return fd;
}
static void
tape_device_open_device (Device * dself, char * device_name,
char * device_type, char * device_node) {
TapeDevice * self;
GValue val;
self = TAPE_DEVICE(dself);
self->fd = -1;
self->private->device_filename = g_strdup(device_node);
/* Set tape drive/OS info */
bzero(&val, sizeof(val));
g_value_init(&val, G_TYPE_BOOLEAN);
self->fsf = TRUE;
g_value_set_boolean(&val, self->fsf);
device_set_simple_property(dself, PROPERTY_FSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->fsf_after_filemark = DEFAULT_FSF_AFTER_FILEMARK;
g_value_set_boolean(&val, self->fsf_after_filemark);
device_set_simple_property(dself, PROPERTY_FSF_AFTER_FILEMARK, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->bsf = TRUE;
g_value_set_boolean(&val, self->bsf);
device_set_simple_property(dself, PROPERTY_BSF, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->fsr = TRUE;
g_value_set_boolean(&val, self->fsr);
device_set_simple_property(dself, PROPERTY_FSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->bsr = TRUE;
g_value_set_boolean(&val, self->bsr);
device_set_simple_property(dself, PROPERTY_BSR, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->eom = TRUE;
g_value_set_boolean(&val, self->eom);
device_set_simple_property(dself, PROPERTY_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->leom = FALSE;
g_value_set_boolean(&val, self->leom);
device_set_simple_property(dself, PROPERTY_LEOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
self->bsf_after_eom = FALSE;
g_value_set_boolean(&val, self->bsf_after_eom);
device_set_simple_property(dself, PROPERTY_BSF_AFTER_EOM, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
g_value_unset(&val);
g_value_init(&val, G_TYPE_UINT);
self->final_filemarks = 2;
g_value_set_uint(&val, self->final_filemarks);
device_set_simple_property(dself, PROPERTY_FINAL_FILEMARKS, &val, PROPERTY_SURETY_BAD, PROPERTY_SOURCE_DEFAULT);
g_value_unset(&val);
/* Chain up */
if (parent_class->open_device) {
parent_class->open_device(dself, device_name, device_type, device_node);
}
}
static DeviceStatusFlags tape_device_read_label(Device * dself) {
TapeDevice * self;
char * header_buffer;
int buffer_len;
IoResult result;
dumpfile_t *header;
DeviceStatusFlags new_status;
char *msg = NULL;
self = TAPE_DEVICE(dself);
amfree(dself->volume_label);
amfree(dself->volume_time);
dumpfile_free(dself->volume_header);
dself->volume_header = NULL;
if (device_in_error(self)) return dself->status;
if (self->fd == -1) {
self->fd = try_open_tape_device(self, self->private->device_filename);
/* if the open failed, then try_open_tape_device already set the
* approppriate error status */
if (self->fd == -1)
return dself->status;
}
/* Rewind it. */
if (!tape_rewind(self->fd)) {
device_set_error(dself,
g_strdup_printf(_("Error rewinding device %s to read label: %s"),
self->private->device_filename, strerror(errno)),
DEVICE_STATUS_DEVICE_ERROR
| DEVICE_STATUS_VOLUME_ERROR);
return dself->status;
}
buffer_len = tape_device_read_size(self);
header_buffer = g_try_malloc(buffer_len);
if (header_buffer == NULL) {
device_set_error(dself,
g_strdup(_("Failed to allocate memory")),
DEVICE_STATUS_DEVICE_ERROR
| DEVICE_STATUS_VOLUME_ERROR);
return dself->status;
}
result = tape_device_robust_read(self, header_buffer, &buffer_len, &msg);
if (result != RESULT_SUCCESS) {
free(header_buffer);
tape_rewind(self->fd);
/* I/O error. */
switch (result) {
case RESULT_NO_DATA:
msg = g_strdup(_("no data"));
new_status = (DEVICE_STATUS_VOLUME_ERROR |
DEVICE_STATUS_VOLUME_UNLABELED);
header = dself->volume_header = g_new(dumpfile_t, 1);
fh_init(header);
break;
case RESULT_SMALL_BUFFER:
msg = g_strdup(_("block size too small"));
new_status = (DEVICE_STATUS_DEVICE_ERROR |
DEVICE_STATUS_VOLUME_ERROR);
header = dself->volume_header = g_new(dumpfile_t, 1);
fh_init(header);
header->type = F_WEIRD;
break;
default:
msg = g_strdup(_("unknown error"));
// fallthrough
case RESULT_ERROR:
new_status = (DEVICE_STATUS_DEVICE_ERROR |
DEVICE_STATUS_VOLUME_ERROR |
DEVICE_STATUS_VOLUME_UNLABELED);
break;
}
device_set_error(dself,
g_strdup_printf(_("Error reading Amanda header: %s"),
msg? msg : _("unknown error")),
new_status);
amfree(msg);
return dself->status;
}
if (buffer_len < 32768) {
device_set_error(dself,
g_strdup_printf(_("header is too small: %d bytes"), buffer_len),
DEVICE_STATUS_DEVICE_ERROR | DEVICE_STATUS_VOLUME_ERROR);
free(header_buffer);
return dself->status;
}
dself->header_block_size = buffer_len;
header = dself->volume_header = g_new(dumpfile_t, 1);
fh_init(header);
parse_file_header(header_buffer, header, buffer_len);
amfree(header_buffer);
if (header->type != F_TAPESTART) {
device_set_error(dself,
g_strdup(_("No tapestart header -- unlabeled device?")),
DEVICE_STATUS_VOLUME_UNLABELED);
return dself->status;
}