Welcome to mirror list, hosted at ThFree Co, Russian Federation.

storage.cc « dird « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 05847313558f40b9e15e39a70ae8ad72bad298c7 (plain)
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
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2016 Planets Communications B.V.
   Copyright (C) 2013-2022 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.
*/
/*
 * Kern Sibbald, October MM
 * Extracted from other source files by Marco van Wieringen, February 2016
 */
/**
 * @file
 * Storage specific routines.
 */

#include "include/bareos.h"
#include "dird/dird_globals.h"
#include "dird/director_jcr_impl.h"
#include "dird/sd_cmds.h"
#include "include/auth_protocol_types.h"
#include "lib/parse_conf.h"
#include "lib/util.h"

#if HAVE_NDMP
#  include "ndmp/ndmagents.h"
#endif

#include "dird/ndmp_dma_storage.h"
#include "dird/storage.h"

namespace directordaemon {

/* Forward referenced functions */

/**
 * See if two storage definitions point to the same Storage Daemon.
 *
 * We compare:
 *  - address
 *  - SDport
 *  - password
 */
bool IsSameStorageDaemon(StorageResource* read_storage,
                         StorageResource* write_storage)
{
  return read_storage->SDport == write_storage->SDport
         && Bstrcasecmp(read_storage->address, write_storage->address)
         && Bstrcasecmp(read_storage->password_.value,
                        write_storage->password_.value);
}
// Copy the storage definitions from an alist to the JobControlRecord
void CopyRwstorage(JobControlRecord* jcr,
                   alist<StorageResource*>* storage,
                   const char* where)
{
  if (jcr->JobReads()) { CopyRstorage(jcr, storage, where); }
  CopyWstorage(jcr, storage, where);
}

/**
 * Set storage override.
 * Releases any previous storage definition.
 */
void SetRwstorage(JobControlRecord* jcr, UnifiedStorageResource* store)
{
  if (!store) {
    Jmsg(jcr, M_FATAL, 0, _("No storage specified.\n"));
    return;
  }
  if (jcr->JobReads()) { SetRstorage(jcr, store); }
  SetWstorage(jcr, store);
}

void FreeRwstorage(JobControlRecord* jcr)
{
  FreeRstorage(jcr);
  FreeWstorage(jcr);
}

// Copy the storage definitions from an alist to the JobControlRecord
void CopyRstorage(JobControlRecord* jcr,
                  alist<StorageResource*>* storage,
                  const char* where)
{
  if (storage) {
    StorageResource* store = nullptr;
    if (jcr->dir_impl->res.read_storage_list) {
      delete jcr->dir_impl->res.read_storage_list;
    }
    jcr->dir_impl->res.read_storage_list
        = new alist<StorageResource*>(10, not_owned_by_alist);
    foreach_alist (store, storage) {
      jcr->dir_impl->res.read_storage_list->append(store);
    }
    if (!jcr->dir_impl->res.rstore_source) {
      jcr->dir_impl->res.rstore_source = GetPoolMemory(PM_MESSAGE);
    }
    PmStrcpy(jcr->dir_impl->res.rstore_source, where);
    if (jcr->dir_impl->res.read_storage_list) {
      jcr->dir_impl->res.read_storage
          = (StorageResource*)jcr->dir_impl->res.read_storage_list->first();
    }
  }
}

/**
 * Set storage override.
 * Remove all previous storage.
 */
void SetRstorage(JobControlRecord* jcr, UnifiedStorageResource* store)
{
  StorageResource* storage = nullptr;

  if (!store->store) { return; }
  if (jcr->dir_impl->res.read_storage_list) { FreeRstorage(jcr); }
  if (!jcr->dir_impl->res.read_storage_list) {
    jcr->dir_impl->res.read_storage_list
        = new alist<StorageResource*>(10, not_owned_by_alist);
  }
  jcr->dir_impl->res.read_storage = store->store;
  if (!jcr->dir_impl->res.rstore_source) {
    jcr->dir_impl->res.rstore_source = GetPoolMemory(PM_MESSAGE);
  }
  PmStrcpy(jcr->dir_impl->res.rstore_source, store->store_source);
  foreach_alist (storage, jcr->dir_impl->res.read_storage_list) {
    if (store->store == storage) { return; }
  }
  /* Store not in list, so add it */
  jcr->dir_impl->res.read_storage_list->prepend(store->store);
}

void FreeRstorage(JobControlRecord* jcr)
{
  if (jcr->dir_impl->res.read_storage_list) {
    delete jcr->dir_impl->res.read_storage_list;
    jcr->dir_impl->res.read_storage_list = NULL;
  }
  jcr->dir_impl->res.read_storage = NULL;
}

// Copy the storage definitions from an alist to the JobControlRecord
void CopyWstorage(JobControlRecord* jcr,
                  alist<StorageResource*>* storage,
                  const char* where)
{
  if (storage) {
    StorageResource* st = nullptr;
    if (jcr->dir_impl->res.write_storage_list) {
      delete jcr->dir_impl->res.write_storage_list;
    }
    jcr->dir_impl->res.write_storage_list
        = new alist<StorageResource*>(10, not_owned_by_alist);
    foreach_alist (st, storage) {
      Dmsg1(100, "write_storage_list=%s\n", st->resource_name_);
      jcr->dir_impl->res.write_storage_list->append(st);
    }
    if (!jcr->dir_impl->res.wstore_source) {
      jcr->dir_impl->res.wstore_source = GetPoolMemory(PM_MESSAGE);
    }
    PmStrcpy(jcr->dir_impl->res.wstore_source, where);
    if (jcr->dir_impl->res.write_storage_list) {
      jcr->dir_impl->res.write_storage
          = (StorageResource*)jcr->dir_impl->res.write_storage_list->first();
      Dmsg2(100, "write_storage=%s where=%s\n",
            jcr->dir_impl->res.write_storage->resource_name_,
            jcr->dir_impl->res.wstore_source);
    }
  }
}

/**
 * Set storage override.
 * Remove all previous storage.
 */
void SetWstorage(JobControlRecord* jcr, UnifiedStorageResource* store)
{
  StorageResource* storage = nullptr;

  if (!store->store) { return; }
  if (jcr->dir_impl->res.write_storage_list) { FreeWstorage(jcr); }
  if (!jcr->dir_impl->res.write_storage_list) {
    jcr->dir_impl->res.write_storage_list
        = new alist<StorageResource*>(10, not_owned_by_alist);
  }
  jcr->dir_impl->res.write_storage = store->store;
  if (!jcr->dir_impl->res.wstore_source) {
    jcr->dir_impl->res.wstore_source = GetPoolMemory(PM_MESSAGE);
  }
  PmStrcpy(jcr->dir_impl->res.wstore_source, store->store_source);
  Dmsg2(50, "write_storage=%s where=%s\n",
        jcr->dir_impl->res.write_storage->resource_name_,
        jcr->dir_impl->res.wstore_source);
  foreach_alist (storage, jcr->dir_impl->res.write_storage_list) {
    if (store->store == storage) { return; }
  }

  // Store not in list, so add it
  jcr->dir_impl->res.write_storage_list->prepend(store->store);
}

void FreeWstorage(JobControlRecord* jcr)
{
  if (jcr->dir_impl->res.write_storage_list) {
    delete jcr->dir_impl->res.write_storage_list;
    jcr->dir_impl->res.write_storage_list = NULL;
  }
  jcr->dir_impl->res.write_storage = NULL;
}

/**
 * For NDMP backup we can setup the backup to run to a NDMP instance
 * of the same Storage Daemon that also does native native backups.
 * This way a Normal Storage Daemon can perform NDMP protocol based
 * saves and restores.
 */
void SetPairedStorage(JobControlRecord* jcr)
{
  StorageResource *store = nullptr, *paired_read_write_storage = nullptr;

  switch (jcr->getJobType()) {
    case JT_BACKUP:
      // For a backup we look at the write storage.
      if (jcr->dir_impl->res.write_storage_list) {
        /*
         * Setup the jcr->dir_impl_->res.write_storage_list to point to all
         * paired_storage entries of all the storage currently in the
         * jcrres.->write_storage_list. Save the original list under
         * jcr->dir_impl_->res.paired_read_write_storage_list.
         */
        jcr->dir_impl->res.paired_read_write_storage_list
            = jcr->dir_impl->res.write_storage_list;
        jcr->dir_impl->res.write_storage_list
            = new alist<StorageResource*>(10, not_owned_by_alist);
        foreach_alist (store,
                       jcr->dir_impl->res.paired_read_write_storage_list) {
          if (store->paired_storage) {
            Dmsg1(100, "write_storage_list=%s\n",
                  store->paired_storage->resource_name_);
            jcr->dir_impl->res.write_storage_list->append(
                store->paired_storage);
          }
        }

        /*
         * Swap the actual jcr->dir_impl_->res.write_storage to point to the
         * paired storage entry. We save the actual storage entry in
         * paired_read_write_storage which is for restore in the
         * FreePairedStorage() function.
         */
        store = jcr->dir_impl->res.write_storage;
        if (store->paired_storage) {
          jcr->dir_impl->res.write_storage = store->paired_storage;
          jcr->dir_impl->res.paired_read_write_storage = store;
        }
      } else {
        Jmsg(jcr, M_FATAL, 0,
             _("No write storage, don't know how to setup paired storage\n"));
      }
      break;
    case JT_RESTORE:
      // For a restores we look at the read storage.
      if (jcr->dir_impl->res.read_storage_list) {
        /*
         * Setup the jcr->dir_impl_->res.paired_read_write_storage_list to point
         * to all paired_storage entries of all the storage currently in the
         * jcr->dir_impl_->res.read_storage_list.
         */
        jcr->dir_impl->res.paired_read_write_storage_list
            = new alist<StorageResource*>(10, not_owned_by_alist);
        foreach_alist (paired_read_write_storage,
                       jcr->dir_impl->res.read_storage_list) {
          store = (StorageResource*)my_config->GetNextRes(R_STORAGE, NULL);
          while (store) {
            if (store->paired_storage == paired_read_write_storage) { break; }

            store = (StorageResource*)my_config->GetNextRes(
                R_STORAGE, (BareosResource*)store);
          }

          /*
           * See if we found a store that has the current
           * paired_read_write_storage as its paired storage.
           */
          if (store) {
            jcr->dir_impl->res.paired_read_write_storage_list->append(store);

            /*
             * If the current processed paired_read_write_storage is also the
             * current entry in jcr->dir_impl_->res.read_storage update the
             * jcr->paired_read_write_storage to point to this storage entry.
             */
            if (paired_read_write_storage == jcr->dir_impl->res.read_storage) {
              jcr->dir_impl->res.paired_read_write_storage = store;
            }
          }
        }
      } else {
        Jmsg(jcr, M_FATAL, 0,
             _("No read storage, don't know how to setup paired storage\n"));
      }
      break;
    case JT_MIGRATE:
    case JT_COPY:
      // For a migrate or copy we look at the read storage.
      if (jcr->dir_impl->res.read_storage_list) {
        /*
         * Setup the jcr->dir_impl_->res.read_storage_list to point to all
         * paired_storage entries of all the storage currently in the
         * jcr->dir_impl_->res.read_storage_list. Save the original list under
         * jcr->dir_impl_->res.paired_read_write_storage_list.
         */
        jcr->dir_impl->res.paired_read_write_storage_list
            = jcr->dir_impl->res.read_storage_list;
        jcr->dir_impl->res.read_storage_list
            = new alist<StorageResource*>(10, not_owned_by_alist);
        foreach_alist (store,
                       jcr->dir_impl->res.paired_read_write_storage_list) {
          if (store->paired_storage) {
            Dmsg1(100, "read_storage_list=%s\n",
                  store->paired_storage->resource_name_);
            jcr->dir_impl->res.read_storage_list->append(store->paired_storage);
          }
        }

        /*
         * Swap the actual jcr->dir_impl_->res.read_storage to point to the
         * paired storage entry. We save the actual storage entry in
         * paired_read_write_storage which is for restore in the
         * FreePairedStorage() function.
         */
        store = jcr->dir_impl->res.read_storage;
        if (store->paired_storage) {
          jcr->dir_impl->res.read_storage = store->paired_storage;
          jcr->dir_impl->res.paired_read_write_storage = store;
        }
      } else {
        Jmsg(jcr, M_FATAL, 0,
             _("No read storage, don't know how to setup paired storage\n"));
      }
      break;
    default:
      Jmsg(jcr, M_FATAL, 0,
           _("Unknown Job Type %s, don't know how to setup paired storage\n"),
           job_type_to_str(jcr->getJobType()));
      break;
  }
}

/**
 * This performs an undo of the actions the SetPairedStorage() function
 * performed. We reset the storage write storage back to its original
 * and remove the paired storage override if any.
 */
void FreePairedStorage(JobControlRecord* jcr)
{
  if (jcr->dir_impl->res.paired_read_write_storage_list) {
    switch (jcr->getJobType()) {
      case JT_BACKUP:
        // For a backup we look at the write storage.
        if (jcr->dir_impl->res.write_storage_list) {
          /*
           * The jcr->dir_impl_->res.write_storage_list contain a set of paired
           * storages. We just delete it content and swap back to the real
           * master storage.
           */
          delete jcr->dir_impl->res.write_storage_list;
          jcr->dir_impl->res.write_storage_list
              = jcr->dir_impl->res.paired_read_write_storage_list;
          jcr->dir_impl->res.paired_read_write_storage_list = NULL;
          jcr->dir_impl->res.write_storage
              = jcr->dir_impl->res.paired_read_write_storage;
          jcr->dir_impl->res.paired_read_write_storage = NULL;
        }
        break;
      case JT_RESTORE:
        /*
         * The jcr->dir_impl_->res.read_storage_list contain a set of paired
         * storages. For the read we created a list of alternative storage which
         * we can just drop now.
         */
        delete jcr->dir_impl->res.paired_read_write_storage_list;
        jcr->dir_impl->res.paired_read_write_storage_list = NULL;
        jcr->dir_impl->res.paired_read_write_storage = NULL;
        break;
      case JT_MIGRATE:
      case JT_COPY:
        // For a migrate or copy we look at the read storage.
        if (jcr->dir_impl->res.read_storage_list) {
          /*
           * The jcr->dir_impl_->res.read_storage_list contains a set of paired
           * storages. We just delete it content and swap back to the real
           * master storage.
           */
          delete jcr->dir_impl->res.read_storage_list;
          jcr->dir_impl->res.read_storage_list
              = jcr->dir_impl->res.paired_read_write_storage_list;
          jcr->dir_impl->res.paired_read_write_storage_list = NULL;
          jcr->dir_impl->res.read_storage
              = jcr->dir_impl->res.paired_read_write_storage;
          jcr->dir_impl->res.paired_read_write_storage = NULL;
        }
        break;
      default:
        Jmsg(jcr, M_FATAL, 0,
             _("Unknown Job Type %s, don't know how to free paired storage\n"),
             job_type_to_str(jcr->getJobType()));
        break;
    }
  }
}

// Check if every possible storage has paired storage associated.
bool HasPairedStorage(JobControlRecord* jcr)
{
  StorageResource* store = nullptr;

  switch (jcr->getJobType()) {
    case JT_BACKUP:
      // For a backup we look at the write storage.
      if (jcr->dir_impl->res.write_storage_list) {
        foreach_alist (store, jcr->dir_impl->res.write_storage_list) {
          if (!store->paired_storage) { return false; }
        }
      } else {
        Jmsg(jcr, M_FATAL, 0,
             _("No write storage, don't know how to check for paired "
               "storage\n"));
        return false;
      }
      break;
    case JT_RESTORE:
    case JT_MIGRATE:
    case JT_COPY:
      if (jcr->dir_impl->res.read_storage_list) {
        foreach_alist (store, jcr->dir_impl->res.read_storage_list) {
          if (!store->paired_storage) { return false; }
        }
      } else {
        Jmsg(
            jcr, M_FATAL, 0,
            _("No read storage, don't know how to check for paired storage\n"));
        return false;
      }
      break;
    default:
      Jmsg(jcr, M_FATAL, 0,
           _("Unknown Job Type %s, don't know how to free paired storage\n"),
           job_type_to_str(jcr->getJobType()));
      return false;
  }

  return true;
}

#define MAX_TRIES 6 * 360 /* 6 hours (10 sec intervals) */

// Change the read storage resource for the current job.
bool SelectNextRstore(JobControlRecord* jcr, bootstrap_info& info)
{
  UnifiedStorageResource ustore;

  if (bstrcmp(jcr->dir_impl->res.read_storage->resource_name_, info.storage)) {
    return true; /* Same SD nothing to change */
  }

  if (!(ustore.store = (StorageResource*)my_config->GetResWithName(
            R_STORAGE, info.storage))) {
    Jmsg(jcr, M_FATAL, 0, _("Could not get storage resource '%s'.\n"),
         info.storage);
    jcr->setJobStatusWithPriorityCheck(JS_ErrorTerminated);
    return false;
  }

  /*
   * We start communicating with a new storage daemon so close the
   * old connection when it is still open.
   */
  if (jcr->store_bsock) {
    jcr->store_bsock->close();
    delete jcr->store_bsock;
    jcr->store_bsock = NULL;
  }

  // Release current read storage and get a new one
  DecReadStore(jcr);
  FreeRstorage(jcr);
  SetRstorage(jcr, &ustore);
  jcr->setJobStatusWithPriorityCheck(JS_WaitSD);

  // Wait for up to 6 hours to increment read stoage counter
  for (int i = 0; i < MAX_TRIES; i++) {
    // Try to get read storage counter incremented
    if (IncReadStore(jcr)) {
      jcr->setJobStatusWithPriorityCheck(JS_Running);
      return true;
    }
    Bmicrosleep(10, 0); /* Sleep 10 secs */
    if (JobCanceled(jcr)) {
      FreeRstorage(jcr);
      return false;
    }
  }

  // Failed to IncReadStore()
  FreeRstorage(jcr);
  Jmsg(jcr, M_FATAL, 0, _("Could not acquire read storage lock for \"%s\""),
       info.storage);
  return false;
}

void StorageStatus(UaContext* ua, StorageResource* store, char* cmd)
{
  switch (store->Protocol) {
    case APT_NATIVE:
      return DoNativeStorageStatus(ua, store, cmd);
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      return DoNdmpStorageStatus(ua, store, cmd);
    default:
      break;
  }
}

// Simple comparison function for binary insert of vol_list_t
int StorageCompareVolListEntry(vol_list_t* e1, vol_list_t* e2)
{
  vol_list_t *v1, *v2;

  v1 = e1;
  v2 = e2;

  ASSERT(v1);
  ASSERT(v2);

  if (v1->element_address == v2->element_address) {
    return 0;
  } else {
    return (v1->element_address < v2->element_address) ? -1 : 1;
  }
}

static inline void FreeVolList(changer_vol_list_t* vol_list)
{
  vol_list_t* vl;

  // make sure cache is treated as empty
  vol_list->timestamp = 0;

  if (vol_list->contents) {
    foreach_dlist (vl, vol_list->contents) {
      if (vl->VolName) { free(vl->VolName); }
    }
    vol_list->contents->destroy();
    delete vol_list->contents;
    vol_list->contents = NULL;
  } else {
    Dmsg0(100, "FreeVolList: vol_list->contents already empty\n");
  }
}

// Generic routine to get the content of a storage autochanger.
changer_vol_list_t* get_vol_list_from_storage(UaContext* ua,
                                              StorageResource* store,
                                              bool listall,
                                              bool scan,
                                              bool cached)
{
  vol_list_type type;
  dlist<vol_list_t>* contents = NULL;
  changer_vol_list_t* vol_list = NULL;

  lock_mutex(store->runtime_storage_status->changer_lock);

  if (listall) {
    type = VOL_LIST_ALL;
  } else {
    type = VOL_LIST_PARTIAL;
  }

  // Do we have a cached version of the content that is still valid ?
  if (store->runtime_storage_status->vol_list) {
    utime_t now;

    now = (utime_t)time(NULL);

    // Are we allowed to return a cached list ?
    if (cached && store->runtime_storage_status->vol_list->type == type) {
      if ((now - store->runtime_storage_status->vol_list->timestamp)
          <= store->cache_status_interval) {
        Dmsg0(100, "Using cached storage status\n");
        vol_list = store->runtime_storage_status->vol_list;
        vol_list->reference_count++;
        goto bail_out;
      }
    }

    /*
     * Cached version expired or want non-cached version or wrong type.
     * Remove the cached contents and retrieve the new contents from the
     * autochanger.
     */
    Dmsg0(100, "Freeing volume list\n");
    if (store->runtime_storage_status->vol_list->reference_count == 0) {
      FreeVolList(store->runtime_storage_status->vol_list);
    } else {
      /*
       * Need to cleanup but things are still referenced.
       * We just remove the old changer_vol_list_t and on the next call to
       * StorageReleaseVolList() this orphaned changer_vol_list_t will
       * then be destroyed.
       */
      Dmsg0(100, "Need to free still referenced vol_list\n");
      store->runtime_storage_status->vol_list
          = (changer_vol_list_t*)malloc(sizeof(changer_vol_list_t));
      changer_vol_list_t empty_vol_list;
      *store->runtime_storage_status->vol_list = empty_vol_list;
    }
  }

  // Nothing cached or uncached data wanted so perform retrieval.
  switch (store->Protocol) {
    case APT_NATIVE:
      contents = native_get_vol_list(ua, store, listall, scan);
      break;
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      contents = ndmp_get_vol_list(ua, store, listall, scan);
      break;
    default:
      break;
  }

  if (contents) {
    // Cache the returned content of the autochanger.
    if (!store->runtime_storage_status->vol_list) {
      store->runtime_storage_status->vol_list
          = (changer_vol_list_t*)malloc(sizeof(changer_vol_list_t));
      changer_vol_list_t empty_vol_list;
      *store->runtime_storage_status->vol_list = empty_vol_list;
    }
    vol_list = store->runtime_storage_status->vol_list;
    vol_list->reference_count++;
    vol_list->contents = contents;
    vol_list->timestamp = (utime_t)time(NULL);
    vol_list->type = type;
  }

bail_out:
  unlock_mutex(store->runtime_storage_status->changer_lock);

  return vol_list;
}

slot_number_t GetNumSlots(UaContext* ua, StorageResource* store)
{
  slot_number_t slots = 0;

  // See if we can use the cached number of slots.
  if (store->runtime_storage_status->slots > 0) {
    return store->runtime_storage_status->slots;
  }

  lock_mutex(store->runtime_storage_status->changer_lock);

  switch (store->Protocol) {
    case APT_NATIVE:
      slots = NativeGetNumSlots(ua, store);
      break;
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      slots = NdmpGetNumSlots(ua, store);
      break;
    default:
      break;
  }

  store->runtime_storage_status->slots = slots;

  unlock_mutex(store->runtime_storage_status->changer_lock);

  return slots;
}

slot_number_t GetNumDrives(UaContext* ua, StorageResource* store)
{
  drive_number_t drives = 0;

  // See if we can use the cached number of drives.
  if (store->runtime_storage_status->drives > 0) {
    return store->runtime_storage_status->drives;
  }

  lock_mutex(store->runtime_storage_status->changer_lock);

  switch (store->Protocol) {
    case APT_NATIVE:
      drives = NativeGetNumDrives(ua, store);
      break;
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      drives = NdmpGetNumDrives(ua, store);
      break;
    default:
      break;
  }

  store->runtime_storage_status->drives = drives;

  unlock_mutex(store->runtime_storage_status->changer_lock);

  return drives;
}

bool transfer_volume(UaContext* ua,
                     StorageResource* store,
                     slot_number_t src_slot,
                     slot_number_t dst_slot)
{
  bool retval = false;

  lock_mutex(store->runtime_storage_status->changer_lock);

  switch (store->Protocol) {
    case APT_NATIVE:
      retval = NativeTransferVolume(ua, store, src_slot, dst_slot);
      break;
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      retval = NdmpTransferVolume(ua, store, src_slot, dst_slot);
      break;
    default:
      break;
  }

  unlock_mutex(store->runtime_storage_status->changer_lock);

  return retval;
}

bool DoAutochangerVolumeOperation(UaContext* ua,
                                  StorageResource* store,
                                  const char* operation,
                                  drive_number_t drive,
                                  slot_number_t slot)
{
  bool retval = false;

  lock_mutex(store->runtime_storage_status->changer_lock);

  switch (store->Protocol) {
    case APT_NATIVE:
      retval
          = NativeAutochangerVolumeOperation(ua, store, operation, drive, slot);
      break;
    case APT_NDMPV2:
    case APT_NDMPV3:
    case APT_NDMPV4:
      retval
          = NdmpAutochangerVolumeOperation(ua, store, operation, drive, slot);
      break;
    default:
      break;
  }

  unlock_mutex(store->runtime_storage_status->changer_lock);

  return retval;
}

// See if a specific slot is loaded in one of the drives.
vol_list_t* vol_is_loaded_in_drive(StorageResource*,
                                   changer_vol_list_t* vol_list,
                                   slot_number_t slot)
{
  vol_list_t* vl;

  vl = (vol_list_t*)vol_list->contents->first();
  while (vl) {
    switch (vl->slot_type) {
      case slot_type_t::kSlotTypeDrive:
        Dmsg2(100, "Checking drive %hd for loaded volume == %hd\n",
              vl->bareos_slot_number, vl->currently_loaded_slot_number);
        if (vl->currently_loaded_slot_number == slot) { return vl; }
        break;
      default:
        break;
    }
    vl = vol_list->contents->next(vl);
  }

  return NULL;
}

/**
 * Release the reference to the volume list returned from
 * get_vol_list_from_storage()
 */
void StorageReleaseVolList(StorageResource* store, changer_vol_list_t* vol_list)
{
  lock_mutex(store->runtime_storage_status->changer_lock);

  Dmsg0(100, "Releasing volume list\n");

  // See if we are releasing a reference to the currently cached value.
  if (store->runtime_storage_status->vol_list == vol_list) {
    vol_list->reference_count--;
  } else {
    vol_list->reference_count--;
    if (vol_list->reference_count == 0) {
      /*
       * It seems this is a release of an uncached version of the vol_list.
       * We just destroy this vol_list as there are no more references to it.
       */
      FreeVolList(vol_list);
      free(vol_list);
    }
  }

  unlock_mutex(store->runtime_storage_status->changer_lock);
}

// Destroy the volume list returned from get_vol_list_from_storage()
void StorageFreeVolList(StorageResource* store, changer_vol_list_t* vol_list)
{
  lock_mutex(store->runtime_storage_status->changer_lock);

  Dmsg1(100, "Freeing volume list at %p\n", vol_list);

  FreeVolList(vol_list);

  // Clear the cached vol_list if needed.
  if (store->runtime_storage_status->vol_list == vol_list) {
    store->runtime_storage_status->vol_list = NULL;
  }

  unlock_mutex(store->runtime_storage_status->changer_lock);
}

/**
 * Invalidate a cached volume list returned from get_vol_list_from_storage()
 * Called by functions that change the content of the storage like mount,
 * umount, release.
 */
void InvalidateVolList(StorageResource* store)
{
  lock_mutex(store->runtime_storage_status->changer_lock);

  if (store->runtime_storage_status->vol_list) {
    Dmsg1(100, "Invalidating volume list at %p\n",
          store->runtime_storage_status->vol_list);

    /*
     * If the volume list is unreferenced we can destroy it otherwise we just
     * reset the pointer and the StorageReleaseVolList() will destroy it for
     * us the moment there are no more references.
     */
    if (store->runtime_storage_status->vol_list->reference_count == 0) {
      FreeVolList(store->runtime_storage_status->vol_list);
    } else {
      store->runtime_storage_status->vol_list = NULL;
    }
  }

  unlock_mutex(store->runtime_storage_status->changer_lock);
}


} /* namespace directordaemon */