1 // This is a data store backend for the Citadel server which uses Berkeley DB.
3 // Copyright (c) 1987-2023 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
8 // Citadel will checkpoint the db at the end of every session, but only if
9 // the specified number of kilobytes has been written, or if the specified
10 // number of minutes has passed, since the last checkpoint.
11 #define MAX_CHECKPOINT_KBYTES 256
12 #define MAX_CHECKPOINT_MINUTES 15
14 #include "../../sysdep.h"
23 #if DB_VERSION_MAJOR < 18
24 #error Citadel requires Berkeley DB v18.0 or newer. Please upgrade.
27 #include <libcitadel.h>
28 #include "../../ctdl_module.h"
29 #include "../../control.h"
30 #include "../../citserver.h"
31 #include "../../config.h"
32 #include "berkeley_db.h"
34 void (*cdb_open_databases)(void) = NULL;
35 void (*cdb_close_databases)(void) = NULL;
36 int (*cdb_store)(int, const void *, int, void *, int) = NULL;
37 int (*cdb_delete)(int, void *, int) = NULL;
38 void (*cdb_free)(struct cdbdata *) = NULL;
39 struct cdbdata * (*cdb_next_item)(int) = NULL;
40 void (*cdb_close_cursor)(int) = NULL;
41 void (*cdb_begin_transaction)(void) = NULL;
42 void (*cdb_end_transaction)(void) = NULL;
43 void (*cdb_check_handles)(void) = NULL;
44 void (*cdb_trunc)(int) = NULL;
45 void (*cdb_chmod_data)(void) = NULL;
46 void (*check_handles)(void *) = NULL;
47 void (*cdb_compact)(void) = NULL;
48 void (*cdb_checkpoint)(void) = NULL;
49 void (*cdb_rewind)(int) = NULL;
50 struct cdbdata * (*cdb_fetch)(int, const void *, int) = NULL;
52 static DB *dbp[MAXCDB]; // One DB handle for each Citadel database
53 static DB_ENV *dbenv; // The DB environment (global)
56 void bdb_abort(void) {
57 syslog(LOG_DEBUG, "bdb: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
58 getuid(), getgid(), geteuid(), getegid()
60 raise(SIGABRT); // This will exit in a way that can produce a core dump if needed.
61 exit(CTDLEXIT_DB); // Exit if the signal failed to end the program.
65 // Verbose logging callback
66 void bdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *foo) {
67 if (!IsEmptyStr(msg)) {
68 syslog(LOG_DEBUG, "bdb: %s %s", msg, foo);
73 // Verbose logging callback
74 void bdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg) {
75 syslog(LOG_ERR, "bdb: %s", msg);
79 // wrapper for txn_abort() that logs/aborts on error
80 static void txabort(DB_TXN *tid) {
83 ret = tid->abort(tid);
86 syslog(LOG_ERR, "bdb: txn_abort: %s", db_strerror(ret));
92 // wrapper for txn_commit() that logs/aborts on error
93 static void txcommit(DB_TXN *tid) {
96 ret = tid->commit(tid, 0);
99 syslog(LOG_ERR, "bdb: txn_commit: %s", db_strerror(ret));
105 // wrapper for txn_begin() that logs/aborts on error
106 static void txbegin(DB_TXN **tid) {
109 ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
112 syslog(LOG_ERR, "bdb: txn_begin: %s", db_strerror(ret));
119 static void dbpanic(DB_ENV *env, int errval) {
120 syslog(LOG_ERR, "bdb: PANIC: %s", db_strerror(errval));
125 static void cclose(DBC *cursor) {
128 if ((ret = cursor->c_close(cursor))) {
129 syslog(LOG_ERR, "bdb: c_close: %s", db_strerror(ret));
135 static void bailIfCursor(DBC **cursors, const char *msg) {
138 for (i = 0; i < MAXCDB; i++)
139 if (cursors[i] != NULL) {
140 syslog(LOG_ERR, "bdb: cursor still in progress on cdb %02x: %s", i, msg);
146 void bdb_check_handles(void) {
147 bailIfCursor(TSD->cursors, "in check_handles");
149 if (TSD->tid != NULL) {
150 syslog(LOG_ERR, "bdb: transaction still in progress!");
156 // Request a checkpoint of the database. Called once per minute by the thread manager.
157 void bdb_checkpoint(void) {
160 syslog(LOG_DEBUG, "bdb: -- checkpoint --");
161 ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
164 syslog(LOG_ERR, "bdb: bdb_checkpoint() txn_checkpoint: %s", db_strerror(ret));
168 // After a successful checkpoint, we can cull the unused logs
169 if (CtdlGetConfigInt("c_auto_cull")) {
170 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 1);
173 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 0);
178 // Open the various databases we'll be using. Any database which
179 // does not exist should be created. Note that we don't need a
180 // critical section here, because there aren't any active threads
181 // manipulating the database yet.
182 void bdb_open_databases(void) {
187 int dbversion_major, dbversion_minor, dbversion_patch;
189 syslog(LOG_DEBUG, "bdb: bdb_open_databases() starting");
190 syslog(LOG_DEBUG, "bdb: Linked zlib: %s", zlibVersion());
191 syslog(LOG_DEBUG, "bdb: Compiled libdb: %s", DB_VERSION_STRING);
192 syslog(LOG_DEBUG, "bdb: Linked libdb: %s", db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
194 // Create synthetic integer version numbers and compare them.
195 // Never allow citserver to run with a libdb older then the one with which it was compiled.
196 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
197 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
198 if (compiled_db_version > linked_db_version) {
199 syslog(LOG_ERR, "bdb: citserver is running with a version of libdb older than the one with which it was compiled.");
200 syslog(LOG_ERR, "bdb: This is an invalid configuration. citserver will now exit to prevent data loss.");
204 // Silently try to create the database subdirectory. If it's already there, no problem.
205 if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) {
206 syslog(LOG_ERR, "bdb: database directory [%s] does not exist and could not be created: %m", ctdl_db_dir);
209 if (chmod(ctdl_db_dir, 0700) != 0) {
210 syslog(LOG_ERR, "bdb: unable to set database directory permissions [%s]: %m", ctdl_db_dir);
213 if (chown(ctdl_db_dir, CTDLUID, (-1)) != 0) {
214 syslog(LOG_ERR, "bdb: unable to set the owner for [%s]: %m", ctdl_db_dir);
217 syslog(LOG_DEBUG, "bdb: Setting up DB environment");
218 ret = db_env_create(&dbenv, 0);
220 syslog(LOG_ERR, "bdb: db_env_create: %s", db_strerror(ret));
221 syslog(LOG_ERR, "bdb: exit code %d", ret);
224 dbenv->set_errpfx(dbenv, "citserver");
225 dbenv->set_paniccall(dbenv, dbpanic);
226 dbenv->set_errcall(dbenv, bdb_verbose_err);
227 dbenv->set_msgcall(dbenv, bdb_verbose_log);
228 dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
229 dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
231 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
232 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
234 syslog(LOG_ERR, "bdb: set_cachesize: %s", db_strerror(ret));
235 dbenv->close(dbenv, 0);
236 syslog(LOG_ERR, "bdb: exit code %d", ret);
240 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
241 syslog(LOG_ERR, "bdb: set_lk_detect: %s", db_strerror(ret));
242 dbenv->close(dbenv, 0);
243 syslog(LOG_ERR, "bdb: exit code %d", ret);
247 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_INIT_LOG;
248 syslog(LOG_DEBUG, "bdb: dbenv->open(dbenv, %s, %d, 0)", ctdl_db_dir, flags);
249 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try opening the database cleanly
250 if (ret == DB_RUNRECOVERY) {
251 syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret));
252 syslog(LOG_ERR, "bdb: attempting recovery...");
254 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try recovery
256 if (ret == DB_RUNRECOVERY) {
257 syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret));
258 syslog(LOG_ERR, "bdb: attempting catastrophic recovery...");
259 flags &= ~DB_RECOVER;
260 flags |= DB_RECOVER_FATAL;
261 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try catastrophic recovery
264 syslog(LOG_ERR, "bdb: dbenv->open: %s", db_strerror(ret));
265 dbenv->close(dbenv, 0);
266 syslog(LOG_ERR, "bdb: exit code %d", ret);
270 syslog(LOG_INFO, "bdb: mounting databases");
271 for (i = 0; i < MAXCDB; ++i) {
272 ret = db_create(&dbp[i], dbenv, 0); // Create a database handle
274 syslog(LOG_ERR, "bdb: db_create: %s", db_strerror(ret));
275 syslog(LOG_ERR, "bdb: exit code %d", ret);
279 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i); // table names by number
280 ret = dbp[i]->open(dbp[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_THREAD, 0600);
282 syslog(LOG_ERR, "bdb: db_open[%02x]: %s", i, db_strerror(ret));
284 syslog(LOG_ERR, "bdb: You may need to tune your database; please check http://www.citadel.org for more information.");
286 syslog(LOG_ERR, "bdb: exit code %d", ret);
293 // Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
294 void bdb_chmod_data(void) {
297 char filename[PATH_MAX];
299 dp = opendir(ctdl_db_dir);
301 while (d = readdir(dp), d != NULL) {
302 if (d->d_name[0] != '.') {
303 snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name);
304 syslog(LOG_DEBUG, "bdb: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
305 syslog(LOG_DEBUG, "bdb: chown(%s, CTDLUID, -1) returned %d", filename, chown(filename, CTDLUID, (-1)));
313 // Close all of the db database files we've opened. This can be done in a loop, since it's just a bunch of closes.
314 void bdb_close_databases(void) {
318 static int closing = 0;
319 while (closing == 1) {
320 syslog(LOG_INFO, "bdb: already closing");
324 syslog(LOG_INFO, "bdb: performing final checkpoint");
325 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
326 syslog(LOG_ERR, "bdb: txn_checkpoint: %s", db_strerror(ret));
329 syslog(LOG_INFO, "bdb: flushing the database logs");
330 if ((ret = dbenv->log_flush(dbenv, NULL))) {
331 syslog(LOG_ERR, "bdb: log_flush: %s", db_strerror(ret));
335 syslog(LOG_INFO, "bdb: closing databases");
336 for (i = 0; i < MAXCDB; ++i) {
337 syslog(LOG_INFO, "bdb: closing database %02x", i);
338 ret = dbp[i]->close(dbp[i], 0);
340 syslog(LOG_ERR, "bdb: db_close: %s", db_strerror(ret));
345 ret = dbenv->close(dbenv, DB_FORCESYNC);
347 syslog(LOG_ERR, "bdb: DBENV->close: %s", db_strerror(ret));
352 // Decompress a database item if it was compressed on disk
353 void bdb_decompress_if_necessary(struct cdbdata *cdb) {
354 static int magic = COMPRESS_MAGIC;
356 if ((cdb == NULL) || (cdb->ptr == NULL) || (cdb->len < sizeof(magic)) || (memcmp(cdb->ptr, &magic, sizeof(magic)))) {
360 // At this point we know we're looking at a compressed item.
362 struct CtdlCompressHeader zheader;
363 char *uncompressed_data;
364 char *compressed_data;
365 uLongf destLen, sourceLen;
368 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
369 cplen = sizeof(struct CtdlCompressHeader);
370 if (sizeof(struct CtdlCompressHeader) > cdb->len) {
373 memcpy(&zheader, cdb->ptr, cplen);
375 compressed_data = cdb->ptr;
376 compressed_data += sizeof(struct CtdlCompressHeader);
378 sourceLen = (uLongf) zheader.compressed_len;
379 destLen = (uLongf) zheader.uncompressed_len;
380 uncompressed_data = malloc(zheader.uncompressed_len);
382 if (uncompress((Bytef *) uncompressed_data,
383 (uLongf *) &destLen, (const Bytef *) compressed_data, (uLong) sourceLen) != Z_OK) {
384 syslog(LOG_ERR, "bdb: uncompress() error");
389 cdb->len = (size_t) destLen;
390 cdb->ptr = uncompressed_data;
394 // Store a piece of data. Returns 0 if the operation was successful. If a
395 // key already exists it should be overwritten.
396 int bdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) {
401 struct CtdlCompressHeader zheader;
402 char *compressed_data = NULL;
404 size_t buffer_len = 0;
407 memset(&dkey, 0, sizeof(DBT));
408 memset(&ddata, 0, sizeof(DBT));
410 dkey.data = (void *) ckey;
411 ddata.size = cdatalen;
414 // "visit" records are numerous and have big, mostly-empty string buffers in them.
415 // If we compress these we can get them down to 1% of their size most of the time.
416 if (cdb == CDB_VISIT) {
418 zheader.magic = COMPRESS_MAGIC;
419 zheader.uncompressed_len = cdatalen;
420 buffer_len = ((cdatalen * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader);
421 destLen = (uLongf) buffer_len;
422 compressed_data = malloc(buffer_len);
423 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)), &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK) {
424 syslog(LOG_ERR, "bdb: compress2() error");
427 zheader.compressed_len = (size_t) destLen;
428 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
429 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
430 ddata.data = compressed_data;
433 if (TSD->tid != NULL) {
434 ret = dbp[cdb]->put(dbp[cdb], // db
435 TSD->tid, // transaction ID
441 syslog(LOG_ERR, "bdb: bdb_store(%d): %s", cdb, db_strerror(ret));
445 free(compressed_data);
450 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
455 if ((ret = dbp[cdb]->put(dbp[cdb], // db
456 tid, // transaction ID
460 if (ret == DB_LOCK_DEADLOCK) {
465 syslog(LOG_ERR, "bdb: bdb_store(%d): %s", cdb, db_strerror(ret));
472 free(compressed_data);
481 // Delete a piece of data. Returns 0 if the operation was successful.
482 int bdb_delete(int cdb, void *key, int keylen) {
487 memset(&dkey, 0, sizeof dkey);
491 if (TSD->tid != NULL) {
492 ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
494 syslog(LOG_ERR, "bdb: bdb_delete(%d): %s", cdb, db_strerror(ret));
495 if (ret != DB_NOTFOUND) {
501 bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
506 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0)) && ret != DB_NOTFOUND) {
507 if (ret == DB_LOCK_DEADLOCK) {
512 syslog(LOG_ERR, "bdb: bdb_delete(%d): %s", cdb, db_strerror(ret));
524 static DBC *localcursor(int cdb) {
528 if (TSD->cursors[cdb] == NULL) {
529 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
532 ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
536 syslog(LOG_ERR, "bdb: localcursor: %s", db_strerror(ret));
544 // Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
545 // a struct cdbdata which it is the caller's responsibility to free later on
546 // using the bdb_free() routine.
547 struct cdbdata *bdb_fetch(int cdb, const void *key, int keylen) {
549 if (keylen == 0) { // key length zero is impossible
553 struct cdbdata *tempcdb;
557 memset(&dkey, 0, sizeof(DBT));
559 dkey.data = (void *) key;
561 if (TSD->tid != NULL) {
562 memset(&dret, 0, sizeof(DBT));
563 dret.flags = DB_DBT_MALLOC;
564 ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
570 memset(&dret, 0, sizeof(DBT));
571 dret.flags = DB_DBT_MALLOC;
572 curs = localcursor(cdb);
573 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
575 } while (ret == DB_LOCK_DEADLOCK);
578 if ((ret != 0) && (ret != DB_NOTFOUND)) {
579 syslog(LOG_ERR, "bdb: bdb_fetch(%d): %s", cdb, db_strerror(ret));
587 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
588 if (tempcdb == NULL) {
589 syslog(LOG_ERR, "bdb: bdb_fetch() cannot allocate memory for tempcdb: %m");
593 tempcdb->len = dret.size;
594 tempcdb->ptr = dret.data;
595 bdb_decompress_if_necessary(tempcdb);
601 // Free a cdbdata item.
603 // Note that we only free the 'ptr' portion if it is not NULL. This allows
604 // other code to assume ownership of that memory simply by storing the
605 // pointer elsewhere and then setting 'ptr' to NULL. bdb_free() will then
607 void bdb_free(struct cdbdata *cdb) {
615 void bdb_close_cursor(int cdb) {
616 if (TSD->cursors[cdb] != NULL) {
617 cclose(TSD->cursors[cdb]);
620 TSD->cursors[cdb] = NULL;
624 // Prepare for a sequential search of an entire database.
625 // (There is guaranteed to be no more than one traversal in
626 // progress per thread at any given time.)
627 void bdb_rewind(int cdb) {
630 if (TSD->cursors[cdb] != NULL) {
631 syslog(LOG_ERR, "bdb: bdb_rewind: must close cursor on database %d before reopening", cdb);
633 // cclose(TSD->cursors[cdb]);
636 // Now initialize the cursor
637 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
639 syslog(LOG_ERR, "bdb: bdb_rewind: db_cursor: %s", db_strerror(ret));
645 // Fetch the next item in a sequential search. Returns a pointer to a
646 // cdbdata structure, or NULL if we've hit the end.
647 struct cdbdata *bdb_next_item(int cdb) {
649 struct cdbdata *cdbret;
652 // Initialize the key/data pair so the flags aren't set.
653 memset(&key, 0, sizeof(key));
654 memset(&data, 0, sizeof(data));
655 data.flags = DB_DBT_MALLOC;
657 ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
660 if (ret != DB_NOTFOUND) {
661 syslog(LOG_ERR, "bdb: bdb_next_item(%d): %s", cdb, db_strerror(ret));
664 bdb_close_cursor(cdb);
665 return NULL; // presumably, end of file
668 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
669 cdbret->len = data.size;
670 cdbret->ptr = data.data;
671 bdb_decompress_if_necessary(cdbret);
677 // Transaction-based stuff. I'm writing this as I bake cookies...
678 void bdb_begin_transaction(void) {
679 bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
681 if (TSD->tid != NULL) {
682 syslog(LOG_ERR, "bdb: bdb_begin_transaction: ERROR: nested transaction");
690 void bdb_end_transaction(void) {
693 for (i = 0; i < MAXCDB; i++) {
694 if (TSD->cursors[i] != NULL) {
695 syslog(LOG_WARNING, "bdb: bdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
696 cclose(TSD->cursors[i]);
697 TSD->cursors[i] = NULL;
701 if (TSD->tid == NULL) {
702 syslog(LOG_ERR, "bdb: bdb_end_transaction: ERROR: txcommit(NULL) !!");
713 // Truncate (delete every record)
714 void bdb_trunc(int cdb) {
718 if (TSD->tid != NULL) {
719 syslog(LOG_ERR, "bdb: bdb_trunc must not be called in a transaction.");
723 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
727 if ((ret = dbp[cdb]->truncate(dbp[cdb], // db
728 NULL, // transaction ID
729 &count, // #rows deleted
731 if (ret == DB_LOCK_DEADLOCK) {
735 syslog(LOG_ERR, "bdb: bdb_truncate(%d): %s", cdb, db_strerror(ret));
737 syslog(LOG_ERR, "bdb: You may need to tune your database; please read http://www.citadel.org for more information.");
746 // compact (defragment) the database, possibly returning space back to the underlying filesystem
747 void bdb_compact(void) {
751 syslog(LOG_DEBUG, "bdb: bdb_compact() started");
752 for (i = 0; i < MAXCDB; i++) {
753 syslog(LOG_DEBUG, "bdb: compacting database %d", i);
754 ret = dbp[i]->compact(dbp[i], NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
756 syslog(LOG_ERR, "bdb: compact: %s", db_strerror(ret));
759 syslog(LOG_DEBUG, "bdb: bdb_compact() finished");
763 // Calling this function activates the Berkeley DB back end.
764 void bdb_init_backend(void) {
765 cdb_compact = bdb_compact;
766 cdb_checkpoint = bdb_checkpoint;
767 cdb_rewind = bdb_rewind;
768 cdb_fetch = bdb_fetch;
769 cdb_open_databases = bdb_open_databases;
770 cdb_close_databases = bdb_close_databases;
771 cdb_store = bdb_store;
772 cdb_delete = bdb_delete;
774 cdb_next_item = bdb_next_item;
775 cdb_close_cursor = bdb_close_cursor;
776 cdb_begin_transaction = bdb_begin_transaction;
777 cdb_end_transaction = bdb_end_transaction;
778 cdb_check_handles = bdb_check_handles;
779 cdb_trunc = bdb_trunc;
780 cdb_chmod_data = bdb_chmod_data;
782 syslog(LOG_INFO, "db: initialized Berkeley DB backend");