-
Notifications
You must be signed in to change notification settings - Fork 256
/
accurate_lmdb.cc
579 lines (497 loc) · 15.9 KB
/
accurate_lmdb.cc
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2013-2014 Planets Communications B.V.
Copyright (C) 2013-2018 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Marco van Wieringen, December 2013
*/
/**
* This file contains the LMDB abstraction of the accurate payload storage.
*/
#include "include/bareos.h"
#include "filed/filed.h"
#include "filed/filed_globals.h"
#ifdef HAVE_LMDB
#include "accurate.h"
#endif
namespace filedaemon {
#ifdef HAVE_LMDB
static int debuglevel = 100;
#define AVG_NR_BYTES_PER_ENTRY 256
#define B_PAGE_SIZE 4096
BareosAccurateFilelistLmdb::BareosAccurateFilelistLmdb(JobControlRecord *jcr, uint32_t number_of_files)
{
filenr_ = 0;
pay_load_ = GetPoolMemory(PM_MESSAGE);
lmdb_name_ = GetPoolMemory(PM_FNAME);
seen_bitmap_ = (char *)malloc(NbytesForBits(number_of_previous_files_));
ClearAllBits(number_of_previous_files_, seen_bitmap_);
db_env_ = NULL;
db_ro_txn_ = NULL;
db_rw_txn_ = NULL;
db_dbi_ = 0;
number_of_previous_files_ = number_of_files;
}
bool BareosAccurateFilelistLmdb::init()
{
int result;
MDB_env *env;
size_t mapsize = 10485760;
if (!db_env_) {
result = mdb_env_create(&env);
if (result) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to create MDB environment: %s\n"), mdb_strerror(result));
return false;
}
if ((number_of_previous_files_ * AVG_NR_BYTES_PER_ENTRY) > mapsize) {
size_t pagesize;
#ifdef HAVE_GETPAGESIZE
pagesize = getpagesize();
#else
pagesize = B_PAGE_SIZE;
#endif
mapsize = (((number_of_previous_files_ * AVG_NR_BYTES_PER_ENTRY) / pagesize) + 1) * pagesize;
}
result = mdb_env_set_mapsize(env, mapsize);
if (result) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to set MDB mapsize: %s\n"), mdb_strerror(result));
goto bail_out;
}
/*
* Explicitly set the number of readers to 1.
*/
result = mdb_env_set_maxreaders(env, 1);
if (result) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to set MDB maxreaders: %s\n"), mdb_strerror(result));
goto bail_out;
}
Mmsg(lmdb_name_, "%s/.accurate_lmdb.%d", me->working_directory, jcr_->JobId);
result = mdb_env_open(env, lmdb_name_, MDB_NOSUBDIR | MDB_NOLOCK | MDB_NOSYNC, 0600);
if (result) {
Jmsg2(jcr_, M_FATAL, 0, _("Unable create LDMD database %s: %s\n"), lmdb_name_, mdb_strerror(result));
goto bail_out;
}
result = mdb_txn_begin(env, NULL, 0, &db_rw_txn_);
if (result) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to start a write transaction: %s\n"), mdb_strerror(result));
goto bail_out;
}
result = mdb_dbi_open(db_rw_txn_, NULL, MDB_CREATE, &db_dbi_);
if (result) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to open LMDB internal database: %s\n"), mdb_strerror(result));
mdb_txn_abort(db_rw_txn_);
db_rw_txn_ = NULL;
goto bail_out;
}
db_env_ = env;
}
return true;
bail_out:
if (env) {
mdb_env_close(env);
}
return false;
}
bool BareosAccurateFilelistLmdb::AddFile( char *fname,
int fname_length,
char *lstat,
int lstat_length,
char *chksum,
int chksulength_,
int32_t delta_seq)
{
accurate_payload *payload;
int result;
int total_length;
MDB_val key, data;
bool retval = false;
total_length = sizeof(accurate_payload) + lstat_length + chksulength_ + 2;
/*
* Make sure pay_load_ is large enough.
*/
pay_load_ = CheckPoolMemorySize(pay_load_, total_length);
/*
* We store the total pay load as:
*
* accurate_payload structure\0lstat\0chksum\0
*/
payload = (accurate_payload *)pay_load_;
payload->lstat = (char *)payload + sizeof(accurate_payload);
memcpy(payload->lstat, lstat, lstat_length);
payload->lstat[lstat_length] = '\0';
payload->chksum = (char *)payload->lstat + lstat_length + 1;
if (chksulength_) {
memcpy(payload->chksum, chksum, chksulength_);
}
payload->chksum[chksulength_] = '\0';
payload->delta_seq = delta_seq;
payload->filenr = filenr_++;
key.mv_data = fname;
key.mv_size = strlen(fname) + 1;
data.mv_data = payload;
data.mv_size = total_length;
retry:
result = mdb_put(db_rw_txn_, db_dbi_, &key, &data, MDB_NOOVERWRITE);
switch (result) {
case 0:
if (chksum) {
Dmsg4(debuglevel, "add fname=<%s> lstat=%s delta_seq=%i chksum=%s\n", fname, lstat, delta_seq, chksum);
} else {
Dmsg2(debuglevel, "add fname=<%s> lstat=%s\n", fname, lstat);
}
retval = true;
break;
case MDB_TXN_FULL:
/*
* Seems we filled the transaction.
* Flush the current transaction start a new one and retry the put.
*/
result = mdb_txn_commit(db_rw_txn_);
if (result == 0) {
result = mdb_txn_begin(db_env_, NULL, 0, &db_rw_txn_);
if (result == 0) {
goto retry;
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable create new transaction: %s\n"), mdb_strerror(result));
}
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to commit full transaction: %s\n"), mdb_strerror(result));
}
break;
default:
Jmsg1(jcr_, M_FATAL, 0, _("Unable insert new data: %s\n"), mdb_strerror(result));
break;
}
return retval;
}
bool BareosAccurateFilelistLmdb::EndLoad()
{
int result;
/*
* Commit any pending write transactions.
*/
if (db_rw_txn_) {
result = mdb_txn_commit(db_rw_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable close write transaction: %s\n"), mdb_strerror(result));
return false;
}
result = mdb_txn_begin(db_env_, NULL, 0, &db_rw_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to create write transaction: %s\n"), mdb_strerror(result));
return false;
}
}
/*
* From now on we also will be doing read transactions so create a read transaction context.
*/
if (!db_ro_txn_) {
result = mdb_txn_begin(db_env_, NULL, MDB_RDONLY, &db_ro_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to create read transaction: %s\n"), mdb_strerror(result));
return false;
}
}
return true;
}
accurate_payload *BareosAccurateFilelistLmdb::lookup_payload(char *fname)
{
int result;
int lstat_length;
MDB_val key, data;
accurate_payload *payload = NULL;
key.mv_data = fname;
key.mv_size = strlen(fname) + 1;
result = mdb_get(db_ro_txn_, db_dbi_, &key, &data);
switch (result) {
case 0:
/*
* Success.
*
* We need to make a private copy of the LDMB data as we are not
* allowed to change its content and we need to update the lstat
* and chksum pointer to point to the actual lstat and chksum that
* is stored behind the accurate_payload structure in the LMDB.
*/
pay_load_ = CheckPoolMemorySize(pay_load_, data.mv_size);
payload = (accurate_payload *)pay_load_;
memcpy(payload, data.mv_data, data.mv_size);
payload->lstat = (char *)payload + sizeof(accurate_payload);
lstat_length = strlen(payload->lstat);
payload->chksum = (char *)payload->lstat + lstat_length + 1;
/*
* We keep the transaction as short a possible so after a lookup
* and copying the actual data out we reset the read transaction
* and do a renew of the read transaction for a new run.
*/
mdb_txn_reset(db_ro_txn_);
result = mdb_txn_renew(db_ro_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to renew read transaction: %s\n"), mdb_strerror(result));
return NULL;
}
break;
case MDB_NOTFOUND:
/*
* Failed to find the given key.
*/
break;
default:
break;
}
return payload;
}
bool BareosAccurateFilelistLmdb::UpdatePayload(char *fname, accurate_payload *payload)
{
int result,
total_length,
lstat_length,
chksulength_;
MDB_val key, data;
bool retval = false;
accurate_payload *new_payload;
lstat_length = strlen(payload->lstat);
chksulength_ = strlen(payload->chksum);
total_length = sizeof(accurate_payload) + lstat_length + chksulength_ + 2;
/*
* Make sure pay_load_ is large enough.
*/
pay_load_ = CheckPoolMemorySize(pay_load_, total_length);
/*
* We store the total pay load as:
*
* accurate_payload structure\0lstat\0chksum\0
*/
new_payload = (accurate_payload *)pay_load_;
new_payload->lstat = (char *)payload + sizeof(accurate_payload);
memcpy(new_payload->lstat, payload->lstat, lstat_length);
new_payload->lstat[lstat_length] = '\0';
new_payload->chksum = (char *)new_payload->lstat + lstat_length + 1;
if (chksulength_) {
memcpy(new_payload->chksum, payload->chksum, chksulength_);
}
new_payload->chksum[chksulength_] = '\0';
new_payload->delta_seq = payload->delta_seq;
new_payload->filenr = payload->filenr;
key.mv_data = fname;
key.mv_size = strlen(fname) + 1;
data.mv_data = new_payload;
data.mv_size = total_length;
retry:
result = mdb_put(db_rw_txn_, db_dbi_, &key, &data, 0);
switch (result) {
case 0:
result = mdb_txn_commit(db_rw_txn_);
if (result == 0) {
result = mdb_txn_begin(db_env_, NULL, 0, &db_rw_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to create write transaction: %s\n"), mdb_strerror(result));
} else {
retval = true;
}
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable close write transaction: %s\n"), mdb_strerror(result));
}
break;
case MDB_TXN_FULL:
/*
* Seems we filled the transaction.
* Flush the current transaction start a new one and retry the put.
*/
result = mdb_txn_commit(db_rw_txn_);
if (result == 0) {
result = mdb_txn_begin(db_env_, NULL, 0, &db_rw_txn_);
if (result == 0) {
goto retry;
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable create new transaction: %s\n"), mdb_strerror(result));
}
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to commit full transaction: %s\n"), mdb_strerror(result));
}
break;
default:
Jmsg1(jcr_, M_FATAL, 0, _("Unable insert new data: %s\n"), mdb_strerror(result));
break;
}
return retval;
}
bool BareosAccurateFilelistLmdb::SendBaseFileList()
{
int result;
int32_t LinkFIc;
FindFilesPacket *ff_pkt;
MDB_cursor *cursor;
MDB_val key, data;
bool retval = false;
accurate_payload *payload;
int stream = STREAM_UNIX_ATTRIBUTES;
if (!jcr_->accurate || jcr_->getJobLevel() != L_FULL) {
return true;
}
/*
* Commit any pending write transactions.
*/
if (db_rw_txn_) {
result = mdb_txn_commit(db_rw_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable close write transaction: %s\n"), mdb_strerror(result));
return false;
}
db_rw_txn_ = NULL;
}
ff_pkt = init_find_files();
ff_pkt->type = FT_BASE;
result = mdb_cursor_open(db_ro_txn_, db_dbi_, &cursor);
if (result == 0) {
while ((result = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
payload = (accurate_payload *)data.mv_data;
if (BitIsSet(payload->filenr, seen_bitmap_)) {
Dmsg1(debuglevel, "base file fname=%s\n", key.mv_data);
DecodeStat(payload->lstat, &ff_pkt->statp, sizeof(struct stat), &LinkFIc); /* decode catalog stat */
ff_pkt->fname = (char *)key.mv_data;
EncodeAndSendAttributes(jcr_, ff_pkt, stream);
}
}
mdb_cursor_close(cursor);
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable create cursor: %s\n"), mdb_strerror(result));
}
mdb_txn_reset(db_ro_txn_);
result = mdb_txn_renew(db_ro_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to renew read transaction: %s\n"), mdb_strerror(result));
goto bail_out;
}
retval = true;
bail_out:
TermFindFiles(ff_pkt);
return retval;
}
bool BareosAccurateFilelistLmdb::SendDeletedList()
{
int result;
int32_t LinkFIc;
struct stat statp;
FindFilesPacket *ff_pkt;
MDB_cursor *cursor;
MDB_val key, data;
bool retval = false;
accurate_payload *payload;
int stream = STREAM_UNIX_ATTRIBUTES;
if (!jcr_->accurate) {
return true;
}
/*
* Commit any pending write transactions.
*/
if (db_rw_txn_) {
result = mdb_txn_commit(db_rw_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable close write transaction: %s\n"), mdb_strerror(result));
return false;
}
db_rw_txn_ = NULL;
}
ff_pkt = init_find_files();
ff_pkt->type = FT_DELETED;
result = mdb_cursor_open(db_ro_txn_, db_dbi_, &cursor);
if (result == 0) {
while ((result = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
payload = (accurate_payload *)data.mv_data;
if (BitIsSet(payload->filenr, seen_bitmap_) ||
PluginCheckFile(jcr_, (char *)key.mv_data)) {
continue;
}
Dmsg1(debuglevel, "deleted fname=%s\n", key.mv_data);
DecodeStat(payload->lstat, &statp, sizeof(struct stat), &LinkFIc); /* decode catalog stat */
ff_pkt->fname = (char *)key.mv_data;
ff_pkt->statp.st_mtime = statp.st_mtime;
ff_pkt->statp.st_ctime = statp.st_ctime;
EncodeAndSendAttributes(jcr_, ff_pkt, stream);
}
mdb_cursor_close(cursor);
} else {
Jmsg1(jcr_, M_FATAL, 0, _("Unable create cursor: %s\n"), mdb_strerror(result));
}
mdb_txn_reset(db_ro_txn_);
result = mdb_txn_renew(db_ro_txn_);
if (result != 0) {
Jmsg1(jcr_, M_FATAL, 0, _("Unable to renew read transaction: %s\n"), mdb_strerror(result));
goto bail_out;
}
retval = true;
bail_out:
TermFindFiles(ff_pkt);
return retval;
}
void BareosAccurateFilelistLmdb::destroy()
{
/*
* Abort any pending read transaction.
*/
if (db_ro_txn_) {
mdb_txn_abort(db_ro_txn_);
db_ro_txn_ = NULL;
}
/*
* Abort any pending write transaction.
*/
if (db_rw_txn_) {
mdb_txn_abort(db_rw_txn_);
db_rw_txn_ = NULL;
}
if (db_env_) {
/*
* Drop the contents of the LMDB.
*/
if (db_dbi_) {
int result;
MDB_txn *txn;
result = mdb_txn_begin(db_env_, NULL, 0, &txn);
if (result == 0) {
result = mdb_drop(txn, db_dbi_, 1);
if (result == 0) {
mdb_txn_commit(txn);
} else {
mdb_txn_abort(txn);
}
}
db_dbi_ = 0;
}
/*
* Close the environment.
*/
mdb_env_close(db_env_);
db_env_ = NULL;
}
if (pay_load_) {
FreePoolMemory(pay_load_);
pay_load_ = NULL;
}
if (lmdb_name_) {
SecureErase(jcr_, lmdb_name_);
FreePoolMemory(lmdb_name_);
lmdb_name_ = NULL;
}
if (seen_bitmap_) {
free(seen_bitmap_);
seen_bitmap_ = NULL;
}
filenr_ = 0;
}
#endif /* HAVE_LMDB */
} /* namespace filedaemon */