forked from brong/cyrus-imapd-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcyrusdb_berkeley.c
1134 lines (966 loc) · 26.9 KB
/
cyrusdb_berkeley.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
/* cyrusdb_berkeley: berkeley db backends
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* $Id: cyrusdb_berkeley.c,v 1.24 2010/01/06 17:01:45 murch Exp $
*/
#include <config.h>
#include <db.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "assert.h"
#include "bsearch.h"
#include "cyrusdb.h"
#include "exitcodes.h"
#include "libcyr_cfg.h"
#include "xmalloc.h"
#include "xstrlcpy.h"
#include "xstrlcat.h"
#define DATA(d) ((d)->data ? (d)->data : "")
#define DATALEN(d) ((d)->size)
extern void fatal(const char *, int);
/* --- cut here --- */
/*
* what berkeley db algorithm should we use for deadlock detection?
*
* DB_LOCK_DEFAULT
* Use the default policy as specified by db_deadlock.
* DB_LOCK_OLDEST
* Abort the oldest transaction.
* DB_LOCK_RANDOM
* Abort a random transaction involved in the deadlock.
* DB_LOCK_YOUNGEST
* Abort the youngest transaction.
*/
#define CONFIG_DEADLOCK_DETECTION DB_LOCK_YOUNGEST
#define MIN_CACHESIZE 20 /* 20KB per Sleepycat docs */
#define MAX_CACHESIZE 4194303 /* UINT32_MAX / 1024 */
/* --- cut here --- */
#if DB_VERSION_MAJOR >= 4
#define txn_checkpoint(xx1,xx2,xx3,xx4) (xx1)->txn_checkpoint(xx1,xx2,xx3,xx4)
#define txn_id(xx1) (xx1)->id(xx1)
#define log_archive(xx1,xx2,xx3,xx4) (xx1)->log_archive(xx1,xx2,xx3)
#define txn_begin(xx1,xx2,xx3,xx4) (xx1)->txn_begin(xx1,xx2,xx3,xx4)
#define txn_commit(xx1,xx2) (xx1)->commit(xx1,xx2)
#define txn_abort(xx1) (xx1)->abort(xx1)
#elif DB_VERSION_MINOR == 3
#define log_archive(xx1,xx2,xx3,xx4) log_archive(xx1,xx2,xx3)
#endif
static int dbinit = 0;
static DB_ENV *dbenv;
/* other routines call this one when they fail */
static int commit_txn(struct dbengine *db, struct txn *tid);
static int abort_txn(struct dbengine *db, struct txn *tid);
static void db_panic(DB_ENV *dbenv __attribute__((unused)),
int errno __attribute__((unused)))
{
syslog(LOG_CRIT, "DBERROR: critical database situation");
/* but don't bounce mail */
exit(EC_TEMPFAIL);
}
#if ((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 3)))
static void db_err(const DB_ENV *dbenv __attribute__((unused)),
const char *db_prfx, const char *buffer)
#else
static void db_err(const char *db_prfx, char *buffer)
#endif
{
syslog(LOG_WARNING, "DBERROR %s: %s", db_prfx, buffer);
}
static void db_msg(const DB_ENV *dbenv __attribute__((unused)),
const char *msg)
{
syslog(LOG_INFO, "DBMSG: %s", msg);
}
static int init(const char *dbdir, int myflags)
{
int r, do_retry = 1;
int flags = 0;
int maj, min, patch;
static char errpfx[10]; /* needs to be static; bdb doesn't copy */
int opt;
if (dbinit++) return 0;
db_version(&maj, &min, &patch);
if (maj != DB_VERSION_MAJOR || min != DB_VERSION_MINOR ||
DB_VERSION_PATCH > patch) {
syslog(LOG_CRIT, "incorrect version of Berkeley db: "
"compiled against %d.%d.%d, linked against %d.%d.%d",
DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH,
maj, min, patch);
fatal("wrong db version", EC_SOFTWARE);
}
if (myflags & CYRUSDB_RECOVER) {
flags |= DB_RECOVER | DB_CREATE;
}
if ((r = db_env_create(&dbenv, 0)) != 0) {
syslog(LOG_ERR, "DBERROR: db_appinit failed: %s", db_strerror(r));
return CYRUSDB_IOERROR;
}
dbenv->set_paniccall(dbenv, (void (*)(DB_ENV *, int)) &db_panic);
if (CONFIG_DB_VERBOSE) {
dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1);
}
if (CONFIG_DB_VERBOSE > 1) {
#ifdef DB_VERB_CHKPOINT
dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT, 1);
#endif
}
#if ((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 3)))
dbenv->set_msgcall(dbenv, db_msg);
#endif
dbenv->set_errcall(dbenv, db_err);
snprintf(errpfx, sizeof(errpfx), "db%d", DB_VERSION_MAJOR);
dbenv->set_errpfx(dbenv, errpfx);
dbenv->set_lk_detect(dbenv, CONFIG_DEADLOCK_DETECTION);
if ((opt = libcyrus_config_getint(CYRUSOPT_BERKELEY_LOCKS_MAX)) < 0) {
syslog(LOG_WARNING,
"DBERROR: invalid berkeley_locks_max value, using internal default");
} else {
#if DB_VERSION_MAJOR >= 4
r = dbenv->set_lk_max_locks(dbenv, opt);
if (!r)
r = dbenv->set_lk_max_lockers(dbenv, opt);
if (!r)
r = dbenv->set_lk_max_objects(dbenv, opt);
#else
r = dbenv->set_lk_max(dbenv, opt);
#endif
if (r) {
dbenv->err(dbenv, r, "set_lk_max");
syslog(LOG_ERR, "DBERROR: set_lk_max(): %s", db_strerror(r));
abort();
}
}
if ((opt = libcyrus_config_getint(CYRUSOPT_BERKELEY_TXNS_MAX)) < 0) {
syslog(LOG_WARNING,
"DBERROR: invalid berkeley_txns_max value, using internal default");
} else {
r = dbenv->set_tx_max(dbenv, opt);
if (r) {
dbenv->err(dbenv, r, "set_tx_max");
syslog(LOG_ERR, "DBERROR: set_tx_max(): %s", db_strerror(r));
abort();
}
}
opt = libcyrus_config_getint(CYRUSOPT_BERKELEY_CACHESIZE);
if (opt < MIN_CACHESIZE || opt > MAX_CACHESIZE) {
syslog(LOG_WARNING,
"DBERROR: invalid berkeley_cachesize value, using internal default");
} else {
r = dbenv->set_cachesize(dbenv, 0, opt * 1024, 0);
if (r) {
dbenv->err(dbenv, r, "set_cachesize");
(dbenv->close)(dbenv, 0);
syslog(LOG_ERR, "DBERROR: set_cachesize(): %s", db_strerror(r));
return CYRUSDB_IOERROR;
}
}
/* what directory are we in? */
retry:
flags |= DB_INIT_LOCK | DB_INIT_MPOOL |
DB_INIT_LOG | DB_INIT_TXN;
#if (DB_VERSION_MAJOR > 3) || ((DB_VERSION_MAJOR == 3) && (DB_VERSION_MINOR > 0))
r = (dbenv->open)(dbenv, dbdir, flags, 0644);
#else
r = (dbenv->open)(dbenv, dbdir, NULL, flags, 0644);
#endif
if (r) {
if (do_retry && (r == ENOENT)) {
/* Per sleepycat Support Request #3838 reporting a performance problem:
Berkeley DB only transactionally protects the open if you're
doing a DB_CREATE. Even if the Cyrus application is opening
the file read/write, we don't need a transaction. I see
from their source that they are always specifying DB_CREATE.
I bet if they changed it to not specifying CREATE and only
creating if necessary, the problem would probably go away.
Given that in general the file should exist, we optimize the most
often case: the file exists. So, we add DB_CREATE only if we fail
to open the file and thereby avoid doing a stat(2) needlessly. Sure, it
should be cached by why waste the cycles anyway?
*/
flags |= DB_CREATE;
do_retry = 0;
goto retry;
}
syslog(LOG_ERR, "DBERROR: dbenv->open '%s' failed: %s", dbdir,
db_strerror(r));
return CYRUSDB_IOERROR;
}
dbinit = 1;
return 0;
}
static int done(void)
{
int r;
if (--dbinit) return 0;
r = (dbenv->close)(dbenv, 0);
dbinit = 0;
if (r) {
syslog(LOG_ERR, "DBERROR: error exiting application: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
return 0;
}
static int mysync(void)
{
int r;
assert(dbinit);
#if !((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1)))
do {
#endif
#if (DB_VERSION_MAJOR > 3) || ((DB_VERSION_MAJOR == 3) && (DB_VERSION_MINOR > 0))
r = txn_checkpoint(dbenv, 0, 0, 0);
#else
r = txn_checkpoint(dbenv, 0, 0);
#endif
#if !((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1)))
} while (r == DB_INCOMPLETE); /* Never returned by BDB 4.1 */
#endif
if (r) {
syslog(LOG_ERR, "DBERROR: couldn't checkpoint: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
return 0;
}
static int myarchive(const strarray_t *fnames, const char *dirname)
{
int r;
char **item, **list;
char dstname[1024], *dp;
int length, rest;
strlcpy(dstname, dirname, sizeof(dstname));
length = strlen(dstname);
dp = dstname + length;
rest = sizeof(dstname) - length;
/* Get the list of log files to remove. */
r = log_archive(dbenv, &list, DB_ARCH_ABS, NULL);
if (r) {
syslog(LOG_ERR, "DBERROR: error listing log files: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (list != NULL) {
for (item = list; *item != NULL; ++item) {
syslog(LOG_DEBUG, "removing log file: %s", *item);
r = unlink(*item);
if (r) {
syslog(LOG_ERR, "DBERROR: error removing log file: %s",
*item);
return CYRUSDB_IOERROR;
}
}
free (list);
}
/* Get the list of database files to archive. */
/* XXX Should we do this, or just use the list given to us? */
r = log_archive(dbenv, &list, DB_ARCH_ABS | DB_ARCH_DATA, NULL);
if (r) {
syslog(LOG_ERR, "DBERROR: error listing database files: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (list != NULL) {
for (item = list; *item != NULL; ++item) {
/* only archive those files specified by the app */
if (strarray_find(fnames, *item, 0) >= 0) {
syslog(LOG_DEBUG, "archiving database file: %s", *item);
strlcpy(dp, strrchr(*item, '/'), rest);
r = cyrusdb_copyfile(*item, dstname);
if (r) {
syslog(LOG_ERR,
"DBERROR: error archiving database file: %s",
*item);
return CYRUSDB_IOERROR;
}
}
}
free (list);
}
/* Get the list of log files to archive. */
r = log_archive(dbenv, &list, DB_ARCH_ABS | DB_ARCH_LOG, NULL);
if (r) {
syslog(LOG_ERR, "DBERROR: error listing log files: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (list != NULL) {
for (item = list; *item != NULL; ++item) {
syslog(LOG_DEBUG, "archiving log file: %s", *item);
strcpy(dp, strrchr(*item, '/'));
r = cyrusdb_copyfile(*item, dstname);
if (r) {
syslog(LOG_ERR, "DBERROR: error archiving log file: %s",
*item);
return CYRUSDB_IOERROR;
}
}
free (list);
}
return 0;
}
static int mbox_compar(DB *db __attribute__((unused)),
const DBT *a, const DBT *b)
{
return bsearch_ncompare_mbox((const char *) a->data, a->size,
(const char *) b->data, b->size);
}
static int myopen(const char *fname, DBTYPE type, int flags, struct dbengine **ret)
{
DB *db = NULL;
int r;
int dbflags = (flags & CYRUSDB_CREATE) ? DB_CREATE : 0;
assert(dbinit && fname && ret);
*ret = NULL;
r = db_create(&db, dbenv, 0);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: opening %s (creating database handle): %s", fname, db_strerror(r));
return CYRUSDB_IOERROR;
}
/* xxx set comparator! */
if (flags & CYRUSDB_MBOXSORT) db->set_bt_compare(db, mbox_compar);
#if ((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 1)))
r = (db->open)(db, NULL, fname, NULL, type, dbflags | DB_AUTO_COMMIT, 0664);
#else
r = (db->open)(db, fname, NULL, type, dbflags, 0664);
#endif
if (r != 0) {
int level = (flags & CYRUSDB_CREATE) ? LOG_ERR : LOG_DEBUG;
syslog(level, "DBERROR: opening %s: %s", fname, db_strerror(r));
r = (db->close)(db, DB_NOSYNC);
if (r != 0) {
syslog(level, "DBERROR: closing %s: %s", fname, db_strerror(r));
}
return CYRUSDB_IOERROR;
}
*ret = (struct dbengine *) db;
return r;
}
static int open_btree(const char *fname, int flags, struct dbengine **ret)
{
return myopen(fname, DB_BTREE, flags, ret);
}
static int open_hash(const char *fname, int flags, struct dbengine **ret)
{
return myopen(fname, DB_HASH, flags, ret);
}
static int myclose(struct dbengine *db)
{
int r;
DB *a = (DB *) db;
assert(dbinit && db);
/* since we're using txns, we can supply DB_NOSYNC */
r = (a->close)(a, DB_NOSYNC);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: error closing: %s", db_strerror(r));
r = CYRUSDB_IOERROR;
}
return r;
}
static int gettid(struct txn **mytid, DB_TXN **tid, const char *where)
{
int r;
if (mytid) {
if (*mytid) {
assert((txn_id((DB_TXN *)*mytid) != 0));
*tid = (DB_TXN *) *mytid;
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "%s: reusing txn %lu", where,
(unsigned long) txn_id(*tid));
} else {
r = txn_begin(dbenv, NULL, tid, 0);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: error beginning txn (%s): %s", where,
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "%s: starting txn %lu", where,
(unsigned long) txn_id(*tid));
}
*mytid = (struct txn *) *tid;
}
return 0;
}
static int myfetch(struct dbengine *mydb,
const char *key, size_t keylen,
const char **data, size_t *datalen,
struct txn **mytid, int flags)
{
int r = 0;
DBT k, d;
DB *db = (DB *) mydb;
DB_TXN *tid = NULL;
assert(dbinit && db);
if (data) *data = NULL;
if (datalen) *datalen = 0;
r = gettid(mytid, &tid, "myfetch");
if (r) return r;
memset(&k, 0, sizeof(k));
memset(&d, 0, sizeof(d));
k.data = (char *) key;
k.size = keylen;
r = db->get(db, tid, &k, &d, flags);
switch (r) {
case 0:
if (data) *data = DATA(&d);
if (datalen) *datalen = DATALEN(&d);
break;
case DB_NOTFOUND:
r = CYRUSDB_NOTFOUND;
break;
case DB_LOCK_DEADLOCK:
if (mytid) {
abort_txn(mydb, *mytid);
*mytid = NULL;
}
r = CYRUSDB_AGAIN;
break;
default:
syslog(LOG_ERR, "DBERROR: error fetching %s: %s", key,
db_strerror(r));
r = CYRUSDB_IOERROR;
break;
}
return r;
}
static int fetch(struct dbengine *mydb,
const char *key, size_t keylen,
const char **data, size_t *datalen,
struct txn **mytid)
{
return myfetch(mydb, key, keylen, data, datalen, mytid, 0);
}
static int fetchlock(struct dbengine *mydb,
const char *key, size_t keylen,
const char **data, size_t *datalen,
struct txn **mytid)
{
return myfetch(mydb, key, keylen, data, datalen, mytid, DB_RMW);
}
#define OPENCURSOR() do { \
r = db->cursor(db, tid, &cursor, 0); \
if (r != 0) { \
syslog(LOG_ERR, "DBERROR: unable to create cursor: %s", \
db_strerror(r)); \
cursor = NULL; \
goto done; \
} \
} while (0)
#define CLOSECURSOR() do { \
int r = cursor->c_close(cursor); \
if (r) { \
syslog(LOG_ERR, "DBERROR: error closing cursor: %s", \
db_strerror(r)); \
cursor = NULL; \
goto done; \
} \
} while (0)
/* instead of "DB_DBT_REALLOC", we might want DB_DBT_USERMEM and allocate
this to the maximum length at the beginning. */
static int foreach(struct dbengine *mydb,
const char *prefix, size_t prefixlen,
foreach_p *goodp,
foreach_cb *cb, void *rock,
struct txn **mytid)
{
int r = 0;
DBT k, d;
DBC *cursor = NULL;
DB *db = (DB *) mydb;
DB_TXN *tid = NULL;
assert(dbinit && db);
assert(cb);
memset(&k, 0, sizeof(k));
memset(&d, 0, sizeof(d));
/* k.flags |= DB_DBT_REALLOC;
d.flags |= DB_DBT_REALLOC;*/
r = gettid(mytid, &tid, "foreach");
if (r) return r;
if (0) {
restart:
CLOSECURSOR();
}
/* create cursor */
OPENCURSOR();
/* find first record */
if (prefix && *prefix) {
/* if (k.data) free(k.data); */
k.data = (char *)prefix;
k.size = prefixlen;
r = cursor->c_get(cursor, &k, &d, DB_SET_RANGE);
} else {
r = cursor->c_get(cursor, &k, &d, DB_FIRST);
}
if (!tid && r == DB_LOCK_DEADLOCK) goto restart;
/* iterate over all mailboxes matching prefix */
while (!r) {
/* does this match our prefix? */
if (prefixlen && memcmp(k.data, prefix, prefixlen)) break;
if (!goodp || goodp(rock, k.data, k.size, DATA(&d), DATALEN(&d))) {
/* we have a winner! */
/* close the cursor, so we're not holding locks
during a callback */
CLOSECURSOR(); cursor = NULL;
r = cb(rock, k.data, k.size, DATA(&d), DATALEN(&d));
if (r != 0) {
if (r < 0) {
syslog(LOG_ERR, "DBERROR: foreach cb() failed");
}
/* don't mistake this for a db error */
r = 0;
break;
}
/* restore the current location & advance */
OPENCURSOR();
r = cursor->c_get(cursor, &k, &d, DB_SET);
switch (r) {
case 0:
r = cursor->c_get(cursor, &k, &d, DB_NEXT);
break;
case DB_NOTFOUND:
/* deleted during callback? */
r = cursor->c_get(cursor, &k, &d, DB_SET_RANGE);
break;
default:
/* handle other cases below */
break;
}
} else {
/* advance the cursor */
r = cursor->c_get(cursor, &k, &d, DB_NEXT);
}
while (r == DB_LOCK_DEADLOCK) {
if (tid) {
break; /* don't autoretry txn-protected */
}
/* if we deadlock, close and reopen the cursor, and
reposition it */
CLOSECURSOR();
OPENCURSOR();
r = cursor->c_get(cursor, &k, &d, DB_SET);
switch (r) {
case 0:
r = cursor->c_get(cursor, &k, &d, DB_NEXT);
break;
case DB_LOCK_DEADLOCK:
continue;
case DB_NOTFOUND: /* deleted? */
r = cursor->c_get(cursor, &k, &d, DB_SET_RANGE);
break;
}
}
}
done:
if (cursor) {
CLOSECURSOR();
}
switch (r) {
case 0: /* ok */
break;
case DB_NOTFOUND: /* also ok */
r = 0;
break;
case DB_LOCK_DEADLOCK: /* erg, we're in a txn! */
if (mytid) {
abort_txn(mydb, *mytid);
*mytid = NULL;
}
r = CYRUSDB_AGAIN;
break;
default:
if (mytid) {
abort_txn(mydb, *mytid);
*mytid = NULL;
}
syslog(LOG_ERR, "DBERROR: error advancing: %s", db_strerror(r));
r = CYRUSDB_IOERROR;
break;
}
/* if (k.data) free(k.data);
if (d.data) free(d.data);*/
return r;
}
static int mystore(struct dbengine *mydb,
const char *key, size_t keylen,
const char *data, size_t datalen,
struct txn **mytid, int putflags, int txnflags)
{
int r = 0;
DBT k, d;
DB_TXN *tid;
DB *db = (DB *) mydb;
assert(dbinit && db);
assert(key && keylen);
if (!data)
datalen = 0;
r = gettid(mytid, &tid, "mystore");
if (r) return r;
memset(&k, 0, sizeof(k));
memset(&d, 0, sizeof(d));
k.data = (char *) key;
k.size = keylen;
d.data = (char *) data;
d.size = datalen;
if (!mytid) {
/* start a transaction for the write */
restart:
r = txn_begin(dbenv, NULL, &tid, 0);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: mystore: error beginning txn: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mystore: starting txn %lu",
(unsigned long) txn_id(tid));
}
r = db->put(db, tid, &k, &d, putflags);
if (!mytid) {
/* finish once-off txn */
if (r) {
int r2;
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mystore: aborting txn %lu",
(unsigned long) txn_id(tid));
r2 = txn_abort(tid);
if (r2) {
syslog(LOG_ERR, "DBERROR: mystore: error aborting txn: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (r == DB_LOCK_DEADLOCK) {
goto restart;
}
} else {
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mystore: committing txn %lu",
(unsigned long) txn_id(tid));
r = txn_commit(tid, txnflags);
}
}
if ( r != 0) {
if (mytid) {
abort_txn(mydb, *mytid);
*mytid = NULL;
}
if (r == DB_LOCK_DEADLOCK) {
r = CYRUSDB_AGAIN;
} else {
syslog(LOG_ERR, "DBERROR: mystore: error storing %s: %s",
key, db_strerror(r));
r = CYRUSDB_IOERROR;
}
}
return r;
}
static int create(struct dbengine *db,
const char *key, size_t keylen,
const char *data, size_t datalen,
struct txn **tid)
{
return mystore(db, key, keylen, data, datalen, tid, DB_NOOVERWRITE, 0);
}
static int store(struct dbengine *db,
const char *key, size_t keylen,
const char *data, size_t datalen,
struct txn **tid)
{
return mystore(db, key, keylen, data, datalen, tid, 0, 0);
}
static int create_nosync(struct dbengine *db,
const char *key, size_t keylen,
const char *data, size_t datalen,
struct txn **tid)
{
return mystore(db, key, keylen, data, datalen, tid, DB_NOOVERWRITE,
DB_TXN_NOSYNC);
}
static int store_nosync(struct dbengine *db,
const char *key, size_t keylen,
const char *data, size_t datalen,
struct txn **tid)
{
return mystore(db, key, keylen, data, datalen, tid, 0, DB_TXN_NOSYNC);
}
static int mydelete(struct dbengine *mydb,
const char *key, size_t keylen,
struct txn **mytid, int txnflags, int force)
{
int r = 0;
DBT k;
DB_TXN *tid;
DB *db = (DB *) mydb;
assert(dbinit && db);
assert(key && keylen);
r = gettid(mytid, &tid, "delete");
if (r) return r;
memset(&k, 0, sizeof(k));
k.data = (char *) key;
k.size = keylen;
if (!mytid) {
restart:
/* start txn for the write */
r = txn_begin(dbenv, NULL, &tid, 0);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: mydelete: error beginning txn: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mydelete: starting txn %lu",
(unsigned long) txn_id(tid));
}
r = db->del(db, tid, &k, 0);
if (force && r == DB_NOTFOUND)
r = CYRUSDB_OK; /* ignore not found errors */
if (!mytid) {
/* finish txn for the write */
if (r) {
int r2;
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mydelete: aborting txn %lu",
(unsigned long) txn_id(tid));
r2 = txn_abort(tid);
if (r2) {
syslog(LOG_ERR, "DBERROR: mydelete: error aborting txn: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
if (r == DB_LOCK_DEADLOCK) {
goto restart;
}
} else {
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mydelete: committing txn %lu",
(unsigned long) txn_id(tid));
r = txn_commit(tid, txnflags);
}
}
if (r != 0) {
if (mytid) {
abort_txn(mydb, *mytid);
*mytid = NULL;
}
if (r == DB_LOCK_DEADLOCK) {
r = CYRUSDB_AGAIN;
} else {
syslog(LOG_ERR, "DBERROR: mydelete: error deleting %s: %s",
key, db_strerror(r));
r = CYRUSDB_IOERROR;
}
}
return r;
}
static int delete(struct dbengine *db,
const char *key, size_t keylen,
struct txn **tid, int force)
{
return mydelete(db, key, keylen, tid, 0, force);
}
static int delete_nosync(struct dbengine *db,
const char *key, size_t keylen,
struct txn **tid, int force)
{
return mydelete(db, key, keylen, tid, DB_TXN_NOSYNC, force);
}
static int mycommit(struct dbengine *db __attribute__((unused)),
struct txn *tid, int txnflags)
{
int r;
DB_TXN *t = (DB_TXN *) tid;
assert(dbinit && tid);
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "mycommit: committing txn %lu",
(unsigned long) txn_id(t));
r = txn_commit(t, txnflags);
switch (r) {
case 0:
break;
case EINVAL:
syslog(LOG_WARNING, "mycommit: tried to commit an already aborted transaction");
r = CYRUSDB_IOERROR;
break;
default:
syslog(LOG_ERR, "DBERROR: mycommit failed on commit: %s",
db_strerror(r));
r = CYRUSDB_IOERROR;
break;
}
return r;
}
static int commit_txn(struct dbengine *db, struct txn *tid)
{
return mycommit(db, tid, 0);
}
static int commit_nosync(struct dbengine *db, struct txn *tid)
{
return mycommit(db, tid, DB_TXN_NOSYNC);
}
static int abort_txn(struct dbengine *db __attribute__((unused)),
struct txn *tid)
{
int r;
DB_TXN *t = (DB_TXN *) tid;
assert(dbinit && tid);
if (CONFIG_DB_VERBOSE)
syslog(LOG_DEBUG, "abort_txn: aborting txn %lu",
(unsigned long) txn_id(t));
r = txn_abort(t);
if (r != 0) {
syslog(LOG_ERR, "DBERROR: abort_txn: error aborting txn: %s",
db_strerror(r));
return CYRUSDB_IOERROR;
}
return 0;
}
static int mycompar(struct dbengine *mydb
#if ((DB_VERSION_MAJOR > 4) || ((DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 8)))
,
const char *a, int alen,
const char *b, int blen)
{