2 * $Id: dbif.c,v 1.2 2005-04-28 20:49:48 bfernhomberg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 #endif /* HAVE_CONFIG_H */
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif /* HAVE_SYS_TYPES_H */
19 #include <sys/types.h>
21 #include <sys/cdefs.h>
23 #include <atalk/logger.h>
28 #define DB_ERRLOGFILE "db_errlog"
31 static DB_ENV *db_env = NULL;
32 static DB_TXN *db_txn = NULL;
33 static FILE *db_errlog = NULL;
35 #ifdef CNID_BACKEND_DBD_TXN
36 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
38 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_PRIVATE)
41 static struct db_table {
44 u_int32_t general_flags;
48 { "cnid2.db", NULL, 0, DB_BTREE},
49 { "devino.db", NULL, 0, DB_BTREE},
50 { "didname.db", NULL, 0, DB_BTREE},
53 static char *old_dbfiles[] = {"cnid.db", NULL};
55 extern int didname(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
56 extern int devino(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
59 static int db_compat_associate (DB *p, DB *s,
60 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
63 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
64 return p->associate(p, db_txn, s, callback, flags);
66 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
67 return p->associate(p, s, callback, flags);
75 static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
79 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
80 ret = db->open(db, db_txn, file, name, type, DB_CREATE, mode);
82 ret = db->open(db, file, name, type, DB_CREATE, mode);
86 LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
94 static int upgrade_required()
100 for (i = 0; old_dbfiles[i] != NULL; i++) {
101 if ( !(stat(old_dbfiles[i], &st) < 0) ) {
105 if (errno != ENOENT) {
106 LOG(log_error, logtype_cnid, "cnid_open: Checking %s gave %s", old_dbfiles[i], strerror(errno));
113 /* --------------- */
114 int dbif_stamp(void *buffer, int size)
122 if ((rc = stat(db_table[0].name, &st)) < 0) {
123 LOG(log_error, logtype_cnid, "error stating database %s: %s", db_table[0].name, db_strerror(rc));
126 memset(buffer, 0, size);
127 memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
132 /* --------------- */
134 * We assume our current directory is already the BDB homedir. Otherwise
135 * opening the databases will not work as expected. If we use transactions,
136 * dbif_env_init(), dbif_close() and dbif_stamp() are the only interface
137 * functions that can be called without a valid transaction handle in db_txn.
139 int dbif_env_init(struct db_param *dbp)
142 #ifdef CNID_BACKEND_DBD_TXN
143 char **logfiles = NULL;
147 /* Refuse to do anything if this is an old version of the CNID database */
148 if (upgrade_required()) {
149 LOG(log_error, logtype_cnid, "Found version 1 of the CNID database. Please upgrade to version 2");
153 if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
154 LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
156 #ifdef CNID_BACKEND_DBD_TXN
157 if ((ret = db_env_create(&db_env, 0))) {
158 LOG(log_error, logtype_cnid, "error creating DB environment: %s",
163 if (db_errlog != NULL)
164 db_env->set_errfile(db_env, db_errlog);
165 db_env->set_verbose(db_env, DB_VERB_RECOVERY, 1);
166 db_env->set_verbose(db_env, DB_VERB_CHKPOINT, 1);
167 if ((ret = db_env->open(db_env, ".", DBOPTIONS | DB_PRIVATE | DB_RECOVER, 0))) {
168 LOG(log_error, logtype_cnid, "error opening DB environment: %s",
170 db_env->close(db_env, 0);
175 if (db_errlog != NULL)
178 if ((ret = db_env->close(db_env, 0))) {
179 LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s",
185 if ((ret = db_env_create(&db_env, 0))) {
186 LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
191 if ((ret = db_env->set_cachesize(db_env, 0, 1024 * dbp->cachesize, 0))) {
192 LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
193 dbp->cachesize, db_strerror(ret));
194 db_env->close(db_env, 0);
199 if (db_errlog != NULL)
200 db_env->set_errfile(db_env, db_errlog);
201 if ((ret = db_env->open(db_env, ".", DBOPTIONS , 0))) {
202 LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
204 db_env->close(db_env, 0);
209 #ifdef CNID_BACKEND_DBD_TXN
210 if (dbp->nosync && (ret = db_env->set_flags(db_env, DB_TXN_NOSYNC, 1))) {
211 LOG(log_error, logtype_cnid, "error setting TXN_NOSYNC flag: %s",
213 db_env->close(db_env, 0);
217 if (dbp->logfile_autoremove && db_env->log_archive(db_env, &logfiles, 0)) {
218 LOG(log_error, logtype_cnid, "error getting list of stale logfiles: %s",
220 db_env->close(db_env, 0);
224 if (logfiles != NULL) {
225 for (file = logfiles; *file != NULL; file++) {
226 if (unlink(*file) < 0)
227 LOG(log_warning, logtype_cnid, "Error removing stale logfile %s: %s", *file, strerror(errno));
235 /* --------------- */
236 int dbif_open(struct db_param *dbp _U_, int do_truncate)
242 for (i = 0; i != DBIF_DB_CNT; i++) {
243 if ((ret = db_create(&(db_table[i].db), db_env, 0))) {
244 LOG(log_error, logtype_cnid, "error creating handle for database %s: %s",
245 db_table[i].name, db_strerror(ret));
249 if (db_table[i].general_flags) {
250 if ((ret = db_table[i].db->set_flags(db_table[i].db, db_table[i].general_flags))) {
251 LOG(log_error, logtype_cnid, "error setting flags for database %s: %s",
252 db_table[i].name, db_strerror(ret));
258 #ifndef CNID_BACKEND_DBD_TXN
259 if ((ret = db_table[i].db->set_cachesize(db_table[i].db, 0, 1024 * dbp->cachesize, 0))) {
260 LOG(log_error, logtype_cnid, "error setting DB cachesize to %i for database %s: %s",
261 dbp->cachesize, db_table[i].name, db_strerror(ret));
264 #endif /* CNID_BACKEND_DBD_TXN */
266 if (db_compat_open(db_table[i].db, db_table[0].name, db_table[i].name, db_table[i].type, 0664) < 0)
268 if (db_errlog != NULL)
269 db_table[i].db->set_errfile(db_table[i].db, db_errlog);
271 if (do_truncate && i > 0) {
272 if ((ret = db_table[i].db->truncate(db_table[i].db, db_txn, &count, 0))) {
273 LOG(log_error, logtype_cnid, "error truncating database %s: %s",
274 db_table[i].name, db_strerror(ret));
280 /* TODO: Implement CNID DB versioning info on new databases. */
281 /* TODO: Make transaction support a runtime option. */
282 /* Associate the secondary with the primary. */
283 if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DIDNAME].db, didname, (do_truncate)?DB_CREATE:0)) != 0) {
284 LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
288 if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DEVINO].db, devino, (do_truncate)?DB_CREATE:0)) != 0) {
289 LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
296 /* ------------------------ */
303 for (i = DBIF_DB_CNT -1; i >= 0; i--) {
304 if (db_table[i].db != NULL && (ret = db_table[i].db->close(db_table[i].db, 0))) {
305 LOG(log_error, logtype_cnid, "error closing database %s: %s", db_table[i].name, db_strerror(ret));
314 /* ------------------------ */
323 if (db_env != NULL && (ret = db_env->close(db_env, 0))) {
324 LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
327 if (db_errlog != NULL && fclose(db_errlog) == EOF) {
328 LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
337 * The following three functions are wrappers for DB->get(), DB->put() and
338 * DB->del(). We define them here because we want access to the db_txn
339 * transaction handle and the database handles limited to the functions in this
340 * file. A consequence is that there is always only one transaction in
341 * progress. For nontransactional access db_txn is NULL. All three return -1 on
342 * error. dbif_get()/dbif_del return 1 if the key was found and 0
343 * otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
344 * the DB_NOOVERWRITE flag was specified and the key already exists.
346 * All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
347 * functions are not expected and therefore error conditions.
350 int dbif_get(const int dbi, DBT *key, DBT *val, u_int32_t flags)
353 DB *db = db_table[dbi].db;
355 ret = db->get(db, db_txn, key, val, flags);
357 if (ret == DB_NOTFOUND)
360 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
366 /* search by secondary return primary */
367 int dbif_pget(const int dbi, DBT *key, DBT *pkey, DBT *val, u_int32_t flags)
370 DB *db = db_table[dbi].db;
372 ret = db->pget(db, db_txn, key, pkey, val, flags);
374 #if DB_VERSION_MAJOR >= 4
375 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD) {
377 if (ret == DB_NOTFOUND) {
382 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
388 /* -------------------------- */
389 int dbif_put(const int dbi, DBT *key, DBT *val, u_int32_t flags)
392 DB *db = db_table[dbi].db;
394 ret = db->put(db, db_txn, key, val, flags);
397 if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
400 LOG(log_error, logtype_cnid, "error setting key/value in %s: %s", db_table[dbi].name, db_strerror(errno));
407 int dbif_del(const int dbi, DBT *key, u_int32_t flags)
410 DB *db = db_table[dbi].db;
412 ret = db->del(db, db_txn, key, flags);
414 #if DB_VERSION_MAJOR > 3
415 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
417 if (ret == DB_NOTFOUND)
421 LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s", db_table[dbi].name, db_strerror(errno));
427 #ifdef CNID_BACKEND_DBD_TXN
432 #if DB_VERSION_MAJOR >= 4
433 ret = db_env->txn_begin(db_env, NULL, &db_txn, 0);
435 ret = txn_begin(db_env, NULL, &db_txn, 0);
438 LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(errno));
444 int dbif_txn_commit()
447 #if DB_VERSION_MAJOR >= 4
448 ret = db_txn->commit(db_txn, 0);
450 ret = txn_commit(db_txn, 0);
453 LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(errno));
462 #if DB_VERSION_MAJOR >= 4
463 ret = db_txn->abort(db_txn);
465 ret = txn_abort(db_txn);
468 LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(errno));
474 int dbif_txn_checkpoint(u_int32_t kbyte, u_int32_t min, u_int32_t flags)
477 #if DB_VERSION_MAJOR >= 4
478 ret = db_env->txn_checkpoint(db_env, kbyte, min, flags);
480 ret = txn_checkpoint(db_env, kbyte, min, flags);
483 LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(errno));
497 for (i = 0; i != /* DBIF_DB_CNT*/ 1; i++) {
498 if ((ret = db_table[i].db->sync(db_table[i].db, 0))) {
499 LOG(log_error, logtype_cnid, "error syncing database %s: %s", db_table[i].name, db_strerror(ret));
511 int dbif_count(const int dbi, u_int32_t *count)
515 DB *db = db_table[dbi].db;
517 ret = db->stat(db, &sp, 0);
520 LOG(log_error, logtype_cnid, "error getting stat infotmation on database: %s", db_strerror(errno));
524 *count = sp->bt_ndata;
529 #endif /* CNID_BACKEND_DBD_TXN */