berkeley_db: cdb_next_item() use DB_REALLOC, not DB_MALLOC
[citadel.git] / citadel / server / backends / berkeley_db / berkeley_db.c
1 // This is a data store backend for the Citadel server which uses Berkeley DB.
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
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.
7
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
13
14 #include "../../sysdep.h"
15 #include <sys/stat.h>
16 #include <stdio.h>
17 #include <zlib.h>
18 #include <db.h>
19 #include <assert.h>
20 #include <libcitadel.h>
21 #include "../../citserver.h"
22 #include "../../config.h"
23 #include "berkeley_db.h"
24
25 #if DB_VERSION_MAJOR < 18
26 #error Citadel requires Berkeley DB v18.0 or newer.  Please upgrade.
27 #endif
28
29 // Globals (these are used across multiple functions in *this* module, but not elsewhere)
30 static DB *bdb_table[MAXCDB];   // One DB handle for each Citadel database
31 static DB_ENV *bdb_env;         // The DB environment (global)
32
33
34 // These are items that we need to store "per thread" rather than "per session".
35 struct bdb_tsd {
36         DB_TXN *tid;            // Transaction handle
37         DBC *cursors[MAXCDB];   // Cursors, for traversals...
38         DBT dbkey[MAXCDB];
39         DBT dbdata[MAXCDB];
40 };
41
42
43 pthread_key_t bdb_thread_key;
44 #define TSD bdb_get_tsd()
45
46
47 // Some functions in this backend need to store some per-thread data.
48 // This returns the pointer to the current thread's per-thread data block, creating it if necessary.
49 // (This will also work in a non-threaded program; it will return the same pointer every time.)
50 struct bdb_tsd *bdb_get_tsd(void) {
51
52         struct bdb_tsd *c = (struct bdb_tsd *) pthread_getspecific(bdb_thread_key) ;
53         if (c != NULL) {
54                 return(c);              // Got it.
55         }
56
57         // If there's no TSD for this thread, it must be a new thread.  Create our TSD region.
58         c = (struct bdb_tsd *) malloc(sizeof(struct bdb_tsd));
59         memset(c, 0, sizeof(struct bdb_tsd));
60         pthread_setspecific(bdb_thread_key, (const void *) c);
61         return(c);
62 }
63
64
65 // Called by other functions in this module to GTFO quickly if we need to.  Not part of the backend API.
66 void bdb_abort(void) {
67         syslog(LOG_DEBUG, "bdb: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
68                 getuid(), getgid(), geteuid(), getegid()
69         );
70         raise(SIGABRT);         // This will exit in a way that can produce a core dump if needed.
71         exit(CTDLEXIT_DB);      // Exit if the signal failed to end the program.
72 }
73
74
75 // Verbose logging callback for Berkeley DB.  Not part of the backend API.
76 void bdb_verbose_log(const DB_ENV *bdb_env, const char *msg, const char *foo) {
77         if (!IsEmptyStr(msg)) {
78                 syslog(LOG_DEBUG, "bdb: %s %s", msg, foo);
79         }
80 }
81
82
83 // Verbose error logging callback for Berkeley DB.  Not part of the backend API.
84 void bdb_verbose_err(const DB_ENV *bdb_env, const char *errpfx, const char *msg) {
85         syslog(LOG_ERR, "bdb: %s", msg);
86 }
87
88
89 // Wrapper for txn_abort() that logs/aborts on error.  Not part of the backend API.
90 static void bdb_txabort(DB_TXN *tid) {
91         int ret;
92
93         ret = tid->abort(tid);
94
95         if (ret) {
96                 syslog(LOG_ERR, "bdb: txn_abort: %s", db_strerror(ret));
97                 bdb_abort();
98         }
99 }
100
101
102 // Wrapper for txn_commit() that logs/aborts on error.  Not part of the backend API.
103 static void bdb_txcommit(DB_TXN *tid) {
104         int ret;
105
106         ret = tid->commit(tid, 0);
107
108         if (ret) {
109                 syslog(LOG_ERR, "bdb: txn_commit: %s", db_strerror(ret));
110                 bdb_abort();
111         }
112 }
113
114
115 // Panic callback for Berkeley DB.  Not part of the backend API.
116 static void bdb_dbpanic(DB_ENV *env, int errval) {
117         syslog(LOG_ERR, "bdb: PANIC: %s", db_strerror(errval));
118         bdb_abort();
119 }
120
121
122 // Close a cursor -- not part of the backend API.
123 static void bdb_cclose(DBC *cursor) {
124         int ret;
125
126         if ((ret = cursor->c_close(cursor))) {
127                 syslog(LOG_ERR, "bdb: c_close: %s", db_strerror(ret));
128                 bdb_abort();
129         }
130 }
131
132
133 // Convenience function to abort if a cursor is still open when we didn't expect it to be.
134 // This is not part of the backend API.
135 static void bdb_bailIfCursor(DBC **cursors, const char *msg) {
136         int i;
137
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);
141                         bdb_abort();
142                 }
143 }
144
145
146 void bdb_check_handles(void) {
147         bdb_bailIfCursor(TSD->cursors, "in check_handles");
148
149         if (TSD->tid != NULL) {
150                 syslog(LOG_ERR, "bdb: transaction still in progress!");
151                 bdb_abort();
152         }
153 }
154
155
156 // Transaction-based stuff.  I'm writing this as I bake cookies...
157 void bdb_begin_transaction(void) {
158         int ret;
159         bdb_bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
160
161         if (TSD->tid != NULL) {
162                 syslog(LOG_ERR, "bdb: bdb_begin_transaction: ERROR: nested transaction");
163                 bdb_abort();
164         }
165
166         ret = bdb_env->txn_begin(bdb_env, NULL, &TSD->tid, 0);
167         if (ret) {
168                 syslog(LOG_ERR, "bdb: bdb_begin_transaction: %s", db_strerror(ret));
169                 bdb_abort();
170         }
171 }
172
173
174 // ...and the cookies are cursed.
175 void bdb_end_transaction(void) {
176         int i;
177
178         for (i = 0; i < MAXCDB; i++) {
179                 if (TSD->cursors[i] != NULL) {
180                         syslog(LOG_WARNING, "bdb: bdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
181                         bdb_cclose(TSD->cursors[i]);
182                         TSD->cursors[i] = NULL;
183                 }
184         }
185
186         if (TSD->tid == NULL) {
187                 syslog(LOG_ERR, "bdb: bdb_end_transaction: ERROR: bdb_txcommit(NULL) !!");
188                 bdb_abort();
189         }
190         else {
191                 bdb_txcommit(TSD->tid);
192         }
193
194         TSD->tid = NULL;
195 }
196
197
198 // Request a checkpoint of the database.  Called once per minute by the thread manager.
199 void bdb_checkpoint(void) {
200         int ret;
201
202         syslog(LOG_DEBUG, "bdb: -- checkpoint --");
203         ret = bdb_env->txn_checkpoint(bdb_env, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
204
205         if (ret != 0) {
206                 syslog(LOG_ERR, "bdb: bdb_checkpoint() txn_checkpoint: %s", db_strerror(ret));
207                 bdb_abort();
208         }
209
210         // After a successful checkpoint, we can cull the unused logs
211         ret = bdb_env->log_set_config(bdb_env, DB_LOG_AUTO_REMOVE, 1);
212         if (ret != 0) {
213                 syslog(LOG_ERR, "bdb: bdb_checkpoint() auto coll logs: %s", db_strerror(ret));
214         }
215 }
216
217
218 // Open the various tables we'll be using.  Any table which
219 // does not exist should be created.  Note that we don't need a
220 // critical section here, because there aren't any active threads
221 // manipulating the database yet.
222 void bdb_open_databases(void) {
223         int ret;
224         int i;
225         char dbfilename[32];
226         u_int32_t flags = 0;
227         int dbversion_major, dbversion_minor, dbversion_patch;
228
229         syslog(LOG_DEBUG, "bdb: bdb_open_databases() starting");
230         syslog(LOG_DEBUG, "bdb:    Linked zlib: %s", zlibVersion());
231         syslog(LOG_DEBUG, "bdb: Compiled libdb: %s", DB_VERSION_STRING);
232         syslog(LOG_DEBUG, "bdb:   Linked libdb: %s", db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
233
234         // Create synthetic integer version numbers and compare them.
235         // Never allow citserver to run with a libdb older then the one with which it was compiled.
236         int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
237         int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
238         if (compiled_db_version > linked_db_version) {
239                 syslog(LOG_ERR, "bdb: citserver is running with a version of libdb older than the one with which it was compiled.");
240                 syslog(LOG_ERR, "bdb: This is an invalid configuration.  citserver will now exit to prevent data loss.");
241                 exit(CTDLEXIT_DB);
242         }
243
244         syslog(LOG_DEBUG, "bdb: Setting up DB environment");
245         ret = db_env_create(&bdb_env, 0);
246         if (ret) {
247                 syslog(LOG_ERR, "bdb: db_env_create: %s", db_strerror(ret));
248                 syslog(LOG_ERR, "bdb: exit code %d", ret);
249                 exit(CTDLEXIT_DB);
250         }
251         bdb_env->set_errpfx(bdb_env, "citserver");
252         bdb_env->set_paniccall(bdb_env, bdb_dbpanic);
253         bdb_env->set_errcall(bdb_env, bdb_verbose_err);
254         bdb_env->set_msgcall(bdb_env, bdb_verbose_log);
255         bdb_env->set_verbose(bdb_env, DB_VERB_DEADLOCK, 1);
256         bdb_env->set_verbose(bdb_env, DB_VERB_RECOVERY, 1);
257
258         flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER | DB_THREAD ;
259         syslog(LOG_DEBUG, "bdb: bdb_env->open(bdb_env, %s, %d, 0)", ctdl_db_dir, flags);
260         ret = bdb_env->open(bdb_env, ctdl_db_dir, flags, 0);                            // try opening the database cleanly
261         if (ret) {
262                 syslog(LOG_ERR, "bdb: bdb_env->open: %s: %s", ctdl_db_dir, db_strerror(ret));
263                 bdb_env->close(bdb_env, 0);
264                 syslog(LOG_ERR, "bdb: exit code %d", ret);
265                 exit(CTDLEXIT_DB);
266         }
267
268         for (i = 0; i < MAXCDB; ++i) {
269                 syslog(LOG_INFO, "bdb: mounting database %02x", i);
270                 ret = db_create(&bdb_table[i], bdb_env, 0);                             // Create a database handle
271                 if (ret) {
272                         syslog(LOG_ERR, "bdb: db_create: %s", db_strerror(ret));
273                         syslog(LOG_ERR, "bdb: exit code %d", ret);
274                         exit(CTDLEXIT_DB);
275                 }
276
277                 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);                 // table names by number
278                 ret = bdb_table[i]->open(bdb_table[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0600);
279                 if (ret) {
280                         syslog(LOG_ERR, "bdb: db_open[%02x]: %s", i, db_strerror(ret));
281                         if (ret == ENOMEM) {
282                                 syslog(LOG_ERR, "bdb: You may need to tune your database; please check http://www.citadel.org for more information.");
283                         }
284                         syslog(LOG_ERR, "bdb: exit code %d", ret);
285                         exit(CTDLEXIT_DB);
286                 }
287         }
288 }
289
290
291 // 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.
292 void bdb_close_databases(void) {
293         int i;
294         int ret;
295
296         static int closing = 0;
297         while (closing == 1) {
298                 syslog(LOG_INFO, "bdb: already closing");
299         }
300         closing = 1;
301
302         syslog(LOG_INFO, "bdb: performing final checkpoint");
303         if ((ret = bdb_env->txn_checkpoint(bdb_env, 0, 0, 0))) {
304                 syslog(LOG_ERR, "bdb: txn_checkpoint: %s", db_strerror(ret));
305         }
306
307         syslog(LOG_INFO, "bdb: flushing the database logs");
308         if ((ret = bdb_env->log_flush(bdb_env, NULL))) {
309                 syslog(LOG_ERR, "bdb: log_flush: %s", db_strerror(ret));
310         }
311
312         // close the tables
313         syslog(LOG_INFO, "bdb: closing databases");
314         for (i = 0; i < MAXCDB; ++i) {
315                 syslog(LOG_INFO, "bdb: closing database %02x", i);
316                 ret = bdb_table[i]->close(bdb_table[i], 0);
317                 if (ret) {
318                         syslog(LOG_ERR, "bdb: db_close: %s", db_strerror(ret));
319                 }
320         }
321
322         // Close the handle.
323         syslog(LOG_INFO, "bdb: closing environment");
324         ret = bdb_env->close(bdb_env, DB_FORCESYNC);
325         if (ret) {
326                 syslog(LOG_ERR, "bdb: DBENV->close: %s", db_strerror(ret));
327         }
328
329         syslog(LOG_INFO, "bdb: shutdown completed");
330 }
331
332
333 // Decompress a DBT data block if it was compressed on disk.
334 void bdb_decompress_if_necessary(DBT *d) {
335         static int magic = COMPRESS_MAGIC;
336
337         if ((d == NULL) || (d->data == NULL) || (d->size < sizeof(magic)) || (memcmp(d->data, &magic, sizeof(magic)))) {
338                 return;
339         }
340
341         // At this point we know we're looking at a compressed item.
342
343         struct CtdlCompressHeader zheader;
344         char *uncompressed_data;
345         char *compressed_data;
346         uLongf destLen, sourceLen;
347         size_t cplen;
348
349         memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
350         cplen = sizeof(struct CtdlCompressHeader);
351         if (sizeof(struct CtdlCompressHeader) > d->size) {
352                 cplen = d->size;
353         }
354         memcpy(&zheader, d->data, cplen);
355
356         compressed_data = d->data;
357         compressed_data += sizeof(struct CtdlCompressHeader);
358
359         sourceLen = (uLongf) zheader.compressed_len;
360         destLen = (uLongf) zheader.uncompressed_len;
361         uncompressed_data = malloc(zheader.uncompressed_len);
362
363         if (uncompress((Bytef *) uncompressed_data,
364                        (uLongf *) &destLen, (const Bytef *) compressed_data, (uLong) sourceLen) != Z_OK) {
365                 syslog(LOG_ERR, "bdb: uncompress() error");
366                 bdb_abort();
367         }
368
369         free(d->data);
370         d->size = (size_t) destLen;
371         d->data = uncompressed_data;
372 }
373
374
375 // Store a piece of data.  Returns 0 if the operation was successful.  If a
376 // key already exists it should be overwritten.
377 int bdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) {
378
379         DBT dkey, ddata;
380         DB_TXN *tid = NULL;
381         int ret = 0;
382         struct CtdlCompressHeader zheader;
383         char *compressed_data = NULL;
384         int compressing = 0;
385         size_t buffer_len = 0;
386         uLongf destLen = 0;
387         int existing_txn = 0;                   // set to nonzero if we are already inside a transaction
388
389         memset(&dkey, 0, sizeof(DBT));
390         memset(&ddata, 0, sizeof(DBT));
391         dkey.size = ckeylen;
392         dkey.data = (void *) ckey;
393         ddata.size = cdatalen;
394         ddata.data = cdata;
395
396         // "visit" records are numerous and have big, mostly-empty string buffers in them.
397         // If we compress these we can get them down to 1% of their size most of the time.
398         if (cdb == CDB_VISIT) {
399                 compressing = 1;
400                 zheader.magic = COMPRESS_MAGIC;
401                 zheader.uncompressed_len = cdatalen;
402                 buffer_len = ((cdatalen * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader);
403                 destLen = (uLongf) buffer_len;
404                 compressed_data = malloc(buffer_len);
405                 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)), &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK) {
406                         syslog(LOG_ERR, "bdb: compress2() error");
407                         bdb_abort();
408                 }
409                 zheader.compressed_len = (size_t) destLen;
410                 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
411                 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
412                 ddata.data = compressed_data;
413         }
414
415         if (TSD->tid != NULL) {
416                 existing_txn = 1;
417         }
418
419         if (!existing_txn) {                            // If we're not already inside a transaction,
420                 bdb_begin_transaction();                // create our own for this operation.
421         }
422
423         do {
424                 ret = bdb_table[cdb]->put(bdb_table[cdb],       // db
425                                 TSD->tid,                       // transaction ID
426                                 &dkey,                  // key
427                                 &ddata,                 // data
428                                 0                               // flags
429                 );
430                 if ((ret != 0) && (ret != DB_LOCK_DEADLOCK)) {
431                         syslog(LOG_ERR, "bdb: bdb_store(%02x): error %d: %s", cdb, ret, db_strerror(ret));
432                         bdb_abort();
433                 }
434                 if (ret == DB_LOCK_DEADLOCK) {
435                         syslog(LOG_DEBUG, "bdb: bdb_store(%02x): would deadlock, trying again", cdb);
436                 }
437         } while (ret == DB_LOCK_DEADLOCK);
438
439         if (!existing_txn) {
440                 bdb_end_transaction();
441         }
442
443         if (compressing) {
444                 free(compressed_data);
445         }
446
447         return ret;
448 }
449
450
451 // Delete a piece of data.  Returns 0 if the operation was successful.
452 int bdb_delete(int cdb, void *key, int keylen) {
453         DBT dkey;
454         int ret;
455         int existing_txn = 0;                           // set to nonzero if we are already inside a transaction
456
457         memset(&dkey, 0, sizeof dkey);
458         dkey.size = keylen;
459         dkey.data = key;
460
461         if (TSD->tid != NULL) {
462                 existing_txn = 1;
463         }
464
465         if (!existing_txn) {                            // If we're not already inside a transaction,
466                 bdb_begin_transaction();                // create our own for this operation.
467         }
468
469         ret = bdb_table[cdb]->del(bdb_table[cdb], TSD->tid, &dkey, 0);
470         if (ret) {
471                 if (ret != DB_NOTFOUND) {
472                         syslog(LOG_ERR, "bdb: bdb_delete(%02x): %s", cdb, db_strerror(ret));
473                         bdb_abort();
474                 }
475         }
476
477         if (!existing_txn) {
478                 bdb_end_transaction();                  // Only end the transaction if we began it.
479         }
480
481         return ret;
482 }
483
484
485 // Fetch a piece of data.  Returns a "struct cdbdata"
486 // If the item is not found, the pointer will be NULL.
487 struct cdbdata bdb_fetch(int cdb, const void *key, int keylen) {
488
489         struct cdbdata returned_data;
490         memset(&returned_data, 0, sizeof(struct cdbdata));
491
492         if (keylen == 0) {              // key length zero is impossible
493                 return(returned_data);
494         }
495
496         DBT dkey;
497         int ret;
498
499         memset(&dkey, 0, sizeof(DBT));
500         dkey.size = keylen;
501         dkey.data = (void *) key;
502
503         TSD->dbdata[cdb].flags = DB_DBT_REALLOC;
504
505         do {
506                 ret = bdb_table[cdb]->get(bdb_table[cdb], TSD->tid, &dkey, &TSD->dbdata[cdb], 0);
507                 if ((ret != 0) && (ret != DB_NOTFOUND) && (ret != DB_LOCK_DEADLOCK)) {
508                         syslog(LOG_ERR, "bdb: bdb_fetch(%d): error %d: %s", cdb, ret, db_strerror(ret));
509                         bdb_abort();
510                 }
511         } while (ret == DB_LOCK_DEADLOCK);
512
513         if (ret == 0) {
514                 bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
515                 returned_data.len = TSD->dbdata[cdb].size;
516                 returned_data.ptr = TSD->dbdata[cdb].data;
517         }
518
519         return(returned_data);
520 }
521
522
523 void bdb_close_cursor(int cdb) {
524         if (TSD->cursors[cdb] != NULL) {
525                 bdb_cclose(TSD->cursors[cdb]);
526         }
527
528         TSD->cursors[cdb] = NULL;
529 }
530
531
532 // Prepare for a sequential search of an entire database.
533 // (There is guaranteed to be no more than one traversal in progress per thread at any given time.)
534 void bdb_rewind(int cdb) {
535         int ret = 0;
536
537         if (TSD->cursors[cdb] != NULL) {
538                 syslog(LOG_ERR, "bdb: bdb_rewind: must close cursor on database %d before reopening", cdb);
539                 bdb_abort();
540                 // bdb_cclose(TSD->cursors[cdb]);
541         }
542
543         // Now initialize the cursor
544         ret = bdb_table[cdb]->cursor(bdb_table[cdb], TSD->tid, &TSD->cursors[cdb], 0);
545         if (ret) {
546                 syslog(LOG_ERR, "bdb: bdb_rewind: db_cursor: %s", db_strerror(ret));
547                 bdb_abort();
548         }
549 }
550
551
552 // Fetch the next item in a sequential search.  Returns a pointer to a 
553 // cdbdata structure, or NULL if we've hit the end.
554 struct cdbkeyval bdb_next_item(int cdb) {
555         struct cdbkeyval kv;
556         int ret = 0;
557
558         memset(&kv, 0, sizeof(struct cdbkeyval));
559
560         // reuse memory from the previous call.
561         TSD->dbkey[cdb].flags = DB_DBT_REALLOC;
562         TSD->dbdata[cdb].flags = DB_DBT_REALLOC;
563
564         assert(TSD->cursors[cdb] != NULL);
565         ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &TSD->dbkey[cdb], &TSD->dbdata[cdb], DB_NEXT);
566
567         if (ret) {
568                 if (ret != DB_NOTFOUND) {
569                         syslog(LOG_ERR, "bdb: bdb_next_item(%d): %s", cdb, db_strerror(ret));
570                         bdb_abort();
571                 }
572                 bdb_close_cursor(cdb);
573                 return(kv);             // presumably, we are at the end
574         }
575
576         bdb_decompress_if_necessary(&TSD->dbdata[cdb]);
577
578         kv.key.len = TSD->dbkey[cdb].size;
579         kv.key.ptr = TSD->dbkey[cdb].data;
580         kv.val.len = TSD->dbdata[cdb].size;
581         kv.val.ptr = TSD->dbdata[cdb].data;
582         return (kv);
583 }
584
585
586 // Truncate (delete every record)
587 void bdb_trunc(int cdb) {
588         int ret;
589         u_int32_t count;
590
591         if (TSD->tid != NULL) {
592                 syslog(LOG_ERR, "bdb: bdb_trunc must not be called in a transaction.");
593                 bdb_abort();
594         }
595
596         bdb_begin_transaction();                                // create our own transaction for this operation.
597         ret = bdb_table[cdb]->truncate(bdb_table[cdb],          // db
598                                       NULL,                     // transaction ID
599                                       &count,                   // #rows deleted
600                                       0);                       // flags
601                                                                 //
602         if (ret) {
603                 syslog(LOG_ERR, "bdb: bdb_truncate(%d): %s", cdb, db_strerror(ret));
604                 if (ret == ENOMEM) {
605                         syslog(LOG_ERR, "bdb: You may need to tune your database; please read http://www.citadel.org for more information.");
606                 }
607                 exit(CTDLEXIT_DB);
608         }
609         bdb_end_transaction();
610 }
611
612
613 // compact (defragment) the database, possibly returning space back to the underlying filesystem
614 void bdb_compact(void) {
615         int ret;
616         int i;
617
618         syslog(LOG_DEBUG, "bdb: bdb_compact() started");
619         for (i = 0; i < MAXCDB; i++) {
620                 syslog(LOG_DEBUG, "bdb: compacting database %d", i);
621                 ret = bdb_table[i]->compact(bdb_table[i], NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
622                 if (ret) {
623                         syslog(LOG_ERR, "bdb: compact: %s", db_strerror(ret));
624                 }
625         }
626         syslog(LOG_DEBUG, "bdb: bdb_compact() finished");
627 }
628
629
630 // periodically called for maintenance
631 void bdb_tick(void) {
632         int ret;
633         int rejected;
634
635         ret = bdb_env->lock_detect(bdb_env, 0, DB_LOCK_DEFAULT, &rejected);
636         if (ret) {
637                 syslog(LOG_ERR, "bdb: lock_detect: %s", db_strerror(ret));
638         }
639         else if (rejected) {
640                 syslog(LOG_DEBUG, "bdb: rejected lock %d", rejected);
641         }
642 }
643
644 // Calling this function activates the Berkeley DB back end.
645 void bdb_init_backend(void) {
646
647         // Assign the backend API stubs to the functions in this module.
648         cdb_compact = bdb_compact;
649         cdb_checkpoint = bdb_checkpoint;
650         cdb_rewind = bdb_rewind;
651         cdb_fetch = bdb_fetch;
652         cdb_open_databases = bdb_open_databases;
653         cdb_close_databases = bdb_close_databases;
654         cdb_store = bdb_store;
655         cdb_delete = bdb_delete;
656         cdb_next_item = bdb_next_item;
657         cdb_close_cursor = bdb_close_cursor;
658         cdb_begin_transaction = bdb_begin_transaction;
659         cdb_end_transaction = bdb_end_transaction;
660         cdb_check_handles = bdb_check_handles;
661         cdb_trunc = bdb_trunc;
662         cdb_tick = bdb_tick;
663
664         // Some functions in this backend need to store some per-thread data.
665         // We crerate the key here, during module initialization.
666         if (pthread_key_create(&bdb_thread_key, NULL) != 0) {
667                 syslog(LOG_ERR, "pthread_key_create() : %m");
668                 exit(CTDLEXIT_THREAD);
669         }
670
671         syslog(LOG_INFO, "db: initialized Berkeley DB backend");
672 }