/
dev.c
2593 lines (2363 loc) · 69.3 KB
/
dev.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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 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
Affero 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.
*/
/*
* dev.c -- low level operations on device (storage device)
*
* Kern Sibbald, MM
*
* NOTE!!!! None of these routines are reentrant. You must
* use dev->rLock() and dev->Unlock() at a higher level,
* or use the xxx_device() equivalents. By moving the
* thread synchronization to a higher level, we permit
* the higher level routines to "seize" the device and
* to carry out operations without worrying about who
* set what lock (i.e. race conditions).
*
* Note, this is the device dependent code, and may have
* to be modified for each system, but is meant to
* be as "generic" as possible.
*
* The purpose of this code is to develop a SIMPLE Storage
* daemon. More complicated coding (double buffering, writer
* thread, ...) is left for a later version.
*/
/*
* Handling I/O errors and end of tape conditions are a bit tricky.
* This is how it is currently done when writing.
* On either an I/O error or end of tape,
* we will stop writing on the physical device (no I/O recovery is
* attempted at least in this daemon). The state flag will be sent
* to include ST_EOT, which is ephemeral, and ST_WEOT, which is
* persistent. Lots of routines clear ST_EOT, but ST_WEOT is
* cleared only when the problem goes away. Now when ST_WEOT
* is set all calls to write_block_to_device() call the fix_up
* routine. In addition, all threads are blocked
* from writing on the tape by calling lock_dev(), and thread other
* than the first thread to hit the EOT will block on a condition
* variable. The first thread to hit the EOT will continue to
* be able to read and write the tape (he sort of tunnels through
* the locking mechanism -- see lock_dev() for details).
*
* Now presumably somewhere higher in the chain of command
* (device.c), someone will notice the EOT condition and
* get a new tape up, get the tape label read, and mark
* the label for rewriting. Then this higher level routine
* will write the unwritten buffer to the new volume.
* Finally, he will release
* any blocked threads by doing a broadcast on the condition
* variable. At that point, we should be totally back in
* business with no lost data.
*/
#include "bareos.h"
#include "stored.h"
#ifdef HAVE_LINUX_OS
#include "backends/vtape.h"
#endif
#ifdef HAVE_GFAPI
#include "backends/gfapi_device.h"
#endif
#ifdef HAVE_OBJECTSTORE
#include "backends/object_store_device.h"
#endif
#ifdef HAVE_RADOS
#include "backends/rados_device.h"
#endif
#ifdef HAVE_WIN32
#include "backends/win32_tape_device.h"
#include "backends/win32_file_device.h"
#else
#include "backends/unix_tape_device.h"
#include "backends/unix_file_device.h"
#endif
#ifndef O_NONBLOCK
#define O_NONBLOCK 0
#endif
/* Forward referenced functions */
void set_os_device_parameters(DCR *dcr);
static bool dev_get_os_pos(DEVICE *dev, struct mtget *mt_stat);
static const char *mode_to_str(int omode);
static DEVICE *m_init_dev(JCR *jcr, DEVRES *device, bool new_init);
/*
* Allocate and initialize the DEVICE structure
* Note, if dev is non-NULL, it is already allocated,
* thus we neither allocate it nor free it. This allows
* the caller to put the packet in shared memory.
*
* Note, for a tape, the device->device_name is the device name
* (e.g. /dev/nst0), and for a file, the device name
* is the directory in which the file will be placed.
*
*/
DEVICE *init_dev(JCR *jcr, DEVRES *device)
{
DEVICE *dev = m_init_dev(jcr, device, false);
return dev;
}
static DEVICE *
m_init_dev(JCR *jcr, DEVRES *device, bool new_init)
{
struct stat statp;
int errstat;
DCR *dcr = NULL;
DEVICE *dev;
uint32_t max_bs;
Dmsg1(400, "max_block_size in device res is %u\n", device->max_block_size);
/*
* If no device type specified, try to guess
*/
if (!device->dev_type) {
/*
* Check that device is available
*/
if (stat(device->device_name, &statp) < 0) {
berrno be;
Jmsg2(jcr, M_ERROR, 0, _("Unable to stat device %s: ERR=%s\n"),
device->device_name, be.bstrerror());
return NULL;
}
if (S_ISDIR(statp.st_mode)) {
device->dev_type = B_FILE_DEV;
} else if (S_ISCHR(statp.st_mode)) {
device->dev_type = B_TAPE_DEV;
} else if (S_ISFIFO(statp.st_mode)) {
device->dev_type = B_FIFO_DEV;
} else if (!(device->cap_bits & CAP_REQMOUNT)) {
Jmsg2(jcr, M_ERROR, 0,
_("%s is an unknown device type. Must be tape or directory, st_mode=%x\n"),
device->device_name, statp.st_mode);
return NULL;
}
}
switch (device->dev_type) {
#ifdef HAVE_LINUX_OS
case B_VTAPE_DEV:
dev = New(vtape);
break;
#endif
#ifdef HAVE_GFAPI
case B_GFAPI_DEV:
dev = New(gfapi_device);
break;
#endif
#ifdef HAVE_OBJECTSTORE
case B_OBJECT_STORE_DEV:
dev = New(object_store_device);
break;
#endif
#ifdef HAVE_RADOS
case B_RADOS_DEV:
dev = New(rados_device);
break;
#endif
#ifdef HAVE_WIN32
case B_TAPE_DEV:
dev = New(win32_tape_device);
break;
case B_FILE_DEV:
dev = New(win32_file_device);
break;
#else
case B_TAPE_DEV:
dev = New(unix_tape_device);
break;
case B_FILE_DEV:
case B_FIFO_DEV:
dev = New(unix_file_device);
break;
#endif
default:
Jmsg2(jcr, M_ERROR, 0, _("%s has an unknown device type %d\n"),
device->device_name, device->dev_type);
return NULL;
}
dev->clear_slot(); /* unknown */
/*
* Copy user supplied device parameters from Resource
*/
dev->dev_name = get_memory(strlen(device->device_name)+1);
pm_strcpy(dev->dev_name, device->device_name);
dev->prt_name = get_memory(strlen(device->device_name) + strlen(device->hdr.name) + 20);
/*
* We edit "Resource-name" (physical-name)
*/
Mmsg(dev->prt_name, "\"%s\" (%s)", device->hdr.name, device->device_name);
Dmsg1(400, "Allocate dev=%s\n", dev->print_name());
dev->capabilities = device->cap_bits;
/*
* current block sizes
*/
dev->min_block_size = device->min_block_size;
dev->max_block_size = device->max_block_size;
dev->max_volume_size = device->max_volume_size;
dev->max_file_size = device->max_file_size;
dev->max_concurrent_jobs = device->max_concurrent_jobs;
dev->volume_capacity = device->volume_capacity;
dev->max_rewind_wait = device->max_rewind_wait;
dev->max_open_wait = device->max_open_wait;
dev->max_open_vols = device->max_open_vols;
dev->vol_poll_interval = device->vol_poll_interval;
dev->max_spool_size = device->max_spool_size;
dev->drive_index = device->drive_index;
dev->autoselect = device->autoselect;
dev->norewindonclose = device->norewindonclose;
dev->dev_type = device->dev_type;
dev->device = device;
/*
* Sanity check
*/
if (dev->vol_poll_interval && dev->vol_poll_interval < 60) {
dev->vol_poll_interval = 60;
}
device->dev = dev;
if (dev->is_fifo()) {
dev->capabilities |= CAP_STREAM; /* set stream device */
}
/*
* If the device requires mount :
* - Check that the mount point is available
* - Check that (un)mount commands are defined
*/
if (dev->is_file() && dev->requires_mount()) {
if (!device->mount_point || stat(device->mount_point, &statp) < 0) {
berrno be;
dev->dev_errno = errno;
Jmsg2(jcr, M_ERROR_TERM, 0, _("Unable to stat mount point %s: ERR=%s\n"),
device->mount_point, be.bstrerror());
}
if (!device->mount_command || !device->unmount_command) {
Jmsg0(jcr, M_ERROR_TERM, 0, _("Mount and unmount commands must defined for a device which requires mount.\n"));
}
}
/*
* Sanity check
*/
if (dev->max_block_size == 0) {
max_bs = DEFAULT_BLOCK_SIZE;
} else {
max_bs = dev->max_block_size;
}
if (dev->min_block_size > max_bs) {
Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"), dev->print_name());
}
if (dev->max_block_size > MAX_BLOCK_LENGTH) {
Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
dev->max_block_size = 0;
}
if (dev->max_block_size % TAPE_BSIZE != 0) {
Jmsg3(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size=%d.\n"),
dev->max_block_size, dev->print_name(), TAPE_BSIZE);
}
if (dev->max_volume_size != 0 && dev->max_volume_size < (dev->max_block_size << 4)) {
Jmsg(jcr, M_ERROR_TERM, 0, _("Max Vol Size < 8 * Max Block Size for device %s\n"), dev->print_name());
}
dev->errmsg = get_pool_memory(PM_EMSG);
*dev->errmsg = 0;
if ((errstat = dev->init_mutex()) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
if ((errstat = pthread_cond_init(&dev->wait, NULL)) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
if ((errstat = pthread_cond_init(&dev->wait_next_vol, NULL)) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init cond variable: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
if ((errstat = pthread_mutex_init(&dev->spool_mutex, NULL)) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init spool mutex: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
if ((errstat = dev->init_acquire_mutex()) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
if ((errstat = dev->init_read_acquire_mutex()) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init read acquire mutex: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
dev->set_mutex_priorities();
#ifdef xxx
if ((errstat = rwl_init(&dev->lock)) != 0) {
berrno be;
dev->dev_errno = errstat;
Mmsg1(dev->errmsg, _("Unable to init mutex: ERR=%s\n"), be.bstrerror(errstat));
Jmsg0(jcr, M_ERROR_TERM, 0, dev->errmsg);
}
#endif
dev->clear_opened();
dev->attached_dcrs = New(dlist(dcr, &dcr->dev_link));
Dmsg2(100, "init_dev: tape=%d dev_name=%s\n", dev->is_tape(), dev->dev_name);
dev->initiated = true;
Dmsg3(100, "dev=%s dev_max_bs=%u max_bs=%u\n", dev->dev_name, dev->device->max_block_size, dev->max_block_size);
return dev;
}
/*
* Set the block size of the device.
* If the volume block size is zero, we set the max block size to what is
* configured in the device resource i.e. dev->device->max_block_size.
*
* If dev->device->max_block_size is zero, do nothing and leave dev->max_block_size as it is.
*/
void DEVICE::set_blocksizes(DCR *dcr) {
DEVICE* dev = this;
JCR* jcr = dcr->jcr;
uint32_t max_bs;
Dmsg4(100, "Device %s has dev->device->max_block_size of %u and dev->max_block_size of %u, dcr->VolMaxBlocksize is %u\n",
dev->print_name(), dev->device->max_block_size, dev->max_block_size, dcr->VolMaxBlocksize);
if (dcr->VolMaxBlocksize == 0 && dev->device->max_block_size != 0) {
Dmsg2(100, "setting dev->max_block_size to dev->device->max_block_size=%u "
"on device %s because dcr->VolMaxBlocksize is 0\n",
dev->device->max_block_size, dev->print_name());
dev->min_block_size = dev->device->min_block_size;
dev->max_block_size = dev->device->max_block_size;
} else if (dcr->VolMaxBlocksize != 0) {
dev->min_block_size = dcr->VolMinBlocksize;
dev->max_block_size = dcr->VolMaxBlocksize;
}
/*
* Sanity check
*/
if (dev->max_block_size == 0) {
max_bs = DEFAULT_BLOCK_SIZE;
} else {
max_bs = dev->max_block_size;
}
if (dev->min_block_size > max_bs) {
Jmsg(jcr, M_ERROR_TERM, 0, _("Min block size > max on device %s\n"), dev->print_name());
}
if (dev->max_block_size > MAX_BLOCK_LENGTH) {
Jmsg3(jcr, M_ERROR, 0, _("Block size %u on device %s is too large, using default %u\n"),
dev->max_block_size, dev->print_name(), DEFAULT_BLOCK_SIZE);
dev->max_block_size = 0;
}
if (dev->max_block_size % TAPE_BSIZE != 0) {
Jmsg3(jcr, M_WARNING, 0, _("Max block size %u not multiple of device %s block size=%d.\n"),
dev->max_block_size, dev->print_name(), TAPE_BSIZE);
}
if (dev->max_volume_size != 0 && dev->max_volume_size < (dev->max_block_size << 4)) {
Jmsg(jcr, M_ERROR_TERM, 0, _("Max Vol Size < 8 * Max Block Size for device %s\n"), dev->print_name());
}
Dmsg3(100, "set minblocksize to %d, maxblocksize to %d on device %s\n",
dev->min_block_size, dev->max_block_size, dev->print_name());
/*
* If blocklen is not dev->max_block_size create a new block with the right size.
* (as header is always dev->label_block_size which is preset with DEFAULT_BLOCK_SIZE)
*/
if (dcr->block) {
if (dcr->block->buf_len != dev->max_block_size) {
Dmsg2(100, "created new block of buf_len: %u on device %s\n",
dev->max_block_size, dev->print_name());
free_block(dcr->block);
dcr->block = new_block(dev);
Dmsg2(100, "created new block of buf_len: %u on device %s, freeing block\n",
dcr->block->buf_len, dev->print_name());
}
}
}
/*
* Set the block size of the device to the label_block_size
* to read labels as we want to always use that blocksize when
* writing volume labels
*/
void DEVICE::set_label_blocksize(DCR *dcr)
{
DEVICE *dev = this;
Dmsg3(100, "setting minblocksize to %u, "
"maxblocksize to label_block_size=%u, on device %s\n",
dev->device->label_block_size, dev->device->label_block_size, dev->print_name());
dev->min_block_size = dev->device->label_block_size;
dev->max_block_size = dev->device->label_block_size;
/*
* If blocklen is not dev->max_block_size create a new block with the right size
* (as header is always label_block_size)
*/
if (dcr->block) {
if (dcr->block->buf_len != dev->max_block_size) {
free_block(dcr->block);
dcr->block = new_block(dev);
Dmsg2(100, "created new block of buf_len: %u on device %s\n",
dcr->block->buf_len, dev->print_name());
}
}
}
/*
* Open the device with the operating system and
* initialize buffer pointers.
*
* Returns: true on success
* false on error
*
* Note, for a tape, the VolName is the name we give to the
* volume (not really used here), but for a file, the
* VolName represents the name of the file to be created/opened.
* In the case of a file, the full name is the device name
* (archive_name) with the VolName concatenated.
*/
bool DEVICE::open(DCR *dcr, int omode)
{
int preserve = 0;
if (is_open()) {
if (open_mode == omode) {
return true;
} else {
d_close(m_fd);
clear_opened();
Dmsg0(100, "Close fd for mode change.\n");
preserve = state & (ST_LABEL | ST_APPENDREADY | ST_READREADY);
}
}
if (dcr) {
dcr->setVolCatName(dcr->VolumeName);
VolCatInfo = dcr->VolCatInfo; /* structure assign */
}
Dmsg4(100, "open dev: type=%d dev_name=%s vol=%s mode=%s\n", dev_type,
print_name(), getVolCatName(), mode_to_str(omode));
state &= ~(ST_LABEL | ST_APPENDREADY | ST_READREADY | ST_EOT | ST_WEOT | ST_EOF);
label_type = B_BAREOS_LABEL;
if (is_tape() || is_fifo()) {
open_tape_device(dcr, omode);
} else {
Dmsg1(100, "call open_file_device mode=%s\n", mode_to_str(omode));
open_file_device(dcr, omode);
}
state |= preserve; /* reset any important state info */
Dmsg2(100, "preserve=0x%x fd=%d\n", preserve, m_fd);
return m_fd >= 0;
}
void DEVICE::set_mode(int mode)
{
switch (mode) {
case CREATE_READ_WRITE:
oflags = O_CREAT | O_RDWR | O_BINARY;
break;
case OPEN_READ_WRITE:
oflags = O_RDWR | O_BINARY;
break;
case OPEN_READ_ONLY:
oflags = O_RDONLY | O_BINARY;
break;
case OPEN_WRITE_ONLY:
oflags = O_WRONLY | O_BINARY;
break;
default:
Emsg0(M_ABORT, 0, _("Illegal mode given to open dev.\n"));
}
}
/*
* Open a tape device
*/
void DEVICE::open_tape_device(DCR *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");
get_autochanger_loaded_slot(dcr);
open_mode = omode;
set_mode(omode);
if (timeout < 1) {
timeout = 1;
}
errno = 0;
if (is_fifo() && timeout) {
/* Set open timer */
tid = start_thread_timer(dcr->jcr, pthread_self(), timeout);
}
Dmsg2(100, "Try open %s mode=%s\n", print_name(), mode_to_str(omode));
#if defined(HAVE_WIN32)
/*
* Windows Code
*/
if ((m_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
*/
m_fd = d_open(dev_name, oflags | O_NONBLOCK, 0);
if (m_fd < 0) {
berrno be;
dev_errno = errno;
Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n",
print_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 (is_tape() && (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0)) {
berrno be;
dev_errno = errno; /* set error status from rewind */
d_close(m_fd);
clear_opened();
Dmsg2(100, "Rewind error on %s close: ERR=%s\n", print_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(m_fd);
m_fd = d_open(dev_name, oflags, 0); /* open normally */
if (m_fd < 0) {
berrno be;
dev_errno = errno;
Dmsg5(100, "Open error on %s omode=%d oflags=%x errno=%d: ERR=%s\n",
print_name(), omode, oflags, errno, be.bstrerror());
break;
}
dev_errno = 0;
lock_door();
set_os_device_parameters(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 (!is_open()) {
berrno be;
Mmsg2(errmsg, _("Unable to open device %s: ERR=%s\n"),
print_name(), be.bstrerror(dev_errno));
Dmsg1(100, "%s", errmsg);
}
/*
* Stop any open() timer we started
*/
if (tid) {
stop_thread_timer(tid);
tid = 0;
}
Dmsg1(100, "open dev: tape %d opened\n", m_fd);
}
/*
* Open a file device.
*/
void DEVICE::open_file_device(DCR *dcr, int omode)
{
POOL_MEM archive_name(PM_FNAME);
get_autochanger_loaded_slot(dcr);
/*
* Handle opening of File Archive (not a tape)
*/
pm_strcpy(archive_name, dev_name);
/*
* If this is a virtual autochanger (i.e. changer_res != NULL)
* we simply use the device name, assuming it has been
* appropriately setup by the "autochanger".
*/
if (!device->changer_res || device->changer_command[0] == 0) {
if (VolCatInfo.VolCatName[0] == 0) {
Mmsg(errmsg, _("Could not open file device %s. No Volume name given.\n"),
print_name());
clear_opened();
return;
}
if (!IsPathSeparator(archive_name.c_str()[strlen(archive_name.c_str())-1])) {
pm_strcat(archive_name, "/");
}
pm_strcat(archive_name, getVolCatName());
}
mount(dcr, 1); /* do mount if required */
open_mode = omode;
set_mode(omode);
/*
* If creating file, give 0640 permissions
*/
Dmsg3(100, "open disk: mode=%s open(%s, 0x%x, 0640)\n", mode_to_str(omode),
archive_name.c_str(), oflags);
if ((m_fd = d_open(archive_name.c_str(), oflags, 0640)) < 0) {
berrno be;
dev_errno = errno;
Mmsg2(errmsg, _("Could not open: %s, ERR=%s\n"), archive_name.c_str(),
be.bstrerror());
Dmsg1(100, "open failed: %s", errmsg);
}
if (m_fd >= 0) {
dev_errno = 0;
file = 0;
file_addr = 0;
}
Dmsg1(100, "open dev: disk fd=%d opened\n", m_fd);
}
/*
* Rewind the device.
* Returns: true on success
* false on failure
*/
bool DEVICE::rewind(DCR *dcr)
{
struct mtop mt_com;
unsigned int i;
bool first = true;
Dmsg3(400, "rewind res=%d fd=%d %s\n", num_reserved(), m_fd, print_name());
state &= ~(ST_EOT|ST_EOF|ST_WEOT); /* remove EOF/EOT flags */
block_num = file = 0;
file_size = 0;
file_addr = 0;
if (m_fd < 0) {
return false;
}
if (is_tape()) {
mt_com.mt_op = MTREW;
mt_com.mt_count = 1;
/* If we get an I/O error on rewind, it is probably because
* the drive is actually busy. We loop for (about 5 minutes)
* retrying every 5 seconds.
*/
for (i=max_rewind_wait; ; i -= 5) {
if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
berrno be;
clrerror(MTREW);
if (i == max_rewind_wait) {
Dmsg1(200, "Rewind error, %s. retrying ...\n", be.bstrerror());
}
/*
* This is a gross hack, because if the user has the
* device mounted (i.e. open), then uses mtx to load
* a tape, the current open file descriptor is invalid.
* So, we close the drive and re-open it.
*/
if (first && dcr) {
int oo_mode = open_mode;
d_close(m_fd);
clear_opened();
open(dcr, oo_mode);
if (m_fd < 0) {
return false;
}
first = false;
continue;
}
#ifdef HAVE_SUN_OS
if (dev_errno == EIO) {
Mmsg1(errmsg, _("No tape loaded or drive offline on %s.\n"), print_name());
return false;
}
#else
if (dev_errno == EIO && i > 0) {
Dmsg0(200, "Sleeping 5 seconds.\n");
bmicrosleep(5, 0);
continue;
}
#endif
Mmsg2(errmsg, _("Rewind error on %s. ERR=%s.\n"),
print_name(), be.bstrerror());
return false;
}
break;
}
} else if (is_file()) {
if (lseek(dcr, (boffset_t)0, SEEK_SET) < 0) {
berrno be;
dev_errno = errno;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
print_name(), be.bstrerror());
return false;
}
} else if (is_fifo() || is_vtl()) {
return true;
} else {
Mmsg1(errmsg, _("Don't know how to rewind device of type %d\n"), dev_type);
return false;
}
return true;
}
/*
* Called to indicate that we have just read an
* EOF from the device.
*/
void DEVICE::set_ateof()
{
set_eof();
if (is_tape()) {
file++;
}
file_addr = 0;
file_size = 0;
block_num = 0;
}
/*
* Called to indicate we are now at the end of the tape, and
* writing is not possible.
*/
void DEVICE::set_ateot()
{
/* Make tape effectively read-only */
state |= (ST_EOF|ST_EOT|ST_WEOT);
clear_append();
}
/*
* Position device to end of medium (end of data)
* Returns: true on succes
* false on error
*/
bool DEVICE::eod(DCR *dcr)
{
struct mtop mt_com;
bool ok = true;
boffset_t pos;
int32_t os_file;
if (m_fd < 0) {
dev_errno = EBADF;
Mmsg1(errmsg, _("Bad call to eod. Device %s not open\n"), print_name());
return false;
}
#if defined (__digital__) && defined (__unix__)
return fsf(VolCatInfo.VolCatFiles);
#endif
Dmsg0(100, "Enter eod\n");
if (at_eot()) {
return true;
}
clear_eof(); /* remove EOF flag */
block_num = file = 0;
file_size = 0;
file_addr = 0;
if (is_fifo()) {
return true;
}
if (!is_tape()) {
pos = lseek(dcr, (boffset_t)0, SEEK_END);
Dmsg1(200, "====== Seek to %lld\n", pos);
if (pos >= 0) {
update_pos(dcr);
set_eot();
return true;
}
dev_errno = errno;
berrno be;
Mmsg2(errmsg, _("lseek error on %s. ERR=%s.\n"),
print_name(), be.bstrerror());
Dmsg0(100, errmsg);
return false;
}
#ifdef MTEOM
if (has_cap(CAP_FASTFSF) && !has_cap(CAP_EOM)) {
Dmsg0(100,"Using FAST FSF for EOM\n");
/* If unknown position, rewind */
if (get_os_tape_file() < 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 (has_cap(CAP_MTIOCGET) && (has_cap(CAP_FASTFSF) || has_cap(CAP_EOM))) {
if (has_cap(CAP_EOM)) {
Dmsg0(100,"Using EOM for EOM\n");
mt_com.mt_op = MTEOM;
mt_com.mt_count = 1;
}
if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
berrno be;
clrerror(mt_com.mt_op);
Dmsg1(50, "ioctl error: %s\n", be.bstrerror());
update_pos(dcr);
Mmsg2(errmsg, _("ioctl MTEOM error on %s. ERR=%s.\n"),
print_name(), be.bstrerror());
Dmsg0(100, errmsg);
return false;
}
os_file = get_os_tape_file();
if (os_file < 0) {
berrno be;
clrerror(-1);
Mmsg2(errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
print_name(), be.bstrerror());
Dmsg0(100, errmsg);
return false;
}
Dmsg1(100, "EOD file=%d\n", os_file);
set_ateof();
file = os_file;
} else {
#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; !at_eot(); 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 (!at_eot() && file_num == (int)file) {
Dmsg1(100, "fsf did not advance from file %d\n", file_num);
set_ateof();
os_file = get_os_tape_file();
if (os_file >= 0) {
Dmsg2(100, "Adjust file from %d to %d\n", file_num, os_file);
file = os_file;
}
break;
}
}
}
/*
* Some drivers leave us after second EOF when doing
* MTEOM, so we must backup so that appending overwrites
* the second EOF.
*/
if (has_cap(CAP_BSFATEOM)) {
/* Backup over EOF */
ok = bsf(1);
/* If BSF worked and fileno is known (not -1), set file */
os_file = get_os_tape_file();
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 {
update_pos(dcr); /* update position */
}
Dmsg1(200, "EOD dev->file=%d\n", file);
return ok;
}
/*
* Set the position of the device -- only for files.
* For other devices, there is no generic way to do it.
*
* Returns: true on succes
* false on error
*/
bool DEVICE::update_pos(DCR *dcr)
{
boffset_t pos;
bool ok = true;
if (!is_open()) {
dev_errno = EBADF;
Mmsg0(errmsg, _("Bad device call. Device not open\n"));
Emsg1(M_FATAL, 0, "%s", errmsg);
return false;