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

droplet_device.cc « backends « stored « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b3d3bd0cebd12ddcdf5c620e3e3dda506191f552 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2014-2017 Planets Communications B.V.
   Copyright (C) 2014-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
   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, February 2014
 *
 * Object Storage API device abstraction.
 *
 * Stacking is the following:
 *
 *   DropletDevice::
 *         |
 *         v
 *   ChunkedDevice::
 *         |
 *         v
 *       Device::
 *
 */
/**
 * @file
 * Object Storage API device abstraction.
 */

#include "include/bareos.h"

#include "stored/stored.h"
#include "stored/sd_backends.h"
#include "chunked_device.h"
#include "droplet_device.h"
#include "lib/edit.h"

#include <string>

namespace storagedaemon {

// Options that can be specified for this device type.
enum device_option_type
{
  argument_none = 0,
  argument_profile,
  argument_location,
  argument_canned_acl,
  argument_storage_class,
  argument_bucket,
  argument_chunksize,
  argument_iothreads,
  argument_ioslots,
  argument_retries,
  argument_mmap
};

struct device_option {
  const char* name;
  enum device_option_type type;
  int compare_size;
};

static device_option device_options[]
    = {{"profile=", argument_profile, 8},
       {"location=", argument_location, 9},
       {"acl=", argument_canned_acl, 4},
       {"storageclass=", argument_storage_class, 13},
       {"bucket=", argument_bucket, 7},
       {"chunksize=", argument_chunksize, 10},
       {"iothreads=", argument_iothreads, 10},
       {"ioslots=", argument_ioslots, 8},
       {"retries=", argument_retries, 8},
       {"mmap", argument_mmap, 4},
       {NULL, argument_none, 0}};

static int droplet_reference_count = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

// Generic log function that glues libdroplet with BAREOS.
static void DropletDeviceLogfunc(dpl_ctx_t*,
                                 dpl_log_level_t level,
                                 const char* message)
{
  switch (level) {
    case DPL_DEBUG:
      Dmsg1(100, "%s\n", message);
      break;
    case DPL_INFO:
      Emsg1(M_INFO, 0, "%s\n", message);
      break;
    case DPL_WARNING:
      Emsg1(M_WARNING, 0, "%s\n", message);
      break;
    case DPL_ERROR:
      Emsg1(M_ERROR, 0, "%s\n", message);
      break;
    default:
      break;
  }
}

// Map the droplet errno's to system ones.
static inline int DropletErrnoToSystemErrno(dpl_status_t status)
{
  switch (status) {
    case DPL_ENOENT:
      errno = ENOENT;
      break;
    case DPL_ETIMEOUT:
      errno = ETIMEDOUT;
      break;
    case DPL_ENOMEM:
      errno = ENOMEM;
      break;
    case DPL_EIO:
      errno = EIO;
      break;
    case DPL_ENAMETOOLONG:
      errno = ENAMETOOLONG;
      break;
    case DPL_ENOTDIR:
      errno = ENOTDIR;
      break;
    case DPL_ENOTEMPTY:
      errno = ENOTEMPTY;
      break;
    case DPL_EISDIR:
      errno = EISDIR;
      break;
    case DPL_EEXIST:
      errno = EEXIST;
      break;
    case DPL_EPERM:
      errno = EPERM;
      break;
    case DPL_FAILURE: /**< General failure */
      errno = EIO;
      break;
    default:
      errno = EINVAL;
      break;
  }

  return errno;
}


// Callback for getting the total size of a chunked volume.
static dpl_status_t chunked_volume_size_callback(dpl_sysmd_t* sysmd,
                                                 dpl_ctx_t*,
                                                 const char*,
                                                 void* data)
{
  dpl_status_t status = DPL_SUCCESS;
  ssize_t* volumesize = (ssize_t*)data;

  *volumesize = *volumesize + sysmd->size;

  return status;
}

/*
 * Callback for truncating a chunked volume.
 *
 * @return DPL_SUCCESS on success, on error: a dpl_status_t value that
 * represents the error.
 */
static dpl_status_t chunked_volume_truncate_callback(dpl_sysmd_t*,
                                                     dpl_ctx_t* ctx,
                                                     const char* chunkpath,
                                                     void*)
{
  dpl_status_t status = DPL_SUCCESS;

  status = dpl_unlink(ctx, chunkpath);

  switch (status) {
    case DPL_SUCCESS:
      break;
    default:
      /* no error message here, as error will be set by calling function. */
      return status;
  }

  return status;
}


/*
 * Generic function that walks a dirname and calls the callback
 * function for each entry it finds in that directory.
 *
 * @return: true - if no error occured
 *          false - if an error has occured. Sets dev_errno and errmsg to the
 * first error.
 */
bool DropletDevice::ForEachChunkInDirectoryRunCallback(
    const char* dirname,
    t_dpl_walk_chunks_call_back callback,
    void* data,
    bool ignore_gaps)
{
  bool retval = true;
  dpl_status_t status;
  dpl_status_t callback_status = DPL_FAILURE;
  PoolMem path(PM_NAME);

  bool found = true;
  int i = 0;
  int tries = 0;

  while ((i < max_chunks_) && (found) && (retval)) {
    path.bsprintf("%s/%04d", dirname, i);

    auto sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_getattr(ctx_,         /* context */
                         path.c_str(), /* locator */
                         nullptr,      /* metadata */
                         sysmd);       /* sysmd */

    switch (status) {
      case DPL_SUCCESS:
        Dmsg1(100, "chunk %s exists. Calling callback.\n", path.c_str());
        callback_status = callback(sysmd, ctx_, path.c_str(), data);
        if (callback_status == DPL_SUCCESS) {
          i++;
        } else {
          Mmsg2(errmsg, _("Operation failed on chunk %s: ERR=%s."),
                path.c_str(), dpl_status_str(callback_status));
          dev_errno = DropletErrnoToSystemErrno(callback_status);
          /* exit loop */
          retval = false;
        }
        break;
      case DPL_ENOENT:
        if (ignore_gaps) {
          Dmsg1(1000, "chunk %s does not exist. Skipped.\n", path.c_str());
          i++;
        } else {
          Dmsg1(100, "chunk %s does not exist. Exiting.\n", path.c_str());
          found = false;
        }
        break;
      default:
        ++tries;
        if (tries < NUMBER_OF_RETRIES) {
          Dmsg2(100, "chunk %s failure: %s. Try again (%d).\n", path.c_str(),
                dpl_status_str(callback_status), tries);
          Bmicrosleep(INFLIGT_RETRY_TIME, 0);
        } else {
          Dmsg2(100, "chunk %s failure: %s. Exiting after %d tries.\n",
                path.c_str(), dpl_status_str(callback_status), tries);
          found = false;
        }
        break;
    }
    if (sysmd) {
      dpl_sysmd_free(sysmd);
      sysmd = nullptr;
    }
  }

  return retval;
}

/**
 * Check if a specific path exists.
 * It uses dpl_getattr() for this.
 * However, dpl_getattr() results wrong results in a couple of situations,
 * espescially directoy names should not be checked using a prepended "/".
 *
 * Results in detail:
 *
 * path      | "name"  | "name/" | target reachable | target not reachable  |
 * target not reachable | wrong credentials | exists  | exists  | | (already
 * initialized) | (not initialized)    |
 * -------------------------------------------------------------------------------------------------------------------
 * ""        | -       | yes     | DPL_SUCCESS      | DPL_SUCCESS (!)       |
 * DPL_FAILURE          | DPL_EPERM
 * "/"       | yes     | -       | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM
 *
 * "name"    | -       | -       | DPL_ENOENT       | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM
 * "/name"   | -       | -       | DPL_ENOENT       | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM "name/"   | -       | -       | DPL_ENOENT
 * | DPL_FAILURE           | DPL_FAILURE          | DPL_EPERM
 * "/name/"  | -       | -       | DPL_SUCCESS (!)  | DPL_SUCCESS (!)       |
 * DPL_SUCCESS (!)      | DPL_SUCCESS (!)
 *
 * "name"    | yes     | -       | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM
 * "/name"   | yes     | -       | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM "name/"   | yes     | -       | DPL_ENOTDIR
 * | DPL_FAILURE           | DPL_FAILURE          | DPL_EPERM
 * "/name/"  | yes     | -       | DPL_SUCCESS (!)  | DPL_SUCCESS (!)       |
 * DPL_SUCCESS (!)      | DPL_SUCCESS (!)
 *
 * "name"    | -       | yes     | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM
 * "/name"   | -       | yes     | DPL_ENOENT  (!)  | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM "name/"   | -       | yes     | DPL_SUCCESS
 * | DPL_FAILURE           | DPL_FAILURE          | DPL_EPERM
 * "/name/"  | -       | yes     | DPL_SUCCESS      | DPL_SUCCESS (!)       |
 * DPL_SUCCESS (!)      | DPL_SUCCESS (!)
 *
 * "name"    | yes     | yes     | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM
 * "/name"   | yes     | yes     | DPL_SUCCESS      | DPL_FAILURE           |
 * DPL_FAILURE          | DPL_EPERM "name/"   | yes     | yes     | DPL_SUCCESS
 * | DPL_FAILURE           | DPL_FAILURE          | DPL_EPERM
 * "/name/"  | yes     | yes     | DPL_SUCCESS      | DPL_SUCCESS (!)       |
 * DPL_SUCCESS (!)      | DPL_SUCCESS (!)
 *
 * Best test for
 *   directories as "dir/"
 *   files as "file" or "/file".
 *
 * Returns DPL_SUCCESS     - if path exists and can be accessed
 *         DPL_* errorcode - otherwise
 */
dpl_status_t DropletDevice::check_path(const char* path)
{
  dpl_status_t status;
  const char* retry = "";

  int tries = 0;
  bool success = false;

  do {
    auto sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_getattr(ctx_,   /* context */
                         path,   /* locator */
                         NULL,   /* metadata */
                         sysmd); /* sysmd */
    Dmsg4(100, "%scheck_path: path=<%s> (device=%s, bucket=%s): Result %s\n",
          retry, path, prt_name, ctx_->cur_bucket, dpl_status_str(status));
    dpl_sysmd_free(sysmd);

    if (status == DPL_SUCCESS || status == DPL_ENOENT) {
      success = true;
    } else {
      retry = "Retry: ";
      ++tries;
      Bmicrosleep(INFLIGT_RETRY_TIME, 0);
    }

  } while (tries < NUMBER_OF_RETRIES && !success);

  return status;
}

/**
 * Checks if the connection to the backend storage system is possible.
 *
 * Returns true  - if connection can be established
 *         false - otherwise
 */
bool DropletDevice::CheckRemoteConnection()
{
  if (!ctx_) {
    if (!initialize()) { return false; }
  }

  auto status = check_path("bareos-test/");

  char* h = dpl_addrlist_get(ctx_->addrlist);
  std::string hostaddr{h != nullptr ? h : "???"};
  free(h);

  switch (status) {
    case DPL_SUCCESS:
    case DPL_ENOENT:
      Dmsg1(100, "Host is accessible: %s\n", hostaddr.c_str());
      return true;
    default:
      Dmsg2(100, "Cannot reach host: %s (%s)\n ", hostaddr.c_str(),
            dpl_status_str(status));
      return false;
  }
}

/*
 * Internal method for flushing a chunk to the backing store.
 * This does the real work either by being called from a
 * io-thread or directly blocking the device.
 */
bool DropletDevice::FlushRemoteChunk(chunk_io_request* request)
{
  bool retval = false;
  dpl_status_t status;
  dpl_option_t dpl_options;
  dpl_sysmd_t* sysmd = NULL;
  PoolMem chunk_dir(PM_FNAME), chunk_name(PM_FNAME);

  Mmsg(chunk_dir, "/%s", request->volname);
  Mmsg(chunk_name, "%s/%04d", chunk_dir.c_str(), request->chunk);

  // Set that we are uploading the chunk.
  if (!SetInflightChunk(request)) { return false; }

  int tries = 0;
  bool success = false;

  do {
    Dmsg1(100, "Flushing chunk %s\n", chunk_name.c_str());

    /*
     * Check on the remote backing store if the chunk already exists.
     * We only upload this chunk if it is bigger then the chunk that exists
     * on the remote backing store. When using io-threads it could happen
     * that there are multiple flush requests for the same chunk when a
     * chunk is reused in a next backup job. We only want the chunk with
     * the biggest amount of valid data to persist as we only append to
     * chunks.
     */
    sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_getattr(ctx_,               /* context */
                         chunk_name.c_str(), /* locator */
                         NULL,               /* metadata */
                         sysmd);             /* sysmd */

    switch (status) {
      case DPL_SUCCESS:
        if (sysmd->size > request->wbuflen) {
          success = true;
          goto bail_out;
        }
        break;
      default:
        // Check on the remote backing store if the chunkdir exists.
        dpl_sysmd_free(sysmd);
        sysmd = dpl_sysmd_dup(&sysmd_);
        status = dpl_getattr(ctx_,              /* context */
                             chunk_dir.c_str(), /* locator */
                             NULL,              /* metadata */
                             sysmd);            /* sysmd */

        switch (status) {
          case DPL_SUCCESS:
            break;
          case DPL_ENOENT:
          case DPL_FAILURE:
            /*
             * Make sure the chunk directory with the name of the volume
             * exists.
             */
            dpl_sysmd_free(sysmd);
            sysmd = dpl_sysmd_dup(&sysmd_);
            status = dpl_mkdir(ctx_,              /* context */
                               chunk_dir.c_str(), /* locator */
                               NULL,              /* metadata */
                               sysmd);            /* sysmd */

            switch (status) {
              case DPL_SUCCESS:
                break;
              default:
                Mmsg2(errmsg,
                      _("Failed to create directory %s using dpl_mkdir(): "
                        "ERR=%s.\n"),
                      chunk_dir.c_str(), dpl_status_str(status));
                dev_errno = DropletErrnoToSystemErrno(status);
                Bmicrosleep(INFLIGT_RETRY_TIME, 0);
                tries++;
                goto again1;
            }
            break;
          default:
            break;
        }
        break;
    }

    /*
     * Create some options for libdroplet.
     *
     * DPL_OPTION_NOALLOC - we provide the buffer to copy the data into
     *                      no need to let the library allocate memory we
     *                      need to free after copying the data.
     */
    memset(&dpl_options, 0, sizeof(dpl_options));
    dpl_options.mask |= DPL_OPTION_NOALLOC;

    dpl_sysmd_free(sysmd);
    sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_fput(ctx_,                   /* context */
                      chunk_name.c_str(),     /* locator */
                      &dpl_options,           /* options */
                      NULL,                   /* condition */
                      NULL,                   /* range */
                      NULL,                   /* metadata */
                      sysmd,                  /* sysmd */
                      (char*)request->buffer, /* data_buf */
                      request->wbuflen);      /* data_len */

    switch (status) {
      case DPL_SUCCESS:
        success = true;
        goto bail_out;
      default:
        Mmsg2(errmsg, _("Failed to flush %s using dpl_fput(): ERR=%s.\n"),
              chunk_name.c_str(), dpl_status_str(status));
        dev_errno = DropletErrnoToSystemErrno(status);
        Bmicrosleep(INFLIGT_RETRY_TIME, 0);
        tries++;
        goto again1;
    }

  again1:
    Dmsg1(100, "Flushing start over again (%d)\n", status);

  } while (!success && tries < NUMBER_OF_RETRIES);

  if (tries == NUMBER_OF_RETRIES) { Dmsg0(100, "dpl_fput timed out\n"); }


bail_out:
  retval = success;
  // Clear that we are uploading the chunk.
  ClearInflightChunk(request);

  if (sysmd) { dpl_sysmd_free(sysmd); }

  return retval;
}

// Internal method for reading a chunk from the remote backing store.
bool DropletDevice::ReadRemoteChunk(chunk_io_request* request)
{
  bool retval = false;
  dpl_status_t status;
  dpl_option_t dpl_options;
  dpl_range_t dpl_range;
  dpl_sysmd_t* sysmd = NULL;
  PoolMem chunk_name(PM_FNAME);

  Mmsg(chunk_name, "/%s/%04d", request->volname, request->chunk);
  Dmsg1(100, "Reading chunk %s\n", chunk_name.c_str());

  // See if chunk exists.
  int tries = 0;
  bool success = false;

  do {
    if (sysmd) { dpl_sysmd_free(sysmd); }
    sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_getattr(ctx_,               /* context */
                         chunk_name.c_str(), /* locator */
                         NULL,               /* metadata */
                         sysmd);             /* sysmd */

    switch (status) {
      case DPL_SUCCESS:
        if (sysmd->size > request->wbuflen) {
          Mmsg3(errmsg,
                _("Failed to read %s (%ld) to big to fit in chunksize of %ld "
                  "bytes\n"),
                chunk_name.c_str(), sysmd->size, request->wbuflen);
          Dmsg1(100, "%s", errmsg);
          dev_errno = EINVAL;
          goto bail_out;
        } else {
          success = true;
          dev_errno = 0;
        }
        break;
      case DPL_ENOENT:
      case DPL_EINVAL:
        Mmsg1(errmsg, _("Failed to open %s doesn't exist\n"),
              chunk_name.c_str());
        Dmsg1(100, "%s", errmsg);
        dev_errno = EIO;
        goto bail_out;
      default:
        Mmsg2(errmsg, _("Failed to open %s (Droplet error: %d)\n"),
              chunk_name.c_str(), status);
        Dmsg1(100, "%s", errmsg);
        dev_errno = EIO;
        Bmicrosleep(INFLIGT_RETRY_TIME, 0);
        tries++;
    }

  } while (!success && tries < NUMBER_OF_RETRIES);

  if (tries == NUMBER_OF_RETRIES) {
    Dmsg0(100, "dpl_getattr timed out");
    goto bail_out;
  }

  /*
   * Create some options for libdroplet.
   *
   * DPL_OPTION_NOALLOC - we provide the buffer to copy the data into
   *                      no need to let the library allocate memory we
   *                      need to free after copying the data.
   */
  tries = 0;
  success = false;

  do {
    memset(&dpl_options, 0, sizeof(dpl_options));
    dpl_options.mask |= DPL_OPTION_NOALLOC;

    dpl_range.start = 0;
    dpl_range.end = sysmd->size;
    *request->rbuflen = sysmd->size;
    dpl_sysmd_free(sysmd);
    sysmd = dpl_sysmd_dup(&sysmd_);
    status = dpl_fget(ctx_,                     /* context */
                      chunk_name.c_str(),       /* locator */
                      &dpl_options,             /* options */
                      NULL,                     /* condition */
                      &dpl_range,               /* range */
                      (char**)&request->buffer, /* data_bufp */
                      request->rbuflen,         /* data_lenp */
                      NULL,                     /* metadatap */
                      sysmd);                   /* sysmdp */

    switch (status) {
      case DPL_SUCCESS:
        success = true;
        dev_errno = 0;
        break;
      case DPL_ENOENT:
        Mmsg1(errmsg, _("Failed to open %s doesn't exist\n"),
              chunk_name.c_str());
        Dmsg1(100, "%s", errmsg);
        dev_errno = EIO;
        Bmicrosleep(INFLIGT_RETRY_TIME, 0);
        ++tries;
        break;
      default:
        Mmsg2(errmsg, _("Failed to read %s using dpl_fget(): ERR=%s.\n"),
              chunk_name.c_str(), dpl_status_str(status));
        Dmsg1(100, "%s", errmsg);
        dev_errno = DropletErrnoToSystemErrno(status);
        Bmicrosleep(INFLIGT_RETRY_TIME, 0);
        ++tries;
    }
  } while (!success && tries < NUMBER_OF_RETRIES);

  if (tries == NUMBER_OF_RETRIES) { Dmsg0(100, "dpl_getattr timed out\n"); }

  retval = success;

bail_out:
  if (sysmd) { dpl_sysmd_free(sysmd); }

  return retval;
}

/*
 * Internal method for truncating a chunked volume on the remote backing
 * store.
 */
bool DropletDevice::TruncateRemoteVolume(DeviceControlRecord*)
{
  PoolMem chunk_dir(PM_FNAME);

  Dmsg1(100, "truncate_remote_chunked_volume(%s) start.\n", getVolCatName());
  Mmsg(chunk_dir, "/%s", getVolCatName());
  bool ignore_gaps = true;
  if (!ForEachChunkInDirectoryRunCallback(chunk_dir.c_str(),
                                          chunked_volume_truncate_callback,
                                          NULL, ignore_gaps)) {
    /* errno already set in ForEachChunkInDirectoryRunCallback. */
    return false;
  }
  Dmsg1(100, "truncate_remote_chunked_volume(%s) finished.\n", getVolCatName());

  return true;
}


bool DropletDevice::d_flush(DeviceControlRecord*)
{
  return WaitUntilChunksWritten();
};

// Initialize backend.
bool DropletDevice::initialize()
{
  dpl_status_t status;

  // Initialize the droplet library when its not done previously.
  lock_mutex(mutex);
  if (droplet_reference_count == 0) {
    dpl_set_log_func(DropletDeviceLogfunc);

    status = dpl_init();
    switch (status) {
      case DPL_SUCCESS:
        break;
      default:
        unlock_mutex(mutex);
        goto bail_out;
    }
  }
  droplet_reference_count++;
  unlock_mutex(mutex);

  if (!configstring_) {
    int len;
    bool done;
    uint64_t value;
    char *bp, *next_option;

    if (!dev_options) {
      Mmsg0(errmsg, _("No device options configured\n"));
      Emsg0(M_FATAL, 0, errmsg);
      return -1;
    }

    configstring_ = strdup(dev_options);

    bp = configstring_;
    while (bp) {
      next_option = strchr(bp, ',');
      if (next_option) { *next_option++ = '\0'; }

      done = false;
      for (int i = 0; !done && device_options[i].name; i++) {
        // Try to find a matching device option.
        if (bstrncasecmp(bp, device_options[i].name,
                         device_options[i].compare_size)) {
          switch (device_options[i].type) {
            case argument_profile: {
              char* profile;

              // Strip any .profile prefix from the libdroplet profile name.
              profile = bp + device_options[i].compare_size;
              len = strlen(profile);
              if (len > 8 && Bstrcasecmp(profile + (len - 8), ".profile")) {
                profile[len - 8] = '\0';
              }
              profile_ = profile;
              done = true;
              break;
            }
            case argument_location:
              location_ = bp + device_options[i].compare_size;
              done = true;
              break;
            case argument_canned_acl:
              canned_acl_ = bp + device_options[i].compare_size;
              done = true;
              break;
            case argument_storage_class:
              storage_class_ = bp + device_options[i].compare_size;
              done = true;
              break;
            case argument_bucket:
              bucketname_ = bp + device_options[i].compare_size;
              done = true;
              break;
            case argument_chunksize:
              size_to_uint64(bp + device_options[i].compare_size, &value);
              chunk_size_ = value;
              done = true;
              break;
            case argument_iothreads:
              size_to_uint64(bp + device_options[i].compare_size, &value);
              io_threads_ = value & 0xFF;
              done = true;
              break;
            case argument_ioslots:
              size_to_uint64(bp + device_options[i].compare_size, &value);
              io_slots_ = value & 0xFF;
              done = true;
              break;
            case argument_retries:
              size_to_uint64(bp + device_options[i].compare_size, &value);
              retries_ = value & 0xFF;
              done = true;
              break;
            case argument_mmap:
              use_mmap_ = true;
              done = true;
              break;
            default:
              break;
          }
        }
      }

      if (!done) {
        Mmsg1(errmsg, _("Unable to parse device option: %s\n"), bp);
        Emsg0(M_FATAL, 0, errmsg);
        goto bail_out;
      }

      bp = next_option;
    }

    if (!profile_) {
      Mmsg0(errmsg, _("No droplet profile configured\n"));
      Emsg0(M_FATAL, 0, errmsg);
      goto bail_out;
    }
  }

  // See if we need to setup a new context for this device.
  if (!ctx_) {
    char* bp;
    PoolMem temp(PM_NAME);

    // Setup global sysmd settings which are cloned for each operation.
    memset(&sysmd_, 0, sizeof(sysmd_));
    if (location_) {
      PmStrcpy(temp, location_);
      sysmd_.mask |= DPL_SYSMD_MASK_LOCATION_CONSTRAINT;
      sysmd_.location_constraint = dpl_location_constraint(temp.c_str());
      if (sysmd_.location_constraint == -1) {
        Mmsg2(errmsg, _("Illegal location argument %s for device %s%s\n"),
              temp.c_str(), archive_device_string);
        goto bail_out;
      }
    }

    if (canned_acl_) {
      PmStrcpy(temp, canned_acl_);
      sysmd_.mask |= DPL_SYSMD_MASK_CANNED_ACL;
      sysmd_.canned_acl = dpl_canned_acl(temp.c_str());
      if (sysmd_.canned_acl == -1) {
        Mmsg2(errmsg, _("Illegal canned_acl argument %s for device %s%s\n"),
              temp.c_str(), archive_device_string);
        goto bail_out;
      }
    }

    if (storage_class_) {
      PmStrcpy(temp, storage_class_);
      sysmd_.mask |= DPL_SYSMD_MASK_STORAGE_CLASS;
      sysmd_.storage_class = dpl_storage_class(temp.c_str());
      if (sysmd_.storage_class == -1) {
        Mmsg2(errmsg, _("Illegal storage_class argument %s for device %s%s\n"),
              temp.c_str(), archive_device_string);
        goto bail_out;
      }
    }

    // See if this is a path.
    PmStrcpy(temp, profile_);
    bp = strrchr(temp.c_str(), '/');
    if (!bp) {
      // Only a profile name.
      ctx_ = dpl_ctx_new(NULL, temp.c_str());
    } else {
      if (bp == temp.c_str()) {
        // Profile in root of filesystem
        ctx_ = dpl_ctx_new("/", bp + 1);
      } else {
        // Profile somewhere else.
        *bp++ = '\0';
        ctx_ = dpl_ctx_new(temp.c_str(), bp);
      }
    }

    // If we failed to allocate a new context fail the open.
    if (!ctx_) {
      Mmsg1(errmsg, _("Failed to create a new context using config %s\n"),
            dev_options);
      Dmsg1(100, "%s", errmsg);
      goto bail_out;
    }

    // Login if that is needed for this backend.
    status = dpl_login(ctx_);

    switch (status) {
      case DPL_SUCCESS:
        break;
      case DPL_ENOTSUPP:
        // Backend doesn't support login which is fine.
        break;
      default:
        Mmsg2(errmsg,
              _("Failed to login for volume %s using dpl_login(): ERR=%s.\n"),
              getVolCatName(), dpl_status_str(status));
        Dmsg1(100, "%s", errmsg);
        goto bail_out;
    }

    // If a bucketname was defined set it in the context.
    if (bucketname_) {
      free(ctx_->cur_bucket);
      ctx_->cur_bucket = strdup(bucketname_);
    }
  }

  return true;

bail_out:
  return false;
}

// Open a volume using libdroplet.
int DropletDevice::d_open(const char* pathname, int flags, int mode)
{
  if (!initialize()) { return -1; }

  return SetupChunk(pathname, flags, mode);
}

// Read data from a volume using libdroplet.
ssize_t DropletDevice::d_read(int fd, void* buffer, size_t count)
{
  return ReadChunked(fd, buffer, count);
}

// Write data to a volume using libdroplet.
ssize_t DropletDevice::d_write(int fd, const void* buffer, size_t count)
{
  return WriteChunked(fd, buffer, count);
}

int DropletDevice::d_close(int) { return CloseChunk(); }

int DropletDevice::d_ioctl(int, ioctl_req_t, char*) { return -1; }

/**
 * Open a directory on the backing store and find out size information for a
 * volume.
 */
ssize_t DropletDevice::RemoteVolumeSize()
{
  ssize_t volumesize = 0;
  dpl_sysmd_t* sysmd = NULL;
  PoolMem chunk_dir(PM_FNAME);

  Mmsg(chunk_dir, "/%s", getVolCatName());

  /*
   * FIXME: With the current version of libdroplet a dpl_getattr() on a
   * directory fails with DPL_ENOENT even when the directory does exist. All
   * other operations succeed and as ForEachChunkInDirectoryRunCallback() does a
   * dpl_chdir() anyway that will fail if the directory doesn't exist for now we
   * should be mostly fine.
   */

  Dmsg1(100, "get RemoteVolumeSize(%s)\n", getVolCatName());
  if (!ForEachChunkInDirectoryRunCallback(
          chunk_dir.c_str(), chunked_volume_size_callback, &volumesize)) {
    /* errno is already set in ForEachChunkInDirectoryRunCallback */
    volumesize = -1;
    goto bail_out;
  }

bail_out:
  if (sysmd) { dpl_sysmd_free(sysmd); }

  Dmsg2(100, "Size of volume %s: %lld\n", chunk_dir.c_str(), volumesize);

  return volumesize;
}

boffset_t DropletDevice::d_lseek(DeviceControlRecord*,
                                 boffset_t offset,
                                 int whence)
{
  switch (whence) {
    case SEEK_SET:
      offset_ = offset;
      break;
    case SEEK_CUR:
      offset_ += offset;
      break;
    case SEEK_END: {
      ssize_t volumesize;

      volumesize = ChunkedVolumeSize();

      Dmsg1(100, "Current volumesize: %lld\n", volumesize);

      if (volumesize >= 0) {
        offset_ = volumesize + offset;
      } else {
        return -1;
      }
      break;
    }
    default:
      return -1;
  }

  if (!LoadChunk()) { return -1; }

  return offset_;
}

bool DropletDevice::d_truncate(DeviceControlRecord* dcr)
{
  return TruncateChunkedVolume(dcr);
}

DropletDevice::~DropletDevice()
{
  if (ctx_) {
    if (bucketname_ && ctx_->cur_bucket) {
      free(ctx_->cur_bucket);
      ctx_->cur_bucket = NULL;
    }
    dpl_ctx_free(ctx_);
    ctx_ = NULL;
  }

  if (configstring_) { free(configstring_); }

  lock_mutex(mutex);
  droplet_reference_count--;
  if (droplet_reference_count == 0) { dpl_free(); }
  unlock_mutex(mutex);
}

class Backend : public BackendInterface {
 public:
  Device* GetDevice() override { return new DropletDevice; }
};

#ifdef HAVE_DYNAMIC_SD_BACKENDS
extern "C" BackendInterface* GetBackend(void) { return new Backend; }
#endif
} /* namespace storagedaemon */