/
ndmp_dma.c
2188 lines (1883 loc) · 59.1 KB
/
ndmp_dma.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_dma.c implements the NDMP Data Management Application (DMA)
* which controls all NDMP backups and restores.
*
* Marco van Wieringen, May 2012
*/
#include "bareos.h"
#include "dird.h"
#if HAVE_NDMP
#include "ndmp/ndmagents.h"
#define B_PAGE_SIZE 4096
#define MAX_PAGES 2400
#define MAX_BUF_SIZE (MAX_PAGES * B_PAGE_SIZE) /* approx 10MB */
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* Imported variables */
extern DIRRES *director;
/* Forward referenced functions */
/*
* Lightweight version of Bareos tree layout for holding the NDMP
* filehandle index database. See lib/tree.[ch] for the full version.
*/
struct ndmp_fhdb_mem {
struct ndmp_fhdb_mem *next; /* next buffer */
int rem; /* remaining bytes */
char *mem; /* memory pointer */
char first[1]; /* first byte */
};
struct ndmp_fhdb_node {
/*
* KEEP sibling as the first member to avoid having to
* do initialization of child
*/
rblink sibling;
rblist child;
char *fname; /* file name */
int32_t FileIndex; /* file index */
uint16_t fname_len; /* filename length */
struct ndmp_fhdb_node *parent;
struct ndmp_fhdb_node *next; /* next hash of FileIndex */
};
struct ndmp_fhdb_root {
/*
* KEEP sibling as the first member to avoid having to
* do initialization of child
*/
rblink sibling;
rblist child;
char *fname; /* file name */
int32_t FileIndex; /* file index */
uint16_t fname_len; /* filename length */
struct ndmp_fhdb_node *parent;
struct ndmp_fhdb_node *next; /* next hash of FileIndex */
/* The above ^^^ must be identical to a ndmp_fhdb_node structure */
struct ndmp_fhdb_node *first; /* first entry in the tree */
struct ndmp_fhdb_node *last; /* last entry in the tree */
struct ndmp_fhdb_mem *mem; /* tree memory */
uint32_t total_size; /* total bytes allocated */
uint32_t blocks; /* total mallocs */
};
/*
* Internal structure to keep track of private data for logging.
*/
struct ndmp_log_cookie {
uint32_t LogLevel;
JCR *jcr;
UAContext *ua;
struct ndmp_fhdb_root *fhdb_root;
};
static char OKbootstrap[] =
"3000 OK bootstrap\n";
/*
* Array used for storing fixed NDMP env keywords.
* Anything special should go into a so called meta-tag in the fileset options.
*/
static char *ndmp_env_keywords[] = {
(char *)"HIST",
(char *)"TYPE",
(char *)"DIRECT",
(char *)"LEVEL",
(char *)"UPDATE",
(char *)"EXCLUDE",
(char *)"INCLUDE",
(char *)"FILESYSTEM",
(char *)"PREFIX"
};
/*
* Index values for above keyword.
*/
enum {
NDMP_ENV_KW_HIST = 0,
NDMP_ENV_KW_TYPE,
NDMP_ENV_KW_DIRECT,
NDMP_ENV_KW_LEVEL,
NDMP_ENV_KW_UPDATE,
NDMP_ENV_KW_EXCLUDE,
NDMP_ENV_KW_INCLUDE,
NDMP_ENV_KW_FILESYSTEM,
NDMP_ENV_KW_PREFIX
};
/*
* Array used for storing fixed NDMP env values.
* Anything special should go into a so called meta-tag in the fileset options.
*/
static char *ndmp_env_values[] = {
(char *)"n",
(char *)"y"
};
/*
* Index values for above values.
*/
enum {
NDMP_ENV_VALUE_NO = 0,
NDMP_ENV_VALUE_YES
};
/*
* Lightweight version of Bareos tree functions for holding the NDMP
* filehandle index database. See lib/tree.[ch] for the full version.
*/
static void malloc_buf(struct ndmp_fhdb_root *root, int size)
{
struct ndmp_fhdb_mem *mem;
mem = (struct ndmp_fhdb_mem *)malloc(size);
root->total_size += size;
root->blocks++;
mem->next = root->mem;
root->mem = mem;
mem->mem = mem->first;
mem->rem = (char *)mem + size - mem->mem;
}
/*
* Note, we allocate a big buffer in the tree root
* from which we allocate nodes. This runs more
* than 100 times as fast as directly using malloc()
* for each of the nodes.
*/
static inline struct ndmp_fhdb_root *ndmp_fhdb_new_tree(int count)
{
struct ndmp_fhdb_root *root;
uint32_t size;
if (count < 1000) { /* minimum tree size */
count = 1000;
}
root = (struct ndmp_fhdb_root *)malloc(sizeof(struct ndmp_fhdb_root));
memset(root, 0, sizeof(struct ndmp_fhdb_root));
/*
* Assume filename + node = 40 characters average length
*/
size = count * (BALIGN(sizeof(struct ndmp_fhdb_root)) + 40);
if (count > 1000000 || size > (MAX_BUF_SIZE / 2)) {
size = MAX_BUF_SIZE;
}
Dmsg2(400, "count=%d size=%d\n", count, size);
malloc_buf(root, size);
return root;
}
/*
* Allocate bytes for filename in tree structure.
* Keep the pointers properly aligned by allocating
* sizes that are aligned.
*/
static inline char *ndmp_fhdb_tree_alloc(struct ndmp_fhdb_root *root, int size)
{
char *buf;
int asize = BALIGN(size);
if (root->mem->rem < asize) {
uint32_t mb_size;
if (root->total_size >= (MAX_BUF_SIZE / 2)) {
mb_size = MAX_BUF_SIZE;
} else {
mb_size = MAX_BUF_SIZE / 2;
}
malloc_buf(root, mb_size);
}
root->mem->rem -= asize;
buf = root->mem->mem;
root->mem->mem += asize;
return buf;
}
/*
* This routine can be called to release the
* previously allocated tree node.
*/
static inline void ndmp_fhdb_free_tree_node(struct ndmp_fhdb_root *root)
{
int asize = BALIGN(sizeof(struct ndmp_fhdb_node));
root->mem->rem += asize;
root->mem->mem -= asize;
}
/*
* Create a new tree node.
*/
static struct ndmp_fhdb_node *ndmp_fhdb_new_tree_node(struct ndmp_fhdb_root *root)
{
struct ndmp_fhdb_node *node;
int size = sizeof(struct ndmp_fhdb_node);
node = (struct ndmp_fhdb_node *)ndmp_fhdb_tree_alloc(root, size);
memset(node, 0, size);
return node;
}
/*
* This routine frees the whole tree
*/
static inline void ndmp_fhdb_free_tree(struct ndmp_fhdb_root *root)
{
struct ndmp_fhdb_mem *mem, *rel;
uint32_t freed_blocks = 0;
for (mem = root->mem; mem; ) {
rel = mem;
mem = mem->next;
free(rel);
freed_blocks++;
}
Dmsg3(100, "Total size=%u blocks=%u freed_blocks=%u\n", root->total_size, root->blocks, freed_blocks);
free(root);
garbage_collect_memory();
return;
}
/*
* Fill a ndmagent structure with the correct info. Instead of calling ndmagent_from_str
* we fill the structure ourself from info provides in a resource.
*/
static inline bool fill_ndmp_agent_config(JCR *jcr,
struct ndmagent *agent,
uint32_t protocol,
uint32_t authtype,
char *address,
uint32_t port,
char *username,
char *password)
{
agent->conn_type = NDMCONN_TYPE_REMOTE;
switch (protocol) {
case APT_NDMPV2:
agent->protocol_version = 2;
break;
case APT_NDMPV3:
agent->protocol_version = 3;
break;
case APT_NDMPV4:
agent->protocol_version = 4;
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Illegal protocol %d for NDMP Job\n"), protocol);
return false;
}
switch (authtype) {
case AT_NONE:
agent->auth_type = 'n';
break;
case AT_CLEAR:
agent->auth_type = 't';
break;
case AT_MD5:
agent->auth_type = 'm';
break;
case AT_VOID:
agent->auth_type = 'v';
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Illegal authtype %d for NDMP Job\n"), authtype);
return false;
}
agent->port = port;
bstrncpy(agent->host, address, sizeof(agent->host));
bstrncpy(agent->account, username, sizeof(agent->account));
bstrncpy(agent->password, password, sizeof(agent->password));
return true;
}
static inline int native_to_ndmp_level(JCR *jcr, char *filesystem)
{
int level = -1;
if (!db_create_ndmp_level_mapping(jcr, jcr->db, &jcr->jr, filesystem)) {
return -1;
}
switch (jcr->getJobLevel()) {
case L_FULL:
level = 0;
break;
case L_DIFFERENTIAL:
level = 1;
break;
case L_INCREMENTAL:
level = db_get_ndmp_level_mapping(jcr, jcr->db, &jcr->jr, filesystem);
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Illegal Job Level %c for NDMP Job\n"), jcr->getJobLevel());
break;
}
/*
* Dump level can be from 0 - 9
*/
if (level < 0 || level > 9) {
Jmsg(jcr, M_FATAL, 0, _("NDMP dump format doesn't support more than 8 "
"incrementals, please run a Differential or a Full Backup\n"));
level = -1;
}
return level;
}
/*
* Parse a meta-tag and convert it into a ndmp_pval
*/
static inline void parse_meta_tag(struct ndm_env_table *env_tab,
char *meta_tag)
{
char *p;
ndmp9_pval pv;
/*
* See if the meta-tag is parseable.
*/
if ((p = strchr(meta_tag, '=')) == NULL) {
return;
}
/*
* Split the tag on the '='
*/
*p = '\0';
pv.name = meta_tag;
pv.value = p + 1;
ndma_store_env_list(env_tab, &pv);
/*
* Restore the '='
*/
*p = '=';
}
/*
* Fill the NDMP backup environment table with the data for the data agent to act on.
*/
static inline bool fill_backup_environment(JCR *jcr,
INCEXE *ie,
char *filesystem,
struct ndm_job_param *job)
{
int i, j, cnt;
bool exclude;
FOPTS *fo;
ndmp9_pval pv;
POOL_MEM pattern;
POOL_MEM tape_device;
/*
* We want to receive file history info from the NDMP backup.
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_HIST];
pv.value = ndmp_env_values[NDMP_ENV_VALUE_YES];
ndma_store_env_list(&job->env_tab, &pv);
/*
* Tell the data agent what type of backup to make.
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_TYPE];
pv.value = job->bu_type;
ndma_store_env_list(&job->env_tab, &pv);
/*
* See if we are doing a dump backup type and set any specific keywords
* for that backup type.
*/
if (bstrcasecmp(job->bu_type, "dump")) {
char text_level[50];
/*
* Set the dump level for the backup.
*/
jcr->DumpLevel = native_to_ndmp_level(jcr, filesystem);
job->bu_level = jcr->DumpLevel;
if (job->bu_level == -1) {
return false;
}
pv.name = ndmp_env_keywords[NDMP_ENV_KW_LEVEL];
pv.value = edit_uint64(job->bu_level, text_level);
ndma_store_env_list(&job->env_tab, &pv);
/*
* Update the dumpdates
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_UPDATE];
pv.value = ndmp_env_values[NDMP_ENV_VALUE_YES];
ndma_store_env_list(&job->env_tab, &pv);
}
/*
* Tell the data engine what to backup.
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_FILESYSTEM];
pv.value = filesystem;
ndma_store_env_list(&job->env_tab, &pv);
/*
* Loop over each option block for this fileset and append any
* INCLUDE/EXCLUDE and/or META tags to the env_tab of the NDMP backup.
*/
for (i = 0; i < ie->num_opts; i++) {
fo = ie->opts_list[i];
/*
* Pickup any interesting patterns.
*/
cnt = 0;
pm_strcpy(pattern, "");
for (j = 0; j < fo->wild.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(pattern, (char *)fo->wild.get(j));
cnt++;
}
for (j = 0; j < fo->wildfile.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(pattern, (char *)fo->wildfile.get(j));
cnt++;
}
for (j = 0; j < fo->wilddir.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(pattern, (char *)fo->wilddir.get(j));
cnt++;
}
/*
* See if this is a INCLUDE or EXCLUDE block.
*/
if (cnt > 0) {
exclude = false;
for (j = 0; fo->opts[j] != '\0'; j++) {
if (fo->opts[j] == 'e') {
exclude = true;
break;
}
}
if (exclude) {
pv.name = ndmp_env_keywords[NDMP_ENV_KW_EXCLUDE];
} else {
pv.name = ndmp_env_keywords[NDMP_ENV_KW_INCLUDE];
}
pv.value = pattern.c_str();
ndma_store_env_list(&job->env_tab, &pv);
}
/*
* Parse all specific META tags for this option block.
*/
for (j = 0; j < fo->meta.size(); j++) {
parse_meta_tag(&job->env_tab, (char *)fo->meta.get(j));
}
}
/*
* If we have a paired storage definition we put the storage daemon
* auth key and the filesystem into the tape device name of the
* NDMP session. This way the storage daemon can link the NDMP
* data and the normal save session together.
*/
if (jcr->store_bsock) {
Mmsg(tape_device, "%s@%s", jcr->sd_auth_key, filesystem);
job->tape_device = bstrdup(tape_device.c_str());
}
return true;
}
/*
* Add a filename to the files we want to restore. Whe need
* to create both the original path and the destination patch
* of the file to restore in a ndmp9_name structure.
*/
static inline void add_to_namelist(struct ndm_job_param *job,
char *filename,
char *restore_prefix)
{
ndmp9_name nl;
POOL_MEM destination_path;
/*
* See if the filename is an absolute pathname.
*/
if (*filename == '/') {
Mmsg(destination_path, "%s%s", restore_prefix, filename);
} else {
Mmsg(destination_path, "%s/%s", restore_prefix, filename);
}
memset(&nl, 0, sizeof(ndmp9_name));
nl.original_path = filename;
nl.destination_path = destination_path.c_str();
ndma_store_nlist(&job->nlist_tab, &nl);
}
/*
* Walk the tree of selected files for restore and lookup the
* correct fileid. Return the actual full pathname of the file
* corresponding to the given fileid.
*/
static inline char *lookup_fileindex(JCR *jcr, int32_t FileIndex)
{
TREE_NODE *node;
POOL_MEM restore_pathname, tmp;
for (node = first_tree_node(jcr->restore_tree_root); node; node = next_tree_node(node)) {
/*
* See if this is the wanted FileIndex.
*/
if (node->FileIndex == FileIndex) {
pm_strcpy(restore_pathname, node->fname);
/*
* Walk up the parent until we hit the head of the list.
*/
for (node = node->parent; node; node = node->parent) {
pm_strcpy(tmp, restore_pathname.c_str());
Mmsg(restore_pathname, "%s/%s", node->fname, tmp.c_str());
}
return bstrdup(restore_pathname.c_str());
}
}
return NULL;
}
/*
* Fill the NDMP restore environment table with the data for the data agent to act on.
*/
static inline bool fill_restore_environment(JCR *jcr,
int32_t current_fi,
struct ndm_job_param *job)
{
int i;
char *bp;
ndmp9_pval pv;
FILESETRES *fileset;
char *restore_pathname,
*restore_prefix;
POOL_MEM tape_device;
POOL_MEM destination_path;
/*
* Tell the data agent what type of restore stream to expect.
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_TYPE];
pv.value = job->bu_type;
ndma_store_env_list(&job->env_tab, &pv);
/*
* Lookup the current fileindex and map it to an actual pathname.
*/
restore_pathname = lookup_fileindex(jcr, current_fi);
if (!restore_pathname) {
return false;
}
/*
* Make sure the restored file pattern is in the pseudo NDMP naming space.
*/
if (!bstrncmp(restore_pathname, "/@NDMP/", 7)) {
return false;
}
/*
* See if there is a level embedded in the pathname.
*/
bp = strrchr(restore_pathname, '%');
if (bp) {
*bp = '\0';
}
/*
* Lookup any meta tags that need to be added.
*/
fileset = jcr->res.fileset;
for (i = 0; i < fileset->num_includes; i++) {
int j;
char *item;
INCEXE *ie = fileset->include_items[i];
/*
* Loop over each file = entry of the fileset.
*/
for (j = 0; j < ie->name_list.size(); j++) {
item = (char *)ie->name_list.get(j);
/*
* See if the restore path matches.
*/
if (bstrcasecmp(item, restore_pathname)) {
int k, l;
FOPTS *fo;
for (k = 0; k < ie->num_opts; k++) {
fo = ie->opts_list[i];
/*
* Parse all specific META tags for this option block.
*/
for (l = 0; l < fo->meta.size(); l++) {
parse_meta_tag(&job->env_tab, (char *)fo->meta.get(l));
}
}
}
}
}
/*
* See where to restore the data.
*/
restore_prefix = NULL;
if (jcr->where) {
restore_prefix = jcr->where;
} else {
restore_prefix = jcr->res.job->RestoreWhere;
}
if (!restore_prefix) {
return false;
}
Mmsg(destination_path, "%s/%s", restore_prefix, restore_pathname + 7);
/*
* Tell the data engine where to restore.
*/
pv.name = ndmp_env_keywords[NDMP_ENV_KW_PREFIX];
pv.value = destination_path.c_str();
ndma_store_env_list(&job->env_tab, &pv);
/*
* The smtape NDMP backup type doesn't need a namelist to restore the
* data as it restores all data from the stream anyhow.
*/
if (!bstrcasecmp(job->bu_type, "smtape")) {
/*
* FIXME: For now we say we want to restore everything later on it would
* be nice to only restore parts of the whole backup.
*/
add_to_namelist(job, (char *)"/", destination_path.c_str());
}
/*
* If we have a paired storage definition we put the storage daemon
* auth key and the filesystem into the tape device name of the
* NDMP session. This way the storage daemon can link the NDMP
* data and the normal save session together.
*/
if (jcr->store_bsock) {
Mmsg(tape_device, "%s@%s", jcr->sd_auth_key, restore_pathname + 6);
job->tape_device = bstrdup(tape_device.c_str());
}
free(restore_pathname);
return true;
}
static inline bool build_ndmp_job(JCR *jcr,
CLIENTRES *client,
STORERES *store,
int operation,
struct ndm_job_param *job)
{
memset(job, 0, sizeof(struct ndm_job_param));
job->operation = operation;
job->bu_type = jcr->backup_format;
/*
* For NDMP the backupformat is a prerequite abort the backup job when
* it is not supplied in the config definition.
*/
if (!job->bu_type) {
Jmsg(jcr, M_FATAL, 0, _("No backup type specified in NDMP job\n"));
goto bail_out;
}
/*
* The data_agent is the client being backuped or restored using NDMP.
*/
if (!fill_ndmp_agent_config(jcr, &job->data_agent, client->Protocol,
client->AuthType, client->address,
client->FDport, client->username,
client->password)) {
goto bail_out;
}
/*
* The tape_agent is the storage daemon via the NDMP protocol.
*/
if (!fill_ndmp_agent_config(jcr, &job->tape_agent, store->Protocol,
store->AuthType, store->address,
store->SDport, store->username,
store->password)) {
goto bail_out;
}
job->record_size = jcr->res.client->ndmp_blocksize;
return true;
bail_out:
return false;
}
static inline bool validate_ndmp_job(JCR *jcr,
struct ndm_job_param *job)
{
int n_err, i;
char audit_buffer[256];
/*
* Audit the job so we only submit a valid NDMP job.
*/
n_err = 0;
i = 0;
do {
n_err = ndma_job_audit(job, audit_buffer, i);
if (n_err) {
Jmsg(jcr, M_ERROR, 0, _("NDMP Job validation error = %s\n"), audit_buffer);
}
i++;
} while (i < n_err);
if (n_err) {
return false;
}
return true;
}
extern "C" int bndmp_add_file(struct ndmlog *ixlog, int tagc, char *raw_name,
ndmp9_file_stat *fstat)
{
struct ndmp_log_cookie *log_cookie;
char namebuf[NDMOS_CONST_PATH_MAX];
ndmcstr_from_str(raw_name, namebuf, sizeof(namebuf));
log_cookie = (struct ndmp_log_cookie *)ixlog->cookie;
log_cookie->jcr->lock();
log_cookie->jcr->JobFiles++;
log_cookie->jcr->unlock();
return 0;
}
extern "C" int bndmp_add_dir(struct ndmlog *ixlog, int tagc, char *raw_name,
ndmp9_u_quad dir_node, ndmp9_u_quad node)
{
struct ndmp_log_cookie *log_cookie;
char namebuf[NDMOS_CONST_PATH_MAX];
ndmcstr_from_str(raw_name, namebuf, sizeof(namebuf));
/*
* Ignore . and .. directory entries.
*/
if (bstrcmp(namebuf, ".") || bstrcmp(namebuf, "..")) {
return 0;
}
log_cookie = (struct ndmp_log_cookie *)ixlog->cookie;
log_cookie->jcr->lock();
log_cookie->jcr->JobFiles++;
log_cookie->jcr->unlock();
return 0;
}
extern "C" int bndmp_add_node(struct ndmlog *ixlog, int tagc,
ndmp9_u_quad node, ndmp9_file_stat *fstat)
{
struct ndmp_log_cookie *log_cookie;
log_cookie = (struct ndmp_log_cookie *)ixlog->cookie;
return 0;
}
extern "C" int bndmp_add_dirnode_root(struct ndmlog *ixlog, int tagc,
ndmp9_u_quad root_node)
{
struct ndmp_log_cookie *log_cookie;
log_cookie = (struct ndmp_log_cookie *)ixlog->cookie;
return 0;
}
/*
* This glues the NDMP File Handle DB with internal code.
*/
static inline void register_callback_hooks(void)
{
struct ndm_fhdb_callbacks fhdb_callbacks;
/*
* Register the FileHandleDB callbacks.
*/
fhdb_callbacks.add_file = bndmp_add_file;
fhdb_callbacks.add_dir = bndmp_add_dir;
fhdb_callbacks.add_node = bndmp_add_node;
fhdb_callbacks.add_dirnode_root = bndmp_add_dirnode_root;
ndmfhdb_register_callbacks(&fhdb_callbacks);
}
static inline void unregister_callback_hooks(void)
{
ndmfhdb_unregister_callbacks();
}
/*
* Extract any post backup statistics.
*/
static inline bool extract_post_backup_stats(JCR *jcr,
char *filesystem,
struct ndm_session *sess)
{
bool retval = true;
struct ndmmedia *me;
/*
* See if an error was raised during the backup session.
*/
if (sess->error_raised) {
return false;
}
/*
* See if there is any media error.
*/
for (me = sess->control_acb->job.result_media_tab.head; me; me = me->next) {
if (me->media_open_error ||
me->media_io_error ||
me->label_io_error ||
me->label_mismatch ||
me->fmark_error) {
retval = false;
}
}
/*
* Update the Job statistics from the NDMP statistics.
*/
jcr->JobBytes += sess->control_acb->job.bytes_written;
/*
* If this was a NDMP backup with backup type dump save the last used dump level.
*/
if (bstrcasecmp(sess->control_acb->job.bu_type, "dump")) {
db_update_ndmp_level_mapping(jcr, jcr->db, &jcr->jr,
filesystem, sess->control_acb->job.bu_level);
}
return retval;
}
/*
* Extract any post backup statistics.
*/
static inline bool extract_post_restore_stats(JCR *jcr,
struct ndm_session *sess)
{
bool retval = true;
struct ndmmedia *me;
/*
* See if an error was raised during the backup session.
*/
if (sess->error_raised) {
return false;
}
/*
* See if there is any media error.
*/
for (me = sess->control_acb->job.result_media_tab.head; me; me = me->next) {
if (me->media_open_error ||
me->media_io_error ||
me->label_io_error ||
me->label_mismatch ||
me->fmark_error) {
retval = false;
}
}
/*
* Update the Job statistics from the NDMP statistics.
*/
jcr->JobBytes += sess->control_acb->job.bytes_read;
jcr->JobFiles++;
return retval;
}
/*
* Helper function to stuff an JCR in the opaque log cookie.
*/
static inline void set_jcr_in_cookie(JCR *jcr, void *cookie)
{
struct ndmp_log_cookie *log_cookie;
log_cookie = (struct ndmp_log_cookie *)cookie;
log_cookie->jcr = jcr;
}
/*
* Helper function to stuff an UAContext in the opaque log cookie.
*/
static inline void set_ua_in_cookie(UAContext *ua, void *cookie)
{
struct ndmp_log_cookie *log_cookie;
log_cookie = (struct ndmp_log_cookie *)cookie;
log_cookie->ua = ua;
}
/*
* Calculate the wanted NDMP loglevel from the current debug level and
* any configure minimum level.
*/
static inline int native_to_ndmp_loglevel(CLIENTRES *client, int debuglevel, void *cookie)
{
unsigned int level;
struct ndmp_log_cookie *log_cookie;
log_cookie = (struct ndmp_log_cookie *)cookie;