forked from zmanda/amanda
/
ndmp-device.c
1965 lines (1622 loc) · 52.5 KB
/
ndmp-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) 2009, 2010 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 version 2 as published
* by the Free Software Foundation.
*
* 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 "util.h"
#include "device.h"
#include "directtcp.h"
#include "ndmlib.h"
#include "ndmpconnobj.h"
/*
* Type checking and casting macros
*/
#define TYPE_NDMP_DEVICE (ndmp_device_get_type())
#define NDMP_DEVICE(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), ndmp_device_get_type(), NdmpDevice)
#define NDMP_DEVICE_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), ndmp_device_get_type(), NdmpDevice const)
#define NDMP_DEVICE_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), ndmp_device_get_type(), NdmpDeviceClass)
#define IS_NDMP_DEVICE(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), ndmp_device_get_type ())
#define NDMP_DEVICE_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), ndmp_device_get_type(), NdmpDeviceClass)
static GType ndmp_device_get_type (void);
/*
* Main object structure
*/
typedef struct NdmpDevice_ NdmpDevice;
struct NdmpDevice_ {
Device __parent__;
NDMPConnection *ndmp;
/* true if tape service is open on the NDMP server */
gboolean tape_open;
/* addresses the object is listening on, and how the connection
* was opened */
DirectTCPAddr *listen_addrs;
gboolean for_writing;
/* Current DirectTCPConnectionNDMP */
struct DirectTCPConnectionNDMP_ *directtcp_conn;
/* constructor parameters and properties */
gchar *ndmp_hostname;
gint ndmp_port;
gchar *ndmp_device_name;
gchar *ndmp_username;
gchar *ndmp_password;
gchar *ndmp_auth;
gboolean verbose;
gsize read_block_size;
};
/*
* Class definition
*/
typedef struct NdmpDeviceClass_ NdmpDeviceClass;
struct NdmpDeviceClass_ {
DeviceClass __parent__;
};
/*
* A directtcp connection subclass representing a running mover on the other end of
* the given NDMP connection
*/
#define TYPE_DIRECTTCP_CONNECTION_NDMP (directtcp_connection_ndmp_get_type())
#define DIRECTTCP_CONNECTION_NDMP(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMP)
#define DIRECTTCP_CONNECTION_NDMP_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMP const)
#define DIRECTTCP_CONNECTION_NDMP_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMPClass)
#define IS_DIRECTTCP_CONNECTION_NDMP(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), directtcp_connection_ndmp_get_type ())
#define DIRECTTCP_CONNECTION_NDMP_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), directtcp_connection_ndmp_get_type(), DirectTCPConnectionNDMPClass)
GType directtcp_connection_ndmp_get_type(void);
typedef struct DirectTCPConnectionNDMP_ {
DirectTCPConnection __parent__;
/* NDMP connection controlling the mover */
NDMPConnection *ndmp;
/* mode for this operation */
ndmp9_mover_mode mode;
/* last reported mover position in the datastream */
guint64 offset;
} DirectTCPConnectionNDMP;
typedef struct DirectTCPConnectionNDMPClass_ {
DirectTCPConnectionClass __parent__;
} DirectTCPConnectionNDMPClass;
static DirectTCPConnectionNDMP *directtcp_connection_ndmp_new(
NDMPConnection *ndmp,
ndmp9_mover_mode mode);
/*
* Constants and static data
*/
#define NDMP_DEVICE_NAME "ndmp"
/* pointer to the class of our parent */
static DeviceClass *parent_class = NULL;
/* robust_write results */
typedef enum {
ROBUST_WRITE_OK,
ROBUST_WRITE_OK_LEOM,
ROBUST_WRITE_ERROR, /* device error already set */
ROBUST_WRITE_NO_SPACE
} robust_write_result;
/*
* device-specific properties
*/
/* Authentication information for NDMP agent. Both of these are strings. */
static DevicePropertyBase device_property_ndmp_username;
static DevicePropertyBase device_property_ndmp_password;
static DevicePropertyBase device_property_ndmp_auth;
#define PROPERTY_NDMP_USERNAME (device_property_ndmp_username.ID)
#define PROPERTY_NDMP_PASSWORD (device_property_ndmp_password.ID)
#define PROPERTY_NDMP_AUTH (device_property_ndmp_auth.ID)
/*
* prototypes
*/
void ndmp_device_register(void);
static void set_error_from_ndmp(NdmpDevice *self);
#define ndmp_device_read_size(self) \
(((NdmpDevice *)(self))->read_block_size? \
((NdmpDevice *)(self))->read_block_size : ((Device *)(self))->block_size)
/*
* Utility functions
*/
static gboolean
open_connection(
NdmpDevice *self)
{
if (!self->ndmp) {
self->ndmp = ndmp_connection_new(
self->ndmp_hostname,
self->ndmp_port,
self->ndmp_username,
self->ndmp_password,
self->ndmp_auth);
if (ndmp_connection_err_code(self->ndmp)) {
char *errmsg = ndmp_connection_err_msg(self->ndmp);
device_set_error(DEVICE(self),
g_strdup_printf("could not connect to ndmp-server '%s:%d': %s",
self->ndmp_hostname, self->ndmp_port, errmsg),
DEVICE_STATUS_DEVICE_ERROR);
g_object_unref(self->ndmp);
self->ndmp = NULL;
return FALSE;
}
if (self->verbose)
ndmp_connection_set_verbose(self->ndmp, TRUE);
self->tape_open = FALSE;
}
return TRUE;
}
static void
close_connection(
NdmpDevice *self)
{
/* note that this does not send NDMP_TAPE_CLOSE, as it's used in error
* situations too */
if (self->ndmp) {
g_object_unref(self->ndmp);
self->ndmp = NULL;
self->tape_open = FALSE;
}
}
static gboolean
open_tape_agent(
NdmpDevice *self)
{
guint64 file_num, blockno, blocksize;
/* if already open, stop now */
if (self->tape_open) {
return TRUE;
}
if (!open_connection(self)) {
/* error message set by open_connection */
return FALSE;
}
g_debug("opening tape device '%s' on NDMP server '%s:%d'",
self->ndmp_device_name, self->ndmp_hostname, self->ndmp_port);
/* send NDMP_TAPE_OPEN, using RAW mode so that it will open even with no tape */
if (!ndmp_connection_tape_open(self->ndmp,
self->ndmp_device_name, NDMP9_TAPE_RAW_MODE)) {
set_error_from_ndmp(self);
return FALSE;
}
/* check that the block sizes match */
if (!ndmp_connection_tape_get_state(self->ndmp,
&blocksize, &file_num, &blockno)) {
set_error_from_ndmp(self);
return FALSE;
}
if (blocksize != 0 && blocksize != DEVICE(self)->block_size) {
device_set_error(DEVICE(self),
g_strdup_printf("NDMP device has fixed block size %ju, but Amanda "
"device is configured with blocksize %ju", (uintmax_t)blocksize,
(uintmax_t)(DEVICE(self)->block_size)),
DEVICE_STATUS_DEVICE_ERROR);
}
self->tape_open = TRUE;
return TRUE;
}
static gboolean
close_tape_agent(
NdmpDevice *self)
{
if (self->tape_open) {
g_debug("closing tape device '%s' on NDMP server '%s:%d'",
self->ndmp_device_name, self->ndmp_hostname, self->ndmp_port);
self->tape_open = FALSE; /* count it as closed even if there is an error */
if (!ndmp_connection_tape_close(self->ndmp)) {
set_error_from_ndmp(self);
return FALSE;
}
}
return TRUE;
}
static gboolean
single_ndmp_mtio(
NdmpDevice *self,
ndmp9_tape_mtio_op tape_op)
{
guint resid;
if (!ndmp_connection_tape_mtio(self->ndmp, tape_op, 1, &resid)) {
set_error_from_ndmp(self);
return FALSE;
}
if (resid != 0) {
device_set_error(DEVICE(self),
g_strdup_printf("NDMP MTIO operation %d did not complete", tape_op),
DEVICE_STATUS_DEVICE_ERROR);
}
return TRUE;
}
/* get the tape state straight from the device; we try to track these things
* accurately in the device, but sometimes it's good to check. */
static gboolean
ndmp_get_state(
NdmpDevice *self)
{
Device *dself = DEVICE(self);
guint64 file_num, blockno, blocksize;
if (!ndmp_connection_tape_get_state(self->ndmp,
&blocksize, &file_num, &blockno)) {
set_error_from_ndmp(self);
return FALSE;
}
g_assert(file_num < INT_MAX);
dself->file = (int)file_num;
dself->block = blockno;
return TRUE;
}
static robust_write_result
robust_write(
NdmpDevice *self,
char *buf,
guint64 count)
{
guint64 actual;
robust_write_result subresult;
if (!ndmp_connection_tape_write(self->ndmp, buf, count, &actual)) {
switch (ndmp_connection_err_code(self->ndmp)) {
case NDMP9_IO_ERR:
/* We encountered PEOM; this only happens when the caller ignores
* LEOM */
return ROBUST_WRITE_NO_SPACE;
case NDMP9_EOM_ERR:
/* We encountered LEOM; retry the write (which should succeed) */
subresult = robust_write(self, buf, count);
if (subresult != ROBUST_WRITE_OK)
return subresult;
g_debug("ndmp device hit logical EOM");
return ROBUST_WRITE_OK_LEOM;
default:
set_error_from_ndmp(self);
return ROBUST_WRITE_ERROR;
}
}
g_assert(count == actual);
return ROBUST_WRITE_OK;
}
static void
set_error_from_ndmp(
NdmpDevice *self)
{
/* translate some error codes to the corresponding Device API status */
switch (ndmp_connection_err_code(self->ndmp)) {
case NDMP9_NO_TAPE_LOADED_ERR:
device_set_error(DEVICE(self),
g_strdup(_("no tape loaded")),
DEVICE_STATUS_VOLUME_MISSING);
break;
case NDMP9_DEVICE_BUSY_ERR:
device_set_error(DEVICE(self),
g_strdup(_("device busy")),
DEVICE_STATUS_DEVICE_BUSY);
break;
case NDMP9_IO_ERR:
device_set_error(DEVICE(self),
g_strdup(_("IO error")),
DEVICE_STATUS_VOLUME_UNLABELED |
DEVICE_STATUS_VOLUME_ERROR |
DEVICE_STATUS_DEVICE_ERROR);
break;
default:
device_set_error(DEVICE(self),
ndmp_connection_err_msg(self->ndmp),
DEVICE_STATUS_DEVICE_ERROR);
break;
}
close_connection(self);
}
/*
* Virtual function overrides
*/
static void
ndmp_device_open_device(
Device *dself,
char *device_name,
char *device_type,
char *device_node)
{
NdmpDevice *self = NDMP_DEVICE(dself);
char *colon, *at;
/* first, extract the various parts of the device_node:
* HOST[:PORT]@DEVICE */
colon = strchr(device_node, ':');
at = strchr(device_node, '@');
if (colon > at)
colon = NULL; /* :PORT only counts if it's before the device name */
if (!at) {
device_set_error(dself,
g_strdup_printf("invalid ndmp device name '%s'", device_name),
DEVICE_STATUS_DEVICE_ERROR);
return;
}
if (colon) {
char *p = NULL;
long port = strtol(colon+1, &p, 10);
if (port < 0 || port >= 65536 || p != at || (!port && EINVAL == errno)) {
device_set_error(dself,
g_strdup_printf("invalid ndmp port in device name '%s'",
device_name),
DEVICE_STATUS_DEVICE_ERROR);
return;
}
self->ndmp_port = (gint)port;
self->ndmp_hostname = g_strndup(device_node, colon-device_node);
} else {
self->ndmp_port = 0; /* (use ndmjob's default, 10000) */
self->ndmp_hostname = g_strndup(device_node, at-device_node);
}
self->ndmp_device_name = g_strdup(at+1);
if (parent_class->open_device) {
parent_class->open_device(dself, device_name, device_type, device_node);
}
}
static void ndmp_device_finalize(GObject * obj_self)
{
NdmpDevice *self = NDMP_DEVICE (obj_self);
if(G_OBJECT_CLASS(parent_class)->finalize)
(* G_OBJECT_CLASS(parent_class)->finalize)(obj_self);
(void)close_tape_agent(self); /* ignore any error */
if (self->directtcp_conn)
g_object_unref(self->directtcp_conn);
if (self->listen_addrs)
g_free(self->listen_addrs);
close_connection(self);
if (self->ndmp_hostname)
g_free(self->ndmp_hostname);
if (self->ndmp_device_name)
g_free(self->ndmp_device_name);
if (self->ndmp_username)
g_free(self->ndmp_username);
if (self->ndmp_password)
g_free(self->ndmp_password);
if (self->ndmp_auth)
g_free(self->ndmp_auth);
}
static DeviceStatusFlags
ndmp_device_read_label(
Device *dself)
{
NdmpDevice *self = NDMP_DEVICE(dself);
dumpfile_t *header = NULL;
gpointer buf = NULL;
guint64 buf_size = 0;
gsize read_block_size = 0;
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 (!open_tape_agent(self)) {
/* error status was set by open_tape_agent */
return dself->status;
}
if (!single_ndmp_mtio(self, NDMP9_MTIO_REW)) {
/* error message, if any, is set by single_ndmp_mtio */
return dself->status;
}
/* read the tape header from the NDMP server */
dself->status = 0;
read_block_size = ndmp_device_read_size(self);
buf = g_malloc(read_block_size);
if (!ndmp_connection_tape_read(self->ndmp,
buf,
read_block_size,
&buf_size)) {
/* handle known errors */
switch (ndmp_connection_err_code(self->ndmp)) {
case NDMP9_NO_TAPE_LOADED_ERR:
device_set_error(dself,
g_strdup(_("no tape loaded")),
DEVICE_STATUS_VOLUME_MISSING);
goto read_err;
case NDMP9_IO_ERR:
device_set_error(dself,
g_strdup(_("IO error reading tape label")),
DEVICE_STATUS_VOLUME_UNLABELED |
DEVICE_STATUS_VOLUME_ERROR |
DEVICE_STATUS_DEVICE_ERROR);
goto read_err;
case NDMP9_EOM_ERR:
case NDMP9_EOF_ERR:
device_set_error(dself,
g_strdup(_("no tape label found")),
DEVICE_STATUS_VOLUME_UNLABELED);
header = dself->volume_header = g_new(dumpfile_t, 1);
fh_init(header);
goto read_err;
default:
set_error_from_ndmp(self);
goto read_err;
}
}
header = dself->volume_header = g_new(dumpfile_t, 1);
fh_init(header);
parse_file_header(buf, header, buf_size);
read_err:
g_free(buf);
if (dself->status != 0) {
/* error already set above */
return dself->status;
}
/* handle a "weird" label */
if (header->type != F_TAPESTART) {
device_set_error(dself,
stralloc(_("No tapestart header -- unlabeled device?")),
DEVICE_STATUS_VOLUME_UNLABELED);
return dself->status;
}
dself->volume_label = g_strdup(header->name);
dself->volume_time = g_strdup(header->datestamp);
/* dself->volume_header is already set */
/* note: connection is left open, as well as the tape device */
device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
return dself->status;
}
static gboolean
ndmp_device_start(
Device *dself,
DeviceAccessMode mode,
char *label,
char *timestamp)
{
NdmpDevice *self = NDMP_DEVICE(dself);
dumpfile_t *header;
char *header_buf;
self = NDMP_DEVICE(dself);
if (device_in_error(self)) return FALSE;
if (!open_tape_agent(self)) {
/* error status was set by open_tape_agent */
return FALSE;
}
if (mode != ACCESS_WRITE && dself->volume_label == NULL) {
if (ndmp_device_read_label(dself) != DEVICE_STATUS_SUCCESS)
/* the error was set by ndmp_device_read_label */
return FALSE;
}
dself->access_mode = mode;
dself->in_file = FALSE;
if (!single_ndmp_mtio(self, NDMP9_MTIO_REW)) {
/* single_ndmp_mtio already set our error message */
return FALSE;
}
/* Position the tape */
switch (mode) {
case ACCESS_APPEND:
device_set_error(dself,
g_strdup("operation not supported"),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
break;
case ACCESS_READ:
dself->file = 0;
break;
case ACCESS_WRITE:
header = make_tapestart_header(dself, label, timestamp);
g_assert(header != NULL);
header_buf = device_build_amanda_header(dself, header, NULL);
if (header_buf == NULL) {
device_set_error(dself,
stralloc(_("Tapestart header won't fit in a single block!")),
DEVICE_STATUS_DEVICE_ERROR);
dumpfile_free(header);
return FALSE;
}
switch (robust_write(self, header_buf, dself->block_size)) {
case ROBUST_WRITE_OK_LEOM:
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_OK:
break;
case ROBUST_WRITE_NO_SPACE:
/* this would be an odd error to see writing the tape label, but
* oh well */
device_set_error(dself,
stralloc(_("No space left on device")),
DEVICE_STATUS_VOLUME_ERROR);
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_ERROR:
/* error was set by robust_write or above */
dumpfile_free(header);
amfree(header_buf);
return FALSE;
}
amfree(header_buf);
if (!single_ndmp_mtio(self, NDMP9_MTIO_EOF)) {
/* error was set by single_ndmp_mtio */
dumpfile_free(header);
return FALSE;
}
dself->volume_label = newstralloc(dself->volume_label, label);
dself->volume_time = newstralloc(dself->volume_time, timestamp);
dumpfile_free(dself->volume_header);
dself->volume_header = header;
/* unset the VOLUME_UNLABELED flag, if it was set */
device_set_error(dself, NULL, DEVICE_STATUS_SUCCESS);
dself->file = 0;
break;
default:
g_assert_not_reached();
}
return TRUE;
}
static gboolean
ndmp_device_finish(
Device *dself)
{
gboolean rval;
NdmpDevice *self = NDMP_DEVICE(dself);
rval = !device_in_error(dself);
/* we're not in a file anymore */
dself->access_mode = ACCESS_NULL;
if (!close_tape_agent(self)) {
/* error is set by close_tape_agent */
rval = FALSE;
}
if (self->ndmp)
close_connection(self);
return rval;
}
static gboolean
ndmp_device_eject(
Device *dself)
{
NdmpDevice *self = NDMP_DEVICE(dself);
if (device_in_error(dself)) return FALSE;
if (!single_ndmp_mtio(self, NDMP9_MTIO_OFF)) {
/* error was set by single_ndmp_mtio */
return FALSE;
}
return TRUE;
}
/* functions for writing */
static gboolean
ndmp_device_start_file(
Device *dself,
dumpfile_t *header)
{
NdmpDevice *self = NDMP_DEVICE(dself);
char *header_buf;
if (device_in_error(self)) return FALSE;
dself->is_eof = FALSE;
dself->is_eom = FALSE;
/* set the blocksize in the header properly */
header->blocksize = dself->block_size;
header_buf = device_build_amanda_header(dself, header, NULL);
if (header_buf == NULL) {
device_set_error(dself,
stralloc(_("Amanda file header won't fit in a single block!")),
DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
}
switch (robust_write(self, header_buf, dself->block_size)) {
case ROBUST_WRITE_OK_LEOM:
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_OK:
break;
case ROBUST_WRITE_NO_SPACE:
/* this would be an odd error to see writing the tape label, but
* oh well */
device_set_error(dself,
stralloc(_("No space left on device")),
DEVICE_STATUS_VOLUME_ERROR);
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_ERROR:
/* error was set by robust_write or above */
amfree(header_buf);
return FALSE;
}
amfree(header_buf);
/* arrange the file numbers correctly */
dself->in_file = TRUE;
if (!ndmp_get_state(self)) {
/* error already set by ndmp_get_state */
return FALSE;
}
/* double-check that the tape agent gave us a non-bogus file number */
g_assert(dself->file > 0);
return TRUE;
}
static gboolean
ndmp_device_write_block(
Device *dself,
guint size,
gpointer data)
{
NdmpDevice *self = NDMP_DEVICE(dself);
gpointer replacement_buffer = NULL;
if (device_in_error(self)) return FALSE;
/* zero out to the end of a short block -- tape devices only write
* whole blocks. */
if (size < dself->block_size) {
replacement_buffer = malloc(dself->block_size);
memcpy(replacement_buffer, data, size);
bzero(replacement_buffer+size, dself->block_size-size);
data = replacement_buffer;
size = dself->block_size;
}
switch (robust_write(self, data, size)) {
case ROBUST_WRITE_OK_LEOM:
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_OK:
break;
case ROBUST_WRITE_NO_SPACE:
/* this would be an odd error to see writing the tape label, but
* oh well */
device_set_error(dself,
stralloc(_("No space left on device")),
DEVICE_STATUS_VOLUME_ERROR);
dself->is_eom = TRUE;
/* fall through */
case ROBUST_WRITE_ERROR:
/* error was set by robust_write or above */
if (replacement_buffer) g_free(replacement_buffer);
return FALSE;
}
dself->block++;
if (replacement_buffer) g_free(replacement_buffer);
return TRUE;
}
static gboolean
ndmp_device_finish_file(
Device *dself)
{
NdmpDevice *self = NDMP_DEVICE(dself);
if (device_in_error(dself)) return FALSE;
/* we're not in a file anymore */
dself->in_file = FALSE;
if (!single_ndmp_mtio(self, NDMP9_MTIO_EOF)) {
/* error was set by single_ndmp_mtio */
dself->is_eom = TRUE;
return FALSE;
}
return TRUE;
}
/* functions for reading */
static dumpfile_t*
ndmp_device_seek_file(
Device *dself,
guint file)
{
NdmpDevice *self = NDMP_DEVICE(dself);
gint delta;
guint resid;
gpointer buf;
guint64 buf_size;
dumpfile_t *header;
gsize read_block_size = 0;
if (device_in_error(dself)) return FALSE;
/* file 0 is the tape header, and isn't seekable as a distinct
* Device-API-level file */
if (file == 0) {
device_set_error(dself,
g_strdup("cannot seek to file 0"),
DEVICE_STATUS_DEVICE_ERROR);
return NULL;
}
/* first, make sure the file and block numbers are correct */
if (!ndmp_get_state(self)) {
/* error already set by ndmp_get_state */
return FALSE;
}
/* now calculate the file delta */
delta = file - dself->file;
if (delta <= 0) {
/* Note that this algorithm will rewind to the beginning of
* the current part, too */
/* BSF *past* the filemark we want to seek to */
if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_BSF, -delta + 1, &resid)) {
set_error_from_ndmp(self);
return NULL;
}
if (resid != 0)
goto incomplete_bsf;
/* now we are on the BOT side of the filemark, but we want to be
* on the EOT side of it. An FSF will get us there.. */
if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_FSF, 1, &resid)) {
set_error_from_ndmp(self);
return NULL;
}
if (resid != 0) {
incomplete_bsf:
device_set_error(dself,
g_strdup_printf("BSF operation failed to seek by %d files", resid),
DEVICE_STATUS_DEVICE_ERROR);
return NULL;
}
} else /* (delta > 0) */ {
if (!ndmp_connection_tape_mtio(self->ndmp, NDMP9_MTIO_FSF, delta, &resid)) {
set_error_from_ndmp(self);
return FALSE;
}
/* if we didn't seek all the way there, then we're past the tapeend */
if (resid > 0) {
device_set_error(dself,
vstrallocf(_("Could not seek forward to file %d"), file),
DEVICE_STATUS_VOLUME_ERROR);
return NULL;
}
}
/* fix up status */
dself->in_file = TRUE;
dself->file = file;
dself->block = 0;
/* now read the header */
read_block_size = ndmp_device_read_size(self);
buf = g_malloc(read_block_size);
if (!ndmp_connection_tape_read(self->ndmp,
buf, read_block_size, &buf_size)) {
switch (ndmp_connection_err_code(self->ndmp)) {
case NDMP9_EOF_ERR:
case NDMP9_EOM_ERR:
return make_tapeend_header();
default:
set_error_from_ndmp(self);
g_free(buf);
return NULL;
}
}
header = g_new(dumpfile_t, 1);
fh_init(header);
parse_file_header(buf, header, buf_size);
g_free(buf);
return header;
}
static gboolean
ndmp_device_seek_block(
Device *dself,
guint64 block)
{
if (device_in_error(dself)) return FALSE;
dself->block = block;
device_set_error(dself, g_strdup("operation not supported"), DEVICE_STATUS_DEVICE_ERROR);
return FALSE;
}
static int
ndmp_device_read_block (Device * dself, gpointer data, int *size_req) {
NdmpDevice *self = NDMP_DEVICE(dself);
guint64 requested, actual;
gsize read_block_size = ndmp_device_read_size(self);
/* We checked the NDMP device's blocksize when the device was opened, which should
* catch any misalignent of server block size and Amanda block size */
g_assert(read_block_size < INT_MAX); /* check data type mismatch */
if (!data || *size_req < (int)(read_block_size)) {
*size_req = (int)(read_block_size);
return 0;
}
requested = *size_req;
if (!ndmp_connection_tape_read(self->ndmp,
data,
requested,
&actual)) {
/* handle known errors */
switch (ndmp_connection_err_code(self->ndmp)) {
case NDMP9_EOM_ERR:
case NDMP9_EOF_ERR:
dself->is_eof = TRUE;
return -1;
default:
set_error_from_ndmp(self);
return -1;
}
}
*size_req = (int)actual; /* cast is OK - requested size was < INT_MAX too */
return *size_req;
}
static gboolean
listen_impl(
Device *dself,
gboolean for_writing,
DirectTCPAddr **addrs)
{