/
ndmp_tape.c
1538 lines (1325 loc) · 42.2 KB
/
ndmp_tape.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) 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.
*/
/*
* ndmp_tape.c implements the NDMP TAPE service which interfaces to
* the internal Bareos infrstructure. This is implemented as a seperate
* daemon protocol on a different port (10000 NDMP by default) which
* interfaces to the standard Bareos storage daemon at the record level.
*
* E.g. normal data from a FD comes via the 9103 port and then get turned
* into records for NDMP packets travel via the NDMP protocol library
* which is named libbareosndmp and the data gets turned into native Bareos
* tape records.
*
* Marco van Wieringen, May 2012
*/
#include "bareos.h"
#if HAVE_NDMP
#include "stored.h"
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef HAVE_ARPA_NAMESER_H
#include <arpa/nameser.h>
#endif
#ifdef HAVE_LIBWRAP
#include "tcpd.h"
#endif
#ifdef HAVE_POLL_H
#include <poll.h>
#elif HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
#include "ndmp/ndmagents.h"
/*
* Structure used to pass arguments to the ndmp_thread_server thread
* via a void * argument. Things like the addresslist, maximum number
* of clients and the client workqueue to use are passed using this
* structure.
*/
struct ndmp_thread_server_args {
dlist *addr_list;
int max_clients;
workq_t *client_wq;
};
/*
* Internal structure to keep track of private data for a NDMP session.
* Referenced via (struct ndm_session)->session_handle.
*/
struct ndmp_session_handle {
int fd; /* Socket file descriptor */
char *host; /* Host name/IP */
int port; /* Local port */
struct sockaddr client_addr; /* Client's IP address */
struct sockaddr_in peer_addr; /* Peer's IP address */
JCR *jcr; /* Internal JCR bound to this NDMP session */
};
/*
* Internal structure to keep track of private data.
*/
struct ndmp_internal_state {
uint32_t LogLevel;
JCR *jcr;
};
typedef struct ndmp_internal_state NIS;
/* Static globals */
static bool quit = false;
static bool ndmp_initialized = false;
static pthread_t ndmp_tid;
static struct ndmp_thread_server_args ndmp_thread_server_args;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* Forward referenced functions */
static inline int native_to_ndmp_loglevel(int debuglevel, NIS *nis)
{
unsigned int level;
memset(nis, 0, sizeof(NIS));
/*
* Lookup the initial default log_level from the default STORES.
*/
nis->LogLevel = me->ndmploglevel;
/*
* NDMP loglevels run from 0 - 9 so we take a look at the
* current debug level and divide it by 100 to get a proper
* value. If the debuglevel is below the wanted initial level
* we set the loglevel to the wanted initial level. As the
* debug logging takes care of logging messages that are
* unwanted we can set the loglevel higher and still don't
* get debug messages.
*/
level = debuglevel / 100;
if (level < nis->LogLevel) {
level = nis->LogLevel;
}
/*
* Make sure the level is in the wanted range.
*/
if (level < 0) {
level = 0;
} else if (level > 9) {
level = 9;
}
return level;
}
/*
* Interface function which glues the logging infra of the NDMP lib with the daemon.
*/
extern "C" void ndmp_loghandler(struct ndmlog *log, char *tag, int level, char *msg)
{
unsigned int internal_level = level * 100;
NIS *nis;
/*
* We don't want any trailing newline in log messages.
*/
strip_trailing_newline(msg);
/*
* Make sure if the logging system was setup properly.
*/
nis = (NIS *)log->ctx;
if (!nis) {
return;
}
/*
* If the log level of this message is under our logging treshold we
* log it as part of the Job.
*/
if ((internal_level / 100) <= nis->LogLevel) {
if (nis->jcr) {
/*
* Look at the tag field to see what is logged.
*/
if (bstrncmp(tag + 1, "LM", 2)) {
/*
* *LM* messages. E.g. log message NDMP protocol msgs.
* First character of the tag is the agent sending the
* message e.g. 'D' == Data Agent
* 'T' == Tape Agent
* 'R' == Robot Agent
* 'C' == Control Agent (DMA)
*
* Last character is the type of message e.g.
* 'n' - normal message
* 'd' - debug message
* 'e' - error message
* 'w' - warning message
* '?' - unknown message level
*/
switch (*(tag + 3)) {
case 'n':
Jmsg(nis->jcr, M_INFO, 0, "%s\n", msg);
break;
case 'e':
Jmsg(nis->jcr, M_ERROR, 0, "%s\n", msg);
break;
case 'w':
Jmsg(nis->jcr, M_WARNING, 0, "%s\n", msg);
break;
case '?':
Jmsg(nis->jcr, M_INFO, 0, "%s\n", msg);
break;
default:
break;
}
} else {
Jmsg(nis->jcr, M_INFO, 0, "%s\n", msg);
}
}
}
/*
* Print any debug message we convert the NDMP level back to an internal
* level and let the normal debug logging handle if it needs to be printed
* or not.
*/
Dmsg3(internal_level, "NDMP: [%s] [%d] %s\n", tag, (internal_level / 100), msg);
}
/*
* Clear text authentication callback.
*/
extern "C" int bndmp_auth_clear(struct ndm_session *sess, char *name, char *pass)
{
NDMPRES *auth_config;
foreach_res(auth_config, R_NDMP) {
/*
* Only consider entries for AT_CLEAR authentication type.
*/
if (auth_config->AuthType != AT_CLEAR) {
continue;
}
if (bstrcmp(name, auth_config->username) &&
bstrcmp(pass, auth_config->password)) {
/*
* See if we need to adjust the logging level.
*/
if (sess->param->log.ctx) {
NIS *nis;
nis = (NIS *)sess->param->log.ctx;
if (nis->LogLevel != auth_config->LogLevel) {
if (auth_config->LogLevel >= 0 && auth_config->LogLevel <= 9) {
nis->LogLevel = auth_config->LogLevel;
}
}
}
return 1;
}
}
return 0;
}
/*
* MD5 authentication callback.
*/
extern "C" int bndmp_auth_md5(struct ndm_session *sess, char *name, char digest[16])
{
NDMPRES *auth_config;
foreach_res(auth_config, R_NDMP) {
/*
* Only consider entries for AT_MD5 authentication type.
*/
if (auth_config->AuthType != AT_MD5) {
continue;
}
if (!bstrcmp(name, auth_config->username)) {
continue;
}
if (!ndmmd5_ok_digest(sess->md5_challenge, auth_config->password, digest)) {
return 0;
}
/*
* See if we need to adjust the logging level.
*/
if (sess->param->log.ctx) {
NIS *nis;
nis = (NIS *)sess->param->log.ctx;
if (nis->LogLevel != auth_config->LogLevel) {
if (auth_config->LogLevel >= 0 && auth_config->LogLevel <= 9) {
nis->LogLevel = auth_config->LogLevel;
}
}
}
return 1;
}
return 0;
}
/*
* Save a record using the native routines.
*/
static inline bool bndmp_write_data_to_block(JCR *jcr,
int stream,
char *data,
u_long data_length)
{
bool retval = false;
DCR *dcr = jcr->dcr;
POOLMEM *rec_data;
/*
* Keep track of the original data buffer and restore it on exit from this function.
*/
rec_data = dcr->rec->data;
dcr->rec->VolSessionId = jcr->VolSessionId;
dcr->rec->VolSessionTime = jcr->VolSessionTime;
dcr->rec->FileIndex = dcr->FileIndex;
dcr->rec->Stream = stream;
dcr->rec->maskedStream = stream & STREAMMASK_TYPE; /* strip high bits */
dcr->rec->data = data;
dcr->rec->data_len = data_length;
if (!dcr->write_record()) {
goto bail_out;
}
if (stream == STREAM_UNIX_ATTRIBUTES) {
dir_update_file_attributes(dcr, dcr->rec);
}
retval = true;
bail_out:
dcr->rec->data = rec_data;
return retval;
}
/*
* Read a record using the native routines.
*
* data_length == 0 = EOF
*/
static inline bool bndmp_read_data_from_block(JCR *jcr,
char *data,
u_long wanted_data_length,
u_long *data_length)
{
DCR *dcr = jcr->read_dcr;
READ_CTX *rctx = jcr->rctx;
bool done = false;
bool ok = true;
if (!rctx) {
return false;
}
while (!done) {
/*
* See if there are any records left to process.
*/
if (!is_block_empty(rctx->rec)) {
if (!read_next_record_from_block(dcr, rctx, &done)) {
/*
* When the done flag is set to true we are done reading all
* records or end of block read next block.
*/
continue;
}
} else {
/*
* Read the next block into our buffers.
*/
if (!read_next_block_from_device(dcr, &rctx->sessrec,
NULL, mount_next_read_volume, &ok)) {
return false;
}
/*
* Get a new record for each Job as defined by VolSessionId and VolSessionTime
*/
if (!rctx->rec ||
rctx->rec->VolSessionId != dcr->block->VolSessionId ||
rctx->rec->VolSessionTime != dcr->block->VolSessionTime) {
read_context_set_record(dcr, rctx);
}
rctx->records_processed = 0;
rctx->rec->state_bits = 0;
rctx->lastFileIndex = READ_NO_FILEINDEX;
if (!read_next_record_from_block(dcr, rctx, &done)) {
/*
* When the done flag is set to true we are done reading all
* records or end of block read next block.
*/
continue;
}
}
/*
* Here we should have read a record from the block which contains some data.
* Its either:
*
* - STREAM_UNIX_ATTRIBUTES
* Which is the start of the dump when we encounter that we just read the next record.
* - STREAM_FILE_DATA
* Normal NDMP data.
* - STREAM_NDMP_SEPERATOR
* End of NDMP data stream.
*
* anything other means a corrupted stream of records and means we give an EOF.
*/
switch (rctx->rec->maskedStream) {
case STREAM_UNIX_ATTRIBUTES:
continue;
case STREAM_FILE_DATA:
if (wanted_data_length < rctx->rec->data_len) {
Jmsg0(jcr, M_FATAL, 0,
_("Data read from volume bigger then NDMP databuffer, please increase the NDMP blocksize.\n"));
return false;
}
memcpy(data, rctx->rec->data, rctx->rec->data_len);
*data_length = rctx->rec->data_len;
return true;
case STREAM_NDMP_SEPERATOR:
*data_length = 0;
return true;
default:
*data_length = 0;
return true;
}
}
if (done) {
*data_length = 0;
}
return true;
}
/*
* Generate virtual file attributes for the whole NDMP stream.
*/
static inline bool bndmp_create_virtual_file(JCR *jcr, char *filename)
{
DCR *dcr = jcr->dcr;
struct stat statp;
time_t now = time(NULL);
char attribs[MAXSTRING];
char data[MAXSTRING];
int32_t size;
memset(&statp, 0, sizeof(statp));
statp.st_mode = 0700 | S_IFREG;
statp.st_ctime = now;
statp.st_mtime = now;
statp.st_atime = now;
statp.st_size = -1;
statp.st_blksize = 4096;
statp.st_blocks = 1;
/*
* Encode a stat structure into an ASCII string.
*/
encode_stat(attribs, &statp, sizeof(statp), dcr->FileIndex, STREAM_UNIX_ATTRIBUTES);
/*
* Generate a file attributes stream.
* File_index
* File type
* Filename (full path)
* Encoded attributes
* Link name (if type==FT_LNK or FT_LNKSAVED)
* Encoded extended-attributes (for Win32)
* Delta Sequence Number
*/
size = bsnprintf(data, sizeof(data), "%ld %d %s%c%s%c%s%c%s%c%d%c",
dcr->FileIndex, /* File_index */
FT_REG, /* File type */
filename, /* Filename (full path) */
0,
attribs, /* Encoded attributes */
0,
"", /* Link name (if type==FT_LNK or FT_LNKSAVED) */
0,
"", /* Encoded extended-attributes (for Win32) */
0,
0, /* Delta Sequence Number */
0);
return bndmp_write_data_to_block(jcr, STREAM_UNIX_ATTRIBUTES, data, size);
}
static int bndmp_simu_flush_weof(struct ndm_session *sess)
{
struct ndm_tape_agent * ta = sess->tape_acb;
if (ta->weof_on_close) {
/* best effort */
ndmos_tape_wfm (sess);
}
return 0;
}
/*
* Search the JCRs for one with the given security key.
*/
static inline JCR *get_jcr_by_security_key(char *security_key)
{
JCR *jcr;
foreach_jcr(jcr) {
if (bstrcmp(jcr->sd_auth_key, security_key)) {
jcr->inc_use_count();
break;
}
}
endeach_jcr(jcr);
return jcr;
}
extern "C" ndmp9_error bndmp_tape_open(struct ndm_session *sess,
char *drive_name,
int will_write)
{
JCR *jcr;
DCR *dcr;
char *filesystem;
struct ndmp_session_handle *handle;
struct ndm_tape_agent *ta;
/*
* The drive_name should be in the form <AuthKey>@<file_system>
*/
if ((filesystem = strchr(drive_name, '@')) == NULL) {
return NDMP9_NO_DEVICE_ERR;
}
/*
* Lookup the jobid the drive_name should contain a valid authentication key.
*/
*filesystem++ = '\0';
if (!(jcr = get_jcr_by_security_key(drive_name))) {
Jmsg1(NULL, M_FATAL, 0, _("NDMP tape open failed: Security Key not found: %s\n"), drive_name);
return NDMP9_NO_DEVICE_ERR;
}
/*
* When we found a JCR with the wanted security key it also implictly
* means the authentication succeeded as the connecting NDMP session
* only knows the exact security key as it was inserted by the director.
*/
jcr->authenticated = true;
/*
* There is a native storage daemon session waiting for the FD to connect.
* In NDMP terms this is the same as a FD connecting so wake any waiting
* threads.
*/
pthread_cond_signal(&jcr->job_start_wait);
/*
* Save the JCR to ndm_session binding so everything furher
* knows which JCR belongs to which NDMP session. We have
* a special ndmp_session_handle which we can use to track
* session specific information.
*/
handle = (struct ndmp_session_handle *)sess->session_handle;
/*
* If we already have a JCR binding for this connection we release it here
* as we are about to establish a new binding (e.g. second NDMP save for
* the same job) and we should end up with the same binding.
*/
if (handle->jcr) {
free_jcr(handle->jcr);
}
handle->jcr = jcr;
/*
* Keep track of the JCR for logging purposes.
*/
if (sess->param->log.ctx) {
NIS *nis;
nis = (NIS *)sess->param->log.ctx;
nis->jcr = jcr;
}
/*
* Depending on the open mode select the right DCR.
*/
if (will_write) {
dcr = jcr->dcr;
} else {
dcr = jcr->read_dcr;
}
if (!dcr) {
Jmsg0(jcr, M_FATAL, 0, _("DCR is NULL!!!\n"));
return NDMP9_NO_DEVICE_ERR;
}
if (!dcr->dev) {
Jmsg0(jcr, M_FATAL, 0, _("DEVICE is NULL!!!\n"));
return NDMP9_NO_DEVICE_ERR;
}
/*
* See if need to setup for write or read.
*/
if (will_write) {
POOL_MEM virtual_filename(PM_FNAME);
/*
* Setup internal system for writing data.
*/
Dmsg1(100, "Start append data. res=%d\n", dcr->dev->num_reserved());
/*
* One NDMP backup Job can be one or more save sessions so we keep
* track if we already acquired the storage.
*/
if (!jcr->acquired_storage) {
/*
* Actually acquire the device which we reserved.
*/
if (!acquire_device_for_append(dcr)) {
goto bail_out;
}
/*
* Let any SD plugin know now its time to setup the record translation infra.
*/
if (generate_plugin_event(jcr, bsdEventSetupRecordTranslation, dcr) != bRC_OK) {
goto bail_out;
}
/*
* Keep track that we acquired the storage.
*/
jcr->acquired_storage = true;
Dmsg1(50, "Begin append device=%s\n", dcr->dev->print_name());
/*
* Change the Job to running state.
*/
jcr->sendJobStatus(JS_Running);
/*
* As we only generate very limited attributes info e.g. one
* set per NDMP backup stream we only setup data spooling and not
* attribute spooling.
*/
if (!begin_data_spool(dcr) ) {
goto bail_out;
}
/*
* Write Begin Session Record
*/
if (!write_session_label(dcr, SOS_LABEL)) {
Jmsg1(jcr, M_FATAL, 0,
_("Write session label failed. ERR=%s\n"),
dcr->dev->bstrerror());
goto bail_out;
}
dcr->VolFirstIndex = dcr->VolLastIndex = 0;
jcr->run_time = time(NULL); /* start counting time for rates */
/*
* The session is saved as one file stream.
*/
dcr->FileIndex = 1;
jcr->JobFiles = 1;
} else {
/*
* The next session is saved as one file stream.
*/
dcr->FileIndex++;
jcr->JobFiles++;
}
/*
* Create a virtual file name @NDMP/<filesystem>%<dumplevel> or
* @NDMP/<filesystem> and save the attributes to the director.
*/
if (bstrcasecmp(jcr->backup_format, "dump")) {
Mmsg(virtual_filename, "/@NDMP%s%%%d", filesystem, jcr->DumpLevel);
} else {
Mmsg(virtual_filename, "/@NDMP%s", filesystem);
}
if (!bndmp_create_virtual_file(jcr, virtual_filename.c_str())) {
Jmsg0(jcr, M_FATAL, 0,
_("Creating virtual file attributes failed.\n"));
goto bail_out;
}
} else {
bool ok = true;
READ_CTX *rctx;
/*
* Setup internal system for reading data (if not done before).
*/
if (!jcr->acquired_storage) {
Dmsg0(20, "Start read data.\n");
if (jcr->NumReadVolumes == 0) {
Jmsg(jcr, M_FATAL, 0, _("No Volume names found for restore.\n"));
goto bail_out;
}
Dmsg2(200, "Found %d volumes names to restore. First=%s\n",
jcr->NumReadVolumes, jcr->VolList->VolumeName);
/*
* Ready device for reading
*/
if (!acquire_device_for_read(dcr)) {
goto bail_out;
}
/*
* Let any SD plugin know now its time to setup the record translation infra.
*/
if (generate_plugin_event(jcr, bsdEventSetupRecordTranslation, dcr) != bRC_OK) {
goto bail_out;
}
/*
* Keep track that we acquired the storage.
*/
jcr->acquired_storage = true;
/*
* Change the Job to running state.
*/
jcr->sendJobStatus(JS_Running);
Dmsg1(50, "Begin reading device=%s\n", dcr->dev->print_name());
position_device_to_first_file(jcr, dcr);
jcr->mount_next_volume = false;
/*
* Allocate a new read context for this Job.
*/
rctx = new_read_context();
jcr->rctx = rctx;
/*
* Read the first block and setup record processing.
*/
if (!read_next_block_from_device(dcr, &rctx->sessrec,
NULL, mount_next_read_volume, &ok)) {
Jmsg1(jcr, M_FATAL, 0,
_("Read session label failed. ERR=%s\n"),
dcr->dev->bstrerror());
goto bail_out;
}
read_context_set_record(dcr, rctx);
rctx->records_processed = 0;
rctx->rec->state_bits = 0;
rctx->lastFileIndex = READ_NO_FILEINDEX;
}
}
/*
* Setup NDMP states.
*/
ta = sess->tape_acb;
ta->tape_fd = 0; /* fake filedescriptor */
bzero (&ta->tape_state, sizeof ta->tape_state);
ta->tape_state.error = NDMP9_NO_ERR;
ta->tape_state.state = NDMP9_TAPE_STATE_OPEN;
ta->tape_state.open_mode = will_write ?
NDMP9_TAPE_RDWR_MODE :
NDMP9_TAPE_READ_MODE;
ta->tape_state.file_num.valid = NDMP9_VALIDITY_VALID;
ta->tape_state.soft_errors.valid = NDMP9_VALIDITY_VALID;
ta->tape_state.block_size.valid = NDMP9_VALIDITY_VALID;
ta->tape_state.blockno.valid = NDMP9_VALIDITY_VALID;
ta->tape_state.total_space.valid = NDMP9_VALIDITY_INVALID;
ta->tape_state.space_remain.valid = NDMP9_VALIDITY_INVALID;
return NDMP9_NO_ERR;
bail_out:
jcr->setJobStatus(JS_ErrorTerminated);
return NDMP9_NO_DEVICE_ERR;
}
extern "C" ndmp9_error bndmp_tape_close(struct ndm_session *sess)
{
JCR *jcr;
ndmp9_error err;
struct ndmp_session_handle *handle;
struct ndm_tape_agent *ta = sess->tape_acb;
char ndmp_seperator[] = { "NdMpSePeRaToR" };
if (ta->tape_fd < 0) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
bndmp_simu_flush_weof(sess);
/*
* Setup the glue between the NDMP layer and the Storage Daemon.
*/
handle = (struct ndmp_session_handle *)sess->session_handle;
jcr = handle->jcr;
if (!jcr) {
Jmsg0(NULL, M_FATAL, 0, _("JCR is NULL!!!\n"));
return NDMP9_DEV_NOT_OPEN_ERR;
}
err = NDMP9_NO_ERR;
if (NDMTA_TAPE_IS_WRITABLE(ta)) {
/*
* Write a seperator record so on restore we can recognize the different
* NDMP datastreams from each other.
*/
if (!bndmp_write_data_to_block(jcr, STREAM_NDMP_SEPERATOR, ndmp_seperator, 13)) {
err = NDMP9_IO_ERR;
}
}
pthread_cond_signal(&jcr->job_end_wait); /* wake any waiting thread */
ta->tape_fd = -1;
ndmos_tape_initialize(sess);
return err;
}
extern "C" ndmp9_error bndmp_tape_mtio(struct ndm_session *sess,
ndmp9_tape_mtio_op op,
u_long count,
u_long *resid)
{
struct ndm_tape_agent *ta = sess->tape_acb;
*resid = 0;
if (ta->tape_fd < 0) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
/*
* audit for valid op and for tape mode
*/
switch (op) {
case NDMP9_MTIO_FSF:
return NDMP9_NO_ERR;
case NDMP9_MTIO_BSF:
return NDMP9_NO_ERR;
case NDMP9_MTIO_FSR:
return NDMP9_NO_ERR;
case NDMP9_MTIO_BSR:
return NDMP9_NO_ERR;
case NDMP9_MTIO_REW:
bndmp_simu_flush_weof(sess);
*resid = 0;
ta->tape_state.file_num.value = 0;
ta->tape_state.blockno.value = 0;
break;
case NDMP9_MTIO_OFF:
return NDMP9_NO_ERR;
case NDMP9_MTIO_EOF: /* should be "WFM" write-file-mark */
return NDMP9_NO_ERR;
default:
return NDMP9_ILLEGAL_ARGS_ERR;
}
return NDMP9_NO_ERR;
}
extern "C" ndmp9_error bndmp_tape_write(struct ndm_session *sess,
char *buf,
u_long count,
u_long *done_count)
{
JCR *jcr;
ndmp9_error err;
struct ndmp_session_handle *handle;
struct ndm_tape_agent *ta = sess->tape_acb;
if (ta->tape_fd < 0) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
if (!NDMTA_TAPE_IS_WRITABLE(ta)) {
return NDMP9_PERMISSION_ERR;
}
/*
* Setup the glue between the NDMP layer and the Storage Daemon.
*/
handle = (struct ndmp_session_handle *)sess->session_handle;
jcr = handle->jcr;
if (!jcr) {
Jmsg0(NULL, M_FATAL, 0, _("JCR is NULL!!!\n"));
return NDMP9_DEV_NOT_OPEN_ERR;
}
/*
* Turn the NDMP data into a internal record and save it.
*/
if (bndmp_write_data_to_block(jcr, STREAM_FILE_DATA, buf, count)) {
ta->tape_state.blockno.value++;
*done_count = count;
err = NDMP9_NO_ERR;
} else {
jcr->setJobStatus(JS_ErrorTerminated);
err = NDMP9_IO_ERR;
}
ta->weof_on_close = 1;
return err;
}
extern "C" ndmp9_error bndmp_tape_wfm(struct ndm_session *sess)
{
ndmp9_error err;
struct ndm_tape_agent *ta = sess->tape_acb;
ta->weof_on_close = 0;
if (ta->tape_fd < 0) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
if (!NDMTA_TAPE_IS_WRITABLE(ta)) {
return NDMP9_PERMISSION_ERR;
}
err = NDMP9_NO_ERR;
return err;
}
extern "C" ndmp9_error bndmp_tape_read(struct ndm_session *sess,
char *buf,
u_long count,
u_long *done_count)
{
JCR *jcr;
ndmp9_error err;
struct ndmp_session_handle *handle;
struct ndm_tape_agent *ta = sess->tape_acb;
if (ta->tape_fd < 0) {
return NDMP9_DEV_NOT_OPEN_ERR;
}
/*
* Setup the glue between the NDMP layer and the Storage Daemon.
*/
handle = (struct ndmp_session_handle *)sess->session_handle;
jcr = handle->jcr;
if (!jcr) {
Jmsg0(NULL, M_FATAL, 0, _("JCR is NULL!!!\n"));
return NDMP9_DEV_NOT_OPEN_ERR;
}
if (bndmp_read_data_from_block(jcr, buf, count, done_count)) {
ta->tape_state.blockno.value++;
if (*done_count == 0) {
err = NDMP9_EOF_ERR;
} else {
err = NDMP9_NO_ERR;
}
} else {
jcr->setJobStatus(JS_ErrorTerminated);
err = NDMP9_IO_ERR;
}
return err;
}
static inline void register_callback_hooks(void)
{
struct ndm_auth_callbacks auth_callbacks;
struct ndm_tape_simulator_callbacks tape_callbacks;