/
rados-fd.c
1201 lines (1034 loc) · 30.1 KB
/
rados-fd.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) 2014-2016 Planets Communications B.V.
Copyright (C) 2014-2016 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, which is
listed 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.
*/
/*
* CEPH rados plugin for the Bareos File Daemon
*/
#include "bareos.h"
#include "fd_plugins.h"
#include "fd_common.h"
#include <rados/librados.h>
static const int dbglvl = 150;
#define PLUGIN_LICENSE "Bareos AGPLv3"
#define PLUGIN_AUTHOR "Marco van Wieringen"
#define PLUGIN_DATE "February 2016"
#define PLUGIN_VERSION "2"
#define PLUGIN_DESCRIPTION "Bareos CEPH rados File Daemon Plugin"
#define PLUGIN_USAGE "rados:conffile=<ceph_conf_file>:namespace=<name_space>:clientid=<client_id>:clustername=<clustername>:username=<username>:snapshotname=<snapshot_name>:"
/*
* Use for versions lower then 0.68.0 of the API the old format and otherwise the new one.
*/
#if LIBRADOS_VERSION_CODE < 17408
#define DEFAULT_CLIENTID "admin"
#else
#define DEFAULT_CLUSTERNAME "ceph"
#define DEFAULT_USERNAME "client.admin"
#endif
/*
* Forward referenced functions
*/
static bRC newPlugin(bpContext *ctx);
static bRC freePlugin(bpContext *ctx);
static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
static bRC endBackupFile(bpContext *ctx);
static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
static bRC startRestoreFile(bpContext *ctx, const char *cmd);
static bRC endRestoreFile(bpContext *ctx);
static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
static bRC checkFile(bpContext *ctx, char *fname);
static bRC getAcl(bpContext *ctx, acl_pkt *ap);
static bRC setAcl(bpContext *ctx, acl_pkt *ap);
static bRC getXattr(bpContext *ctx, xattr_pkt *xp);
static bRC setXattr(bpContext *ctx, xattr_pkt *xp);
static bRC parse_plugin_definition(bpContext *ctx, void *value);
static bRC setup_backup(bpContext *ctx, void *value);
static bRC setup_restore(bpContext *ctx, void *value);
static bRC end_restore_job(bpContext *ctx, void *value);
/*
* Pointers to Bareos functions
*/
static bFuncs *bfuncs = NULL;
static bInfo *binfo = NULL;
/*
* Plugin Information block
*/
static genpInfo pluginInfo = {
sizeof(pluginInfo),
FD_PLUGIN_INTERFACE_VERSION,
FD_PLUGIN_MAGIC,
PLUGIN_LICENSE,
PLUGIN_AUTHOR,
PLUGIN_DATE,
PLUGIN_VERSION,
PLUGIN_DESCRIPTION,
PLUGIN_USAGE
};
/*
* Plugin entry points for Bareos
*/
static pFuncs pluginFuncs = {
sizeof(pluginFuncs),
FD_PLUGIN_INTERFACE_VERSION,
/* Entry points into plugin */
newPlugin, /* new plugin instance */
freePlugin, /* free plugin instance */
getPluginValue,
setPluginValue,
handlePluginEvent,
startBackupFile,
endBackupFile,
startRestoreFile,
endRestoreFile,
pluginIO,
createFile,
setFileAttributes,
checkFile,
getAcl,
setAcl,
getXattr,
setXattr
};
/*
* Plugin private context
*/
struct plugin_ctx {
int32_t backup_level;
utime_t since;
char *plugin_options;
uint32_t JobId;
char *rados_conffile;
char *rados_clientid;
char *rados_clustername;
char *rados_username;
char *rados_poolname;
char *rados_namespace;
char *rados_snapshotname;
bool cluster_initialized;
const char *object_name;
uint64_t object_size;
time_t object_mtime;
POOLMEM *next_filename;
rados_t cluster;
rados_ioctx_t ioctx;
rados_snap_t snap_id;
rados_list_ctx_t list_iterator;
rados_xattrs_iter_t xattr_iterator;
boffset_t offset;
};
/*
* This defines the arguments that the plugin parser understands.
*/
enum plugin_argument_type {
argument_none,
argument_conffile,
argument_poolname,
argument_clientid,
argument_clustername,
argument_username,
argument_namespace,
argument_snapshotname
};
struct plugin_argument {
const char *name;
enum plugin_argument_type type;
};
static plugin_argument plugin_arguments[] = {
{ "conffile", argument_conffile },
{ "poolname", argument_poolname },
{ "clientid", argument_clientid },
{ "clustername", argument_clustername },
{ "username", argument_username },
{ "namespace", argument_namespace },
{ "snapshotname", argument_snapshotname },
{ NULL, argument_none }
};
#ifdef __cplusplus
extern "C" {
#endif
/*
* loadPlugin() and unloadPlugin() are entry points that are exported, so Bareos can
* directly call these two entry points they are common to all Bareos plugins.
*
* External entry point called by Bareos to "load" the plugin
*/
bRC DLL_IMP_EXP loadPlugin(bInfo *lbinfo,
bFuncs *lbfuncs,
genpInfo **pinfo,
pFuncs **pfuncs)
{
bfuncs = lbfuncs; /* set Bareos funct pointers */
binfo = lbinfo;
*pinfo = &pluginInfo; /* return pointer to our info */
*pfuncs = &pluginFuncs; /* return pointer to our functions */
return bRC_OK;
}
/*
* External entry point to unload the plugin
*/
bRC DLL_IMP_EXP unloadPlugin()
{
return bRC_OK;
}
#ifdef __cplusplus
}
#endif
/*
* The following entry points are accessed through the function pointers we supplied to Bareos.
* Each plugin type (dir, fd, sd) has its own set of entry points that the plugin must define.
*
* Create a new instance of the plugin i.e. allocate our private storage
*/
static bRC newPlugin(bpContext *ctx)
{
plugin_ctx *p_ctx;
p_ctx = (plugin_ctx *)malloc(sizeof(plugin_ctx));
if (!p_ctx) {
return bRC_Error;
}
memset(p_ctx, 0, sizeof(plugin_ctx));
ctx->pContext = (void *)p_ctx; /* set our context pointer */
p_ctx->next_filename = get_pool_memory(PM_FNAME);
bfuncs->getBareosValue(ctx, bVarJobId, (void *)&p_ctx->JobId);
/*
* Only register the events we are really interested in.
*/
bfuncs->registerBareosEvents(ctx,
7,
bEventLevel,
bEventSince,
bEventRestoreCommand,
bEventBackupCommand,
bEventPluginCommand,
bEventEndRestoreJob,
bEventNewPluginOptions);
return bRC_OK;
}
/*
* Free a plugin instance, i.e. release our private storage
*/
static bRC freePlugin(bpContext *ctx)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
Dmsg(ctx, dbglvl, "rados-fd: entering freePlugin\n");
if (p_ctx->snap_id) {
rados_ioctx_snap_remove(p_ctx->ioctx, p_ctx->rados_snapshotname);
p_ctx->snap_id = 0;
}
if (p_ctx->cluster_initialized) {
rados_shutdown(p_ctx->cluster);
p_ctx->cluster_initialized = false;
}
free_pool_memory(p_ctx->next_filename);
if (p_ctx->rados_snapshotname) {
free(p_ctx->rados_snapshotname);
}
if (p_ctx->rados_namespace) {
free(p_ctx->rados_namespace);
}
if (p_ctx->rados_poolname) {
free(p_ctx->rados_poolname);
}
if (p_ctx->rados_clientid) {
free(p_ctx->rados_clientid);
}
if (p_ctx->rados_clustername) {
free(p_ctx->rados_clustername);
}
if (p_ctx->rados_username) {
free(p_ctx->rados_username);
}
if (p_ctx->rados_conffile) {
free(p_ctx->rados_conffile);
}
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
}
free(p_ctx);
p_ctx = NULL;
Dmsg(ctx, dbglvl, "rados-fd: leaving freePlugin\n");
return bRC_OK;
}
/*
* Return some plugin value (none defined)
*/
static bRC getPluginValue(bpContext *ctx, pVariable var, void *value)
{
return bRC_OK;
}
/*
* Set a plugin value (none defined)
*/
static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
{
return bRC_OK;
}
/*
* Handle an event that was generated in Bareos
*/
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
{
bRC retval;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
switch (event->eventType) {
case bEventLevel:
p_ctx->backup_level = (int64_t)value;
retval = bRC_OK;
break;
case bEventSince:
p_ctx->since = (int64_t)value;
retval = bRC_OK;
break;
case bEventRestoreCommand:
retval = parse_plugin_definition(ctx, value);
if (retval == bRC_OK) {
retval = setup_restore(ctx, value);
}
break;
case bEventBackupCommand:
retval = parse_plugin_definition(ctx, value);
if (retval == bRC_OK) {
retval = setup_backup(ctx, value);
}
break;
case bEventPluginCommand:
retval = parse_plugin_definition(ctx, value);
break;
case bEventNewPluginOptions:
/*
* Free any previous value.
*/
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
p_ctx->plugin_options = NULL;
}
retval = parse_plugin_definition(ctx, value);
/*
* Save that we got a plugin override.
*/
p_ctx->plugin_options = bstrdup((char *)value);
break;
case bEventEndRestoreJob:
retval = end_restore_job(ctx, value);
break;
default:
Jmsg(ctx, M_FATAL, "rados-fd: unknown event=%d\n", event->eventType);
Dmsg(ctx, dbglvl, "rados-fd: unknown event=%d\n", event->eventType);
retval = bRC_Error;
break;
}
return retval;
}
/*
* Get the next object to backup.
* - Get the next objectname from the list iterator.
* - Check using AcceptFile if it matches the fileset.
*/
static bRC get_next_object_to_backup(bpContext *ctx)
{
int status;
struct save_pkt sp;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
while (1) {
#if defined(HAVE_RADOS_NAMESPACES) && defined(HAVE_RADOS_NOBJECTS_LIST)
status = rados_nobjects_list_next(p_ctx->list_iterator, &p_ctx->object_name, NULL, NULL);
#else
status = rados_objects_list_next(p_ctx->list_iterator, &p_ctx->object_name, NULL);
#endif
if (status < 0) {
berrno be;
switch (status) {
case -ENOENT:
#if defined(HAVE_RADOS_NAMESPACES) && defined(HAVE_RADOS_NOBJECTS_LIST)
rados_nobjects_list_close(p_ctx->list_iterator);
#else
rados_objects_list_close(p_ctx->list_iterator);
#endif
p_ctx->list_iterator = NULL;
return bRC_OK;
default:
#if defined(HAVE_RADOS_NAMESPACES) && defined(HAVE_RADOS_NOBJECTS_LIST)
Jmsg(ctx, M_ERROR, "rados_nobjects_list_next() failed: %s\n", be.bstrerror(-status));
#else
Jmsg(ctx, M_ERROR, "rados_objects_list_next() failed: %s\n", be.bstrerror(-status));
#endif
return bRC_Error;
}
} else {
Mmsg(p_ctx->next_filename, "%s/%s", p_ctx->rados_poolname, p_ctx->object_name);
}
/*
* See if we accept this file under the currently loaded fileset.
*/
memset(&sp, 0, sizeof(sp));
sp.pkt_size = sizeof(sp);
sp.pkt_end = sizeof(sp);
sp.fname = p_ctx->next_filename;
sp.statp.st_mode = 0700 | S_IFREG;
if (bfuncs->AcceptFile(ctx, &sp) == bRC_Skip) {
continue;
}
status = rados_stat(p_ctx->ioctx, p_ctx->object_name,
&p_ctx->object_size, &p_ctx->object_mtime);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_stat(%s) failed: %s\n", p_ctx->object_name, be.bstrerror(-status));
return bRC_Error;
}
/*
* If we made it here we have the next object to backup.
*/
break;
}
return bRC_More;
}
/*
* Start the backup of a specific file
*/
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
sp->fname = p_ctx->next_filename;
sp->statp.st_mode = 0700 | S_IFREG;
sp->statp.st_mtime = p_ctx->object_mtime;
sp->statp.st_ctime = sp->statp.st_mtime;
sp->statp.st_atime = sp->statp.st_mtime;
sp->statp.st_blksize = 4096;
sp->statp.st_size = p_ctx->object_size;
sp->statp.st_blocks = (uint32_t)(sp->statp.st_size + 4095) / 4096;
sp->save_time = p_ctx->since;
/*
* For Incremental and Differential backups use checkChanges method to
* see if we need to backup this file.
*/
switch (p_ctx->backup_level) {
case L_INCREMENTAL:
case L_DIFFERENTIAL:
switch (bfuncs->checkChanges(ctx, sp)) {
case bRC_Seen:
sp->type = FT_NOCHG;
break;
default:
sp->type = FT_REG;
break;
}
break;
default:
sp->type = FT_REG;
break;
}
return bRC_OK;
}
/*
* Done with backup of this file
*/
static bRC endBackupFile(bpContext *ctx)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx || !p_ctx->list_iterator) {
return bRC_Error;
}
return get_next_object_to_backup(ctx);
}
/*
* Strip any backslashes in the string.
*/
static inline void strip_back_slashes(char *value)
{
char *bp;
bp = value;
while (*bp) {
switch (*bp) {
case '\\':
bstrinlinecpy(bp, bp + 1);
break;
default:
break;
}
bp++;
}
}
/*
* Parse a boolean value e.g. check if its yes or true anything else translates to false.
*/
static inline bool parse_boolean(const char *argument_value)
{
if (bstrcasecmp(argument_value, "yes") ||
bstrcasecmp(argument_value, "true")) {
return true;
} else {
return false;
}
}
/*
* Only set destination to value when it has no previous setting.
*/
static inline void set_string_if_null(char **destination, char *value)
{
if (!*destination) {
*destination = bstrdup(value);
strip_back_slashes(*destination);
}
}
/*
* Always set destination to value and clean any previous one.
*/
static inline void set_string(char **destination, char *value)
{
if (*destination) {
free(*destination);
}
*destination = bstrdup(value);
strip_back_slashes(*destination);
}
/*
* Parse the plugin definition passed in.
*
* The definition is in this form:
*
* rados:
*/
static bRC parse_plugin_definition(bpContext *ctx, void *value)
{
int i;
bool keep_existing;
char *plugin_definition, *bp, *argument, *argument_value;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx || !value) {
return bRC_Error;
}
keep_existing = (p_ctx->plugin_options) ? true : false;
/*
* Parse the plugin definition.
* Make a private copy of the whole string.
*/
plugin_definition = bstrdup((char *)value);
bp = strchr(plugin_definition, ':');
if (!bp) {
Jmsg(ctx, M_FATAL, "Illegal plugin definition %s\n", plugin_definition);
Dmsg(ctx, dbglvl, "Illegal plugin definition %s\n", plugin_definition);
goto bail_out;
}
/*
* Skip the first ':'
*/
bp++;
while (bp) {
if (strlen(bp) == 0) {
break;
}
/*
* Each argument is in the form:
* <argument> = <argument_value>
*
* So we setup the right pointers here, argument to the beginning
* of the argument, argument_value to the beginning of the argument_value.
*/
argument = bp;
argument_value = strchr(bp, '=');
if (!argument_value) {
Jmsg(ctx, M_FATAL, "Illegal argument %s without value\n", argument);
Dmsg(ctx, dbglvl, "Illegal argument %s without value\n", argument);
goto bail_out;
}
*argument_value++ = '\0';
/*
* See if there are more arguments and setup for the next run.
*/
bp = argument_value;
do {
bp = strchr(bp, ':');
if (bp) {
if (*(bp - 1) != '\\') {
*bp++ = '\0';
break;
} else {
bp++;
}
}
} while (bp);
for (i = 0; plugin_arguments[i].name; i++) {
if (bstrcasecmp(argument, plugin_arguments[i].name)) {
char **str_destination = NULL;
switch (plugin_arguments[i].type) {
case argument_conffile:
str_destination = &p_ctx->rados_conffile;
break;
case argument_clientid:
str_destination = &p_ctx->rados_clientid;
break;
case argument_clustername:
str_destination = &p_ctx->rados_clustername;
break;
case argument_username:
str_destination = &p_ctx->rados_username;
break;
case argument_poolname:
str_destination = &p_ctx->rados_poolname;
break;
case argument_namespace:
str_destination = &p_ctx->rados_namespace;
break;
case argument_snapshotname:
str_destination = &p_ctx->rados_snapshotname;
break;
default:
break;
}
/*
* Keep the first value, ignore any next setting.
*/
if (str_destination) {
if (keep_existing) {
set_string_if_null(str_destination, argument_value);
} else {
set_string(str_destination, argument_value);
}
}
/*
* When we have a match break the loop.
*/
break;
}
}
/*
* Got an invalid keyword ?
*/
if (!plugin_arguments[i].name) {
Jmsg(ctx, M_FATAL, "Illegal argument %s with value %s in plugin definition\n", argument, argument_value);
Dmsg(ctx, dbglvl, "Illegal argument %s with value %s in plugin definition\n", argument, argument_value);
goto bail_out;
}
}
free(plugin_definition);
return bRC_OK;
bail_out:
free(plugin_definition);
return bRC_Error;
}
/*
* Connect via RADOS protocol to a CEPH cluster.
*/
static bRC connect_to_rados(bpContext *ctx)
{
int status;
#if LIBRADOS_VERSION_CODE >= 17408
uint64_t rados_flags = 0;
#endif
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
/*
* See if we need to initialize the cluster connection.
*/
if (!p_ctx->cluster_initialized) {
#if LIBRADOS_VERSION_CODE < 17408
if (!p_ctx->rados_clientid) {
p_ctx->rados_clientid = bstrdup(DEFAULT_CLIENTID);
}
status = rados_create(&p_ctx->cluster, p_ctx->rados_clientid);
#else
if (!p_ctx->rados_clustername) {
p_ctx->rados_clustername = bstrdup(DEFAULT_CLUSTERNAME);
}
if (!p_ctx->rados_username) {
/*
* See if this uses the old clientid.
*/
if (p_ctx->rados_clientid) {
POOL_MEM temp;
Mmsg(temp, "client.%s", p_ctx->rados_clientid);
p_ctx->rados_username = bstrdup(temp.c_str());
} else {
p_ctx->rados_username = bstrdup(DEFAULT_USERNAME);
}
}
status = rados_create2(&p_ctx->cluster, p_ctx->rados_clustername, p_ctx->rados_username, rados_flags);
#endif
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_create() failed: %s\n", be.bstrerror(-status));
return bRC_Error;
}
status = rados_conf_read_file(p_ctx->cluster, p_ctx->rados_conffile);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_conf_read_file(%s) failed: %s\n",
p_ctx->rados_conffile, be.bstrerror(-status));
return bRC_Error;
}
status = rados_connect(p_ctx->cluster);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_connect() failed: %s\n", be.bstrerror(-status));
rados_shutdown(p_ctx->cluster);
return bRC_Error;
}
p_ctx->cluster_initialized = true;
}
/*
* See if we need to initialize the IO context.
*/
if (!p_ctx->ioctx) {
status = rados_ioctx_create(p_ctx->cluster, p_ctx->rados_poolname, &p_ctx->ioctx);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_ioctx_create(%s) failed: %s\n",
p_ctx->rados_poolname, be.bstrerror(-status));
rados_shutdown(p_ctx->cluster);
p_ctx->cluster_initialized = false;
return bRC_Error;
}
}
return bRC_OK;
}
/*
* Generic setup for performing a backup.
*/
static bRC setup_backup(bpContext *ctx, void *value)
{
int status;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx || !value) {
return bRC_Error;
}
if (connect_to_rados(ctx) != bRC_OK) {
return bRC_Error;
}
/*
* Create a snapshot and use it for consistent reading.
*/
if (!p_ctx->rados_snapshotname) {
POOL_MEM snapshotname(PM_NAME);
Mmsg(snapshotname, "bareos_backup_%ld", p_ctx->JobId);
p_ctx->rados_snapshotname = bstrdup(snapshotname.c_str());
}
status = rados_ioctx_snap_create(p_ctx->ioctx, p_ctx->rados_snapshotname);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_ioctx_snap_create(%s) failed: %s\n",
p_ctx->rados_snapshotname, be.bstrerror(-status));
goto bail_out;
}
status = rados_ioctx_snap_lookup(p_ctx->ioctx, p_ctx->rados_snapshotname, &p_ctx->snap_id);
if (status < 0) {
berrno be;
Jmsg(ctx, M_ERROR, "rados_ioctx_snap_lookup(%s) failed: %s\n",
p_ctx->rados_snapshotname, be.bstrerror(-status));
goto bail_out;
}
rados_ioctx_snap_set_read(p_ctx->ioctx, p_ctx->snap_id);
#if defined(HAVE_RADOS_NAMESPACES) && defined(LIBRADOS_ALL_NSPACES)
if (!p_ctx->rados_namespace || bstrcasecmp(p_ctx->rados_namespace, "all")) {
rados_ioctx_set_namespace(p_ctx->ioctx, LIBRADOS_ALL_NSPACES);
} else {
rados_ioctx_set_namespace(p_ctx->ioctx, p_ctx->rados_namespace);
}
#endif
/*
* See if we need to initialize an list iterator.
*/
if (!p_ctx->list_iterator) {
#if defined(HAVE_RADOS_NAMESPACES) && defined(HAVE_RADOS_NOBJECTS_LIST)
status = rados_nobjects_list_open(p_ctx->ioctx, &p_ctx->list_iterator);
#else
status = rados_objects_list_open(p_ctx->ioctx, &p_ctx->list_iterator);
#endif
if (status < 0) {
berrno be;
#if defined(HAVE_RADOS_NAMESPACES) && defined(HAVE_RADOS_NOBJECTS_LIST)
Jmsg(ctx, M_ERROR, "rados_nobjects_list_open() failed: %s\n", be.bstrerror(-status));
#else
Jmsg(ctx, M_ERROR, "rados_objects_list_open() failed: %s\n", be.bstrerror(-status));
#endif
goto bail_out;
}
return get_next_object_to_backup(ctx);
}
return bRC_OK;
bail_out:
if (p_ctx->snap_id) {
rados_ioctx_snap_remove(p_ctx->ioctx, p_ctx->rados_snapshotname);
p_ctx->snap_id = 0;
}
if (p_ctx->cluster_initialized) {
rados_shutdown(p_ctx->cluster);
p_ctx->cluster_initialized = false;
}
return bRC_Error;
}
/*
* Generic setup for performing a restore.
*/
static bRC setup_restore(bpContext *ctx, void *value)
{
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx || !value) {
return bRC_Error;
}
if (connect_to_rados(ctx) != bRC_OK) {
return bRC_Error;
}
return bRC_OK;
}
/*
* Bareos is calling us to do the actual I/O
*/
static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
{
int io_count;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}
switch(io->func) {
case IO_OPEN:
p_ctx->offset = 0;
io->status = 0;
io->io_errno = 0;
break;
case IO_READ:
io_count = rados_read(p_ctx->ioctx, p_ctx->object_name, io->buf, io->count, p_ctx->offset);
if (io_count >= 0) {
p_ctx->offset += io_count;
io->status = io_count;
io->io_errno = 0;
} else {
io->io_errno = -io_count;
goto bail_out;
}
break;
case IO_WRITE:
io_count = rados_write(p_ctx->ioctx, p_ctx->object_name, io->buf, io->count, p_ctx->offset);
#if LIBRADOS_VERSION_CODE <= 17408
if (io_count >= 0) {
p_ctx->offset += io_count;
io->status = io_count;
#else
if (io_count == 0) {
p_ctx->offset += io->count;
io->status = io->count;
#endif
io->io_errno = 0;
} else {
io->io_errno = -io_count;
goto bail_out;
}
break;
case IO_CLOSE:
p_ctx->offset = 0;
io->status = 0;
io->io_errno = 0;
break;
case IO_SEEK:
Jmsg(ctx, M_ERROR, "Illegal Seek request on rados device.");
Dmsg(ctx, dbglvl, "Illegal Seek request on rados device.");
io->io_errno = EINVAL;
goto bail_out;
}
return bRC_OK;
bail_out:
io->lerror = 0;
io->win32 = false;
io->status = -1;
return bRC_Error;
}
/*
* See if we need to do any postprocessing after the restore.
*/
static bRC end_restore_job(bpContext *ctx, void *value)
{
bRC retval = bRC_OK;