/
ndmp_dma_storage.c
1192 lines (1019 loc) · 34.3 KB
/
ndmp_dma_storage.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-2015 Planets Communications B.V.
Copyright (C) 2013-2017 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.
*/
/*
* Marco van Wieringen, May 2015
*/
/**
* @file
* Storage specific NDMP Data Management Application (DMA) routines
*/
#include "bareos.h"
#include "dird.h"
#if HAVE_NDMP
#include "ndmp/ndmagents.h"
#include "ndmp_dma_priv.h"
/* Imported variables */
/* Forward referenced functions */
/**
* ndmp query callback
*/
int get_tape_info_cb(struct ndm_session *sess, ndmp9_device_info *info, unsigned n_info)
{
Dmsg0(100, "Get tape info called\n");
unsigned int i, j, k;
const char *what = "tape";
JCR *jcr = NULL;
STORERES *store = NULL;
NIS *nis = (NIS *)sess->param->log.ctx;
if (nis->jcr) {
jcr = nis->jcr;
} else if (nis->ua && nis->ua->jcr) {
jcr = nis->ua->jcr;
} else {
return -1;
}
if (jcr->is_JobType(JT_BACKUP)) {
store = jcr->res.wstore;
} else if (jcr->is_JobType(JT_RESTORE)) {
store = jcr->res.rstore;
} else {
return -1;
}
/* if (store->rss->ndmp_deviceinfo) { */
/* delete(store->rss->ndmp_deviceinfo); */
/* store->rss->ndmp_deviceinfo = NULL; */
/* } */
if (!store->rss->ndmp_deviceinfo) {
store->rss->ndmp_deviceinfo = new(std::list<ndmp_deviceinfo_t>);
for (i = 0; i < n_info; i++) {
Dmsg2(100, " %s %s\n", what, info[i].model);
ndmp_deviceinfo_t *devinfo = new(ndmp_deviceinfo_t);
devinfo->JobIdUsingDevice = 0;
ndmp9_device_capability *info_dc;
info_dc = info[i].caplist.caplist_val;
devinfo->model = info[i].model;
devinfo->device = info_dc->device;
store->rss->ndmp_deviceinfo->push_back(*devinfo);
for (j = 0; j < info[i].caplist.caplist_len; j++) {
ndmp9_device_capability *dc;
uint32_t attr;
dc = &info[i].caplist.caplist_val[j];
Dmsg1(100, " device %s\n", dc->device);
if (!strcmp(what, "tape\n")) {
#ifndef NDMOS_OPTION_NO_NDMP3
if (sess->plumb.tape->protocol_version == 3) {
attr = dc->v3attr.value;
Dmsg1(100, " attr 0x%lx\n",
attr);
if (attr & NDMP3_TAPE_ATTR_REWIND)
Dmsg0(100, " REWIND\n");
if (attr & NDMP3_TAPE_ATTR_UNLOAD)
Dmsg0(100, " UNLOAD\n");
}
#endif /* !NDMOS_OPTION_NO_NDMP3 */
#ifndef NDMOS_OPTION_NO_NDMP4
if (sess->plumb.tape->protocol_version == 4) {
attr = dc->v4attr.value;
Dmsg1(100, " attr 0x%lx\n",
attr);
if (attr & NDMP4_TAPE_ATTR_REWIND)
Dmsg0(100, " REWIND\n");
if (attr & NDMP4_TAPE_ATTR_UNLOAD)
Dmsg0(100, " UNLOAD\n");
}
#endif /* !NDMOS_OPTION_NO_NDMP4 */
}
for (k = 0; k < dc->capability.capability_len; k++) {
Dmsg2(100, " set %s=%s\n",
dc->capability.capability_val[k].name,
dc->capability.capability_val[k].value);
}
if (k == 0)
Dmsg0(100, " empty capabilities\n");
}
if (j == 0)
Dmsg0(100, " empty caplist\n");
Dmsg0(100, "\n");
}
}
if (i == 0)
Dmsg1(100, " Empty %s info\n", what);
return 0;
}
/**
* execute NDMP_QUERY_AGENTS on Tape and Robot
*/
bool do_ndmp_native_query_tape_and_robot_agents(JCR *jcr, STORERES *store) {
struct ndm_job_param ndmp_job;
if (!ndmp_build_storage_job(jcr,
store,
true, /* Query Tape Agent */
true, /* Query Robot Agent */
NDM_JOB_OP_QUERY_AGENTS,
&ndmp_job)) {
Dmsg0(100, "error in ndmp_build_storage_job");
return false;
}
struct ndmca_query_callbacks query_callbacks;
query_callbacks.get_tape_info = get_tape_info_cb;
ndmca_query_callbacks *query_cbs = &query_callbacks;
ndmp_do_query(NULL, jcr, &ndmp_job, me->ndmp_loglevel, query_cbs);
/*
* Debug output
*/
if (store->rss->ndmp_deviceinfo) {
Jmsg(jcr, M_INFO, 0, "NDMP Devices for storage %s:(%s)\n", store->name(), store->rss->smc_ident);
}
for (auto devinfo = store->rss->ndmp_deviceinfo->begin();
devinfo != store->rss->ndmp_deviceinfo->end();
devinfo++) {
Jmsg(jcr, M_INFO, 0, " %s\n",
devinfo->device.c_str(), devinfo->model.c_str() );
}
return true;
}
/**
* get status of a NDMP Native storage and store the information
* coming in via the NDMP protocol
*/
void do_ndmp_native_storage_status(UAContext *ua, STORERES *store, char *cmd)
{
struct ndm_job_param ndmp_job;
ua->jcr->res.wstore = store;
if (!ndmp_build_storage_job(ua->jcr,
store,
true, /* Query Tape Agent */
true, /* Query Robot Agent */
NDM_JOB_OP_QUERY_AGENTS,
&ndmp_job)) {
ua->info_msg("build_storage_job failed\n");
return;
}
struct ndmca_query_callbacks query_callbacks;
query_callbacks.get_tape_info = get_tape_info_cb;
ndmca_query_callbacks *query_cbs = &query_callbacks;
ndmp_do_query(ua, NULL, &ndmp_job, me->ndmp_loglevel, query_cbs);
ndmp_deviceinfo_t *deviceinfo = NULL;
int i = 0;
if (store->rss->ndmp_deviceinfo) {
ua->info_msg("NDMP Devices for storage %s:(%s)\n", store->name(), store->rss->smc_ident);
ua->info_msg(" PhysicalSlotNumber Device Model (JobId) \n");
for (auto devinfo = store->rss->ndmp_deviceinfo->begin();
devinfo != store->rss->ndmp_deviceinfo->end();
devinfo++) {
ua->info_msg(" %d %s %s (%d)\n",
i++, devinfo->device.c_str(), devinfo->model.c_str(),
devinfo->JobIdUsingDevice);
}
}
}
/**
* Output the status of a storage daemon when its a normal storage
* daemon accessed via the NDMP protocol or query the TAPE and ROBOT
* agent of a native NDMP server.
*/
void do_ndmp_storage_status(UAContext *ua, STORERES *store, char *cmd)
{
/*
* See if the storage is just a NDMP instance of a normal storage daemon.
*/
if (store->paired_storage) {
do_native_storage_status(ua, store->paired_storage, cmd);
} else {
do_ndmp_native_storage_status(ua, store, cmd);
}
}
/**
* Interface function which glues the logging infra of the NDMP lib for debugging.
*/
extern "C" void ndmp_robot_status_handler(struct ndmlog *log, char *tag, int lev, char *msg)
{
NIS *nis;
/*
* Make sure if the logging system was setup properly.
*/
nis = (NIS *)log->ctx;
if (!nis) {
return;
}
Dmsg1(100, "%s\n", msg);
}
/**
* Generic cleanup function that can be used after a successful or failed NDMP Job ran.
*/
static void cleanup_ndmp_session(struct ndm_session *ndmp_sess)
{
/*
* Destroy the session.
*/
ndma_session_destroy(ndmp_sess);
/*
* Free the param block.
*/
free(ndmp_sess->param->log_tag);
free(ndmp_sess->param->log.ctx);
free(ndmp_sess->param);
free(ndmp_sess);
}
/**
* Generic function to run a storage Job on a remote NDMP server.
*/
static bool ndmp_run_storage_job(JCR *jcr, STORERES *store, struct ndm_session *ndmp_sess, struct ndm_job_param *ndmp_job)
{
NIS *nis;
ndmp_sess->conn_snooping = (me->ndmp_snooping) ? 1 : 0;
ndmp_sess->control_agent_enabled = 1;
ndmp_sess->param = (struct ndm_session_param *)malloc(sizeof(struct ndm_session_param));
memset(ndmp_sess->param, 0, sizeof(struct ndm_session_param));
ndmp_sess->param->log.deliver = ndmp_robot_status_handler;
nis = (NIS *)malloc(sizeof(NIS));
memset(nis, 0, sizeof(NIS));
ndmp_sess->param->log_level = native_to_ndmp_loglevel(me->ndmp_loglevel, debug_level, nis);
ndmp_sess->param->log.ctx = nis;
ndmp_sess->param->log_tag = bstrdup("DIR-NDMP");
nis->jcr = jcr;
/*
* Initialize the session structure.
*/
if (ndma_session_initialize(ndmp_sess)) {
goto bail_out;
}
/*
* Copy the actual job to perform.
*/
memcpy(&ndmp_sess->control_acb->job, ndmp_job, sizeof(struct ndm_job_param));
if (!ndmp_validate_job(jcr, &ndmp_sess->control_acb->job)) {
goto bail_out;
}
/*
* Commission the session for a run.
*/
if (ndma_session_commission(ndmp_sess)) {
goto bail_out;
}
/*
* Setup the DMA.
*/
if (ndmca_connect_control_agent(ndmp_sess)) {
goto bail_out;
}
ndmp_sess->conn_open = 1;
ndmp_sess->conn_authorized = 1;
/*
* Let the DMA perform its magic.
*/
if (ndmca_control_agent(ndmp_sess) != 0) {
goto bail_out;
}
return true;
bail_out:
return false;
}
/**
* Generic function to get the current element status of a NDMP robot.
*/
static bool get_robot_element_status(JCR *jcr, STORERES *store, struct ndm_session **ndmp_sess)
{
struct ndm_job_param ndmp_job;
/*
* See if this is an autochanger.
*/
if (!store->autochanger || !store->ndmp_changer_device) {
return false;
}
if (!ndmp_build_storage_job(jcr,
store,
false, /* Setup Tape Agent */
true, /* Setup Robot Agent */
NDM_JOB_OP_INIT_ELEM_STATUS,
&ndmp_job)) {
return false;
}
/*
* Set the remote robotics name to use.
* We use the ndmscsi_target_from_str() function which parses the NDMJOB format of a
* device in the form NAME[,[CNUM,]SID[,LUN]
*/
ndmp_job.robot_target = (struct ndmscsi_target *)actuallymalloc(sizeof(struct ndmscsi_target));
if (ndmscsi_target_from_str(ndmp_job.robot_target, store->ndmp_changer_device) != 0) {
actuallyfree(ndmp_job.robot_target);
return false;
}
ndmp_job.have_robot = 1;
ndmp_job.auto_remedy = 1;
/*
* Initialize a new NDMP session
*/
*ndmp_sess = (struct ndm_session *)malloc(sizeof(struct ndm_session));
memset(*ndmp_sess, 0, sizeof(struct ndm_session));
if (!ndmp_run_storage_job(jcr, store, *ndmp_sess, &ndmp_job)) {
cleanup_ndmp_session(*ndmp_sess);
return false;
}
return true;
}
/**
* Get the volume names from a smc_element_descriptor.
*/
static void fill_volume_name(vol_list_t *vl, struct smc_element_descriptor *edp)
{
if (edp->PVolTag) {
vl->VolName = bstrdup((char *)edp->primary_vol_tag->volume_id);
strip_trailing_junk(vl->VolName);
} else if (edp->AVolTag) {
vl->VolName = bstrdup((char *)edp->alternate_vol_tag->volume_id);
strip_trailing_junk(vl->VolName);
}
}
/**
* Get the information to map logical addresses (index) to
* physical address (scsi element address)
*
* Everything that is needed for that is stored in the
* smc smc_element_address_assignment.
*
* For each type of element a start address and the
* number of entries (count) is stored there.
*/
static void ndmp_fill_storage_mappings(STORERES *store, struct ndm_session *ndmp_sess)
{
drive_number_t drive;
slot_number_t slot,
picker;
struct smc_ctrl_block *smc;
smc = ndmp_sess->control_acb->smc_cb;
memcpy(store->rss->smc_ident, smc->ident, sizeof(store->rss->smc_ident));
if (smc->valid_elem_aa) {
memcpy(&store->rss->storage_mapping, &smc->elem_aa, sizeof(store->rss->storage_mapping));
} else {
Dmsg0(0,"Warning, smc does not have valid elem_aa info\n");
}
}
/**
* Get the current content of the autochanger as a generic vol_list dlist.
*/
dlist *ndmp_get_vol_list(UAContext *ua, STORERES *store, bool listall, bool scan)
{
struct ndm_session *ndmp_sess;
struct smc_ctrl_block *smc;
struct smc_element_descriptor *edp;
vol_list_t *vl = NULL;
dlist *vol_list = NULL;
ua->warning_msg(_("get ndmp_vol_list...\n"));
if (!get_robot_element_status(ua->jcr, store, &ndmp_sess)) {
return (dlist *)NULL;
}
/*
* If we have no storage mappings create them now from the data we just retrieved.
*/
/* if (!store->rss->storage_mappings) { */
ndmp_fill_storage_mappings(store, ndmp_sess);
/* } */
/*
* Start with an empty dlist().
*/
vol_list = New(dlist(vl, &vl->link));
/*
* Process the robot element status retrieved.
*/
smc = ndmp_sess->control_acb->smc_cb;
for (edp = smc->elem_desc; edp; edp = edp->next) {
vl = (vol_list_t *)malloc(sizeof(vol_list_t));
memset(vl, 0, sizeof(vol_list_t));
if (scan && !listall) {
/*
* Scanning -- require only valid slot
*/
switch (edp->element_type_code) {
case SMC_ELEM_TYPE_SE:
/*
* Normal slot
*/
vl->Type = slot_type_storage;
if (edp->Full) {
vl->SlotStatus = slot_status_full;
fill_volume_name(vl, edp);
} else {
vl->SlotStatus = slot_status_empty;
}
vl->PhysicalSlotNumber = edp->element_address;
break;
default:
free(vl);
continue;
}
} else if (!listall) {
/*
* Not scanning and not listall.
*/
switch (edp->element_type_code) {
case SMC_ELEM_TYPE_SE:
/*
* Normal slot
*/
vl->Type = slot_type_storage;
vl->PhysicalSlotNumber = edp->element_address;
if (!edp->Full) {
free(vl);
continue;
} else {
vl->SlotStatus = slot_status_full;
fill_volume_name(vl, edp);
}
break;
default:
free(vl);
continue;
}
} else {
/*
* Listall.
*/
switch (edp->element_type_code) {
case SMC_ELEM_TYPE_MTE:
/*
* Transport
*/
free(vl);
continue;
case SMC_ELEM_TYPE_SE:
/*
* Normal slot
*/
vl->Type = slot_type_storage;
vl->PhysicalSlotNumber = edp->element_address;
if (edp->Full) {
vl->SlotStatus = slot_status_full;
fill_volume_name(vl, edp);
} else {
vl->SlotStatus = slot_status_empty;
}
break;
case SMC_ELEM_TYPE_IEE:
/*
* Import/Export LogicalSlotNumber
*/
vl->Type = slot_type_import;
vl->PhysicalSlotNumber = edp->element_address;
if (edp->Full) {
vl->SlotStatus = slot_status_full;
fill_volume_name(vl, edp);
} else {
vl->SlotStatus = slot_status_empty;
}
if (edp->InEnab) {
vl->Flags |= can_import;
}
if (edp->ExEnab) {
vl->Flags |= can_export;
}
if (edp->ImpExp) {
vl->Flags |= by_oper;
} else {
vl->Flags |= by_mte;
}
break;
case SMC_ELEM_TYPE_DTE:
/*
* Drive
*/
vl->Type = slot_type_drive;
vl->PhysicalSlotNumber = edp->element_address;
if (edp->Full) {
slot_number_t slot_mapping;
vl->SlotStatus = slot_status_full;
slot_mapping = get_logical_slotnumber_by_physical_slotnumber(&store->rss->storage_mapping, slot_type_storage, edp->src_se_addr);
vl->CurrentlyLoadedSlot = slot_mapping;
fill_volume_name(vl, edp);
} else {
vl->SlotStatus = slot_status_empty;
}
break;
default:
vl->Type = slot_type_unknown;
vl->PhysicalSlotNumber = edp->element_address;
break;
}
}
/*
* Map physical storage address to logical one using the storage mappings.
*/
vl->LogicalSlotNumber = get_logical_slotnumber_by_physical_slotnumber(&store->rss->storage_mapping, vl->Type, edp->element_address);
if (vl->VolName) {
Dmsg6(100, "Add phys_slot = %hd logi_slot=%hd loaded=%hd type=%hd status=%hd Vol=%s to SD list.\n",
vl->PhysicalSlotNumber, vl->LogicalSlotNumber, vl->CurrentlyLoadedSlot, vl->Type, vl->SlotStatus, NPRT(vl->VolName));
} else {
Dmsg5(100, "Add phys_slot = %hd logi_slot=%hd loaded=%hd type=%hd status=%hd Vol=NULL to SD list.\n",
vl->PhysicalSlotNumber, vl->LogicalSlotNumber, vl->CurrentlyLoadedSlot, vl->Type, vl->SlotStatus);
}
vol_list->binary_insert(vl, storage_compare_vol_list_entry);
}
if (vol_list->size() == 0) {
delete vol_list;
vol_list = NULL;
}
cleanup_ndmp_session(ndmp_sess);
return vol_list;
}
/**
* Update the mapping table from logical to physical storage addresses.
*/
bool ndmp_update_storage_mappings(JCR *jcr, STORERES *store)
{
struct ndm_session *ndmp_sess;
if (!get_robot_element_status(jcr, store, &ndmp_sess)) {
return false;
}
ndmp_fill_storage_mappings(store, ndmp_sess);
cleanup_ndmp_session(ndmp_sess);
return true;
}
/**
* Update the mapping table from logical to physical storage addresses.
*/
bool ndmp_update_storage_mappings(UAContext *ua, STORERES *store)
{
struct ndm_session *ndmp_sess;
if (!get_robot_element_status(ua->jcr, store, &ndmp_sess)) {
return false;
}
ndmp_fill_storage_mappings(store, ndmp_sess);
cleanup_ndmp_session(ndmp_sess);
return true;
}
/**
* Number of slots in a NDMP autochanger.
*/
slot_number_t ndmp_get_num_slots(UAContext *ua, STORERES *store)
{
slot_number_t slots = 0;
/*
* See if the mappings are already determined.
*/
if (!ndmp_update_storage_mappings(ua, store)) {
return slots;
}
return store->rss->storage_mapping.se_count
+ store->rss->storage_mapping.iee_count;
}
/**
* Number of drives in a NDMP autochanger.
*/
drive_number_t ndmp_get_num_drives(UAContext *ua, STORERES *store)
{
drive_number_t drives = 0;
/*
* See if the mappings are already determined.
*/
if (!ndmp_update_storage_mappings(ua, store)) {
return drives;
}
return store->rss->storage_mapping.dte_count;
}
/**
* Move a volume from one slot to an other in a NDMP autochanger.
*/
bool ndmp_transfer_volume(UAContext *ua, STORERES *store,
slot_number_t src_slot, slot_number_t dst_slot)
{
bool retval = false;
slot_number_t slot_mapping;
struct ndm_job_param ndmp_job;
struct ndm_session *ndmp_sess;
/*
* See if this is an autochanger.
*/
if (!store->autochanger || !store->ndmp_changer_device) {
return retval;
}
if (!ndmp_build_storage_job(ua->jcr,
store,
false, /* Setup Tape Agent */
true, /* Setup Robot Agent */
NDM_JOB_OP_MOVE_TAPE,
&ndmp_job)) {
return retval;
}
/*
* Fill in the from and to address.
*
* As the upper level functions work with logical slot numbers convert them
* to physical slot numbers for the actual NDMP operation.
*/
/* slot_mapping = lookup_storage_mapping(store, slot_type_storage, LOGICAL_TO_PHYSICAL, src_slot); */
slot_mapping = get_physical_slotnumber_by_logical_slotnumber(&store->rss->storage_mapping, slot_type_storage, src_slot);
if (slot_mapping == -1) {
ua->error_msg("No slot mapping for slot %hd\n", src_slot);
return retval;
}
ndmp_job.from_addr = slot_mapping;
ndmp_job.from_addr_given = 1;
slot_mapping = get_physical_slotnumber_by_logical_slotnumber(&store->rss->storage_mapping, slot_type_storage, dst_slot);
if (slot_mapping == -1) {
ua->error_msg("No slot mapping for slot %hd\n", dst_slot);
return retval;
}
ndmp_job.to_addr = slot_mapping;
ndmp_job.to_addr_given = 1;
ua->warning_msg(_ ("transferring form slot %hd to slot %hd...\n"), src_slot, dst_slot );
/*
* Set the remote robotics name to use.
* We use the ndmscsi_target_from_str() function which parses the NDMJOB format of a
* device in the form NAME[,[CNUM,]SID[,LUN]
*/
ndmp_job.robot_target = (struct ndmscsi_target *)actuallymalloc(sizeof(struct ndmscsi_target));
if (ndmscsi_target_from_str(ndmp_job.robot_target, store->ndmp_changer_device) != 0) {
actuallyfree(ndmp_job.robot_target);
return retval;
}
ndmp_job.have_robot = 1;
ndmp_job.auto_remedy = 1;
/*
* Initialize a new NDMP session
*/
ndmp_sess = (struct ndm_session *)malloc(sizeof(struct ndm_session));
memset(ndmp_sess, 0, sizeof(struct ndm_session));
if (!ndmp_run_storage_job(ua->jcr, store, ndmp_sess, &ndmp_job)) {
cleanup_ndmp_session(ndmp_sess);
return retval;
}
retval = true;
cleanup_ndmp_session(ndmp_sess);
return retval;
}
/**
* reserve a NDMP Tape drive for a certain job
* lock the devinfo list
* check if any of the devices is available (deviceinfo.JobUsingDevice == 0)
* set the JobId into deviceinfo.JobUsingDevice
* unlock devinfo
* return name of device that was reserved
*/
std::string reserve_ndmp_tapedevice_for_job(STORERES *store, JCR *jcr) {
JobId_t jobid = jcr->JobId;
std::string returnvalue;
P(store->rss->ndmp_deviceinfo_lock);
if (store->rss->ndmp_deviceinfo) {
for (auto devinfo = store->rss->ndmp_deviceinfo->begin();
devinfo != store->rss->ndmp_deviceinfo->end(); devinfo++) {
if (devinfo->JobIdUsingDevice == 0) {
devinfo->JobIdUsingDevice = jobid;
returnvalue = devinfo->device;
Jmsg(jcr, M_INFO, 0, _("successfully reserved NDMP Tape Device %s for job %d\n"),
returnvalue.c_str(), jobid);
break;
} else{
Jmsg(jcr, M_INFO, 0, _("NDMP Tape Device %s is already reserved for for job %d\n"),
devinfo->device.c_str(), devinfo->JobIdUsingDevice);
}
}
}
V(store->rss->ndmp_deviceinfo_lock);
return returnvalue;
}
/*
* remove job from tapedevice
*/
bool unreserve_ndmp_tapedevice_for_job(STORERES *store, JCR *jcr)
{
JobId_t jobid = jcr->JobId;
bool retval = false;
P(store->rss->ndmp_deviceinfo_lock);
if (store->rss->ndmp_deviceinfo) {
for (auto devinfo = store->rss->ndmp_deviceinfo->begin();
devinfo != store->rss->ndmp_deviceinfo->end(); devinfo++) {
if (devinfo->JobIdUsingDevice == jobid) {
devinfo->JobIdUsingDevice = 0;
retval = true;
Jmsg(jcr, M_INFO, 0, _("removed reservation of NDMP Tape Device %s for job %d\n"),
devinfo->device.c_str(), jobid);
break;
}
}
}
V(store->rss->ndmp_deviceinfo_lock);
return retval;
}
/**
* Lookup the drive device name by drive number in a NDMP autochanger.
*/
char *lookup_ndmp_drivename_by_number(STORERES *store, drive_number_t drivenumber)
{
int cnt = 0;
char *tapedevice;
RES *tapedeviceres;
if (store->device) {
foreach_alist(tapedeviceres, store->device) {
if (cnt == drivenumber) {
tapedevice = tapedeviceres->name;
return tapedevice;
}
cnt++;
}
}
return NULL;
}
/**
* Lookup the drive index by device name in a NDMP autochanger.
*/
int lookup_ndmp_driveindex_by_name(STORERES *store, char *drivename)
{
int cnt = 0;
if (!drivename) {
return -1;
}
if (store->rss->ndmp_deviceinfo) {
for (auto devinfo = store->rss->ndmp_deviceinfo->begin();
devinfo != store->rss->ndmp_deviceinfo->end();
devinfo++) {
if ((drivename == devinfo->device)) {
return cnt;
}
cnt++;
}
}
return -1;
}
/**
* Perform an autochanger operation in a NDMP autochanger.
*/
bool ndmp_autochanger_volume_operation(UAContext *ua, STORERES *store, const char *operation,
drive_number_t drive, slot_number_t slot)
{
drive_number_t drive_mapping;
int ndmp_operation;
bool retval = false;
struct ndm_job_param ndmp_job;
struct ndm_session *ndmp_sess;
Dmsg3(100, "ndmp_autochanger_volume_operation: operation %s, drive %hd, slot %hd\n", operation, drive, slot);
ua->warning_msg(_("ndmp_autochanger_volume_operation: operation %s, drive %hd, slot %hd\n"), operation, drive, slot);
/*
* See if this is an autochanger.
*/
if (!store->autochanger || !store->ndmp_changer_device) {
return retval;
}
if (bstrcmp(operation, "unmount") || bstrcmp(operation, "release")) {
ndmp_operation = NDM_JOB_OP_UNLOAD_TAPE;
} else if (bstrcmp(operation, "mount")) {
ndmp_operation = NDM_JOB_OP_LOAD_TAPE;
} else {
ua->error_msg("Illegal autochanger operation %s\n", operation);
return retval;
}
if (!ndmp_build_storage_job(ua->jcr,
store,
false, /* Setup Tape Agent */
true, /* Setup Robot Agent */
ndmp_operation,
&ndmp_job)) {
return retval;
}
/*
* See if the mappings are already determined.
*/
if (!ndmp_update_storage_mappings(ua, store)) {
return false;
}
if (slot >= 0) {
slot_number_t slot_mapping;
/*
* Map the logical address to a physical one.
*/
slot_mapping = get_physical_slotnumber_by_logical_slotnumber(&store->rss->storage_mapping, slot_type_storage, slot);
if (slot_mapping == -1) {
ua->error_msg("No slot mapping for slot %hd\n", slot);
return retval;
}
ndmp_job.from_addr = slot_mapping;
ndmp_job.from_addr_given = 1;
}
/*
* Map the logical address to a physical one.
*/
drive_mapping = get_physical_slotnumber_by_logical_slotnumber(&store->rss->storage_mapping, slot_type_drive, slot);
if (drive_mapping == -1) {
ua->error_msg("No slot mapping for drive %hd\n", drive);
return retval;
}
ndmp_job.drive_addr = drive_mapping;
ndmp_job.drive_addr_given = 1;
/*
* Set the remote robotics name to use.
* We use the ndmscsi_target_from_str() function which parses the NDMJOB format of a
* device in the form NAME[,[CNUM,]SID[,LUN]
*/
ndmp_job.robot_target = (struct ndmscsi_target *)actuallymalloc(sizeof(struct ndmscsi_target));
if (ndmscsi_target_from_str(ndmp_job.robot_target, store->ndmp_changer_device) != 0) {
actuallyfree(ndmp_job.robot_target);
return retval;
}
ndmp_job.have_robot = 1;
ndmp_job.auto_remedy = 1;
/*
* Initialize a new NDMP session
*/
ndmp_sess = (struct ndm_session *)malloc(sizeof(struct ndm_session));
memset(ndmp_sess, 0, sizeof(struct ndm_session));
if (!ndmp_run_storage_job(ua->jcr, store, ndmp_sess, &ndmp_job)) {
cleanup_ndmp_session(ndmp_sess);
return retval;
}
retval = true;
cleanup_ndmp_session(ndmp_sess);
return retval;
}
/**
* Label a volume in a NDMP autochanger.
*/
bool ndmp_send_label_request(UAContext *ua, STORERES *store, MEDIA_DBR *mr,
MEDIA_DBR *omr, POOL_DBR *pr, bool relabel,
drive_number_t drive, slot_number_t slot)
{
bool retval = false;
struct ndm_job_param ndmp_job;
struct ndm_session *ndmp_sess;
struct ndmmedia *media;
Dmsg4(100,"ndmp_send_label_request: VolumeName=%s MediaType=%s PoolName=%s drive=%hd\n", mr->VolumeName, mr->MediaType, pr->Name, drive);
/*
* See if this is an autochanger.
*/
if (!store->autochanger || !store->ndmp_changer_device) {
return retval;
}
if (!ndmp_build_storage_job(ua->jcr,
store,
true, /* Setup Tape Agent */
true, /* Setup Robot Agent */