2 * $Id: dbif.c,v 1.1.4.15 2004-04-29 18:09:16 lenneis Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 #endif /* HAVE_CONFIG_H */
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif /* HAVE_SYS_TYPES_H */
18 #include <sys/types.h>
20 #include <sys/cdefs.h>
22 #include <atalk/logger.h>
27 #define DB_ERRLOGFILE "db_errlog"
30 static DB_ENV *db_env = NULL;
31 static DB_TXN *db_txn = NULL;
32 static FILE *db_errlog = NULL;
34 #ifdef CNID_BACKEND_DBD_TXN
35 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
37 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_PRIVATE)
40 static struct db_table {
43 u_int32_t general_flags;
47 { "cnid2.db", NULL, 0, DB_BTREE},
48 { "devino.db", NULL, 0, DB_BTREE},
49 { "didname.db", NULL, 0, DB_BTREE},
52 static char *old_dbfiles[] = {"cnid.db", NULL};
54 extern int didname(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
55 extern int devino(DB *dbp, const DBT *pkey, const DBT *pdata, DBT *skey);
58 static int db_compat_associate (DB *p, DB *s,
59 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
62 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
63 return p->associate(p, db_txn, s, callback, flags);
65 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
66 return p->associate(p, s, callback, flags);
74 static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
78 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
79 ret = db->open(db, db_txn, file, name, type, DB_CREATE, mode);
81 ret = db->open(db, file, name, type, DB_CREATE, mode);
85 LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
93 static int upgrade_required()
99 for (i = 0; old_dbfiles[i] != NULL; i++) {
100 if ( !(stat(old_dbfiles[i], &st) < 0) ) {
104 if (errno != ENOENT) {
105 LOG(log_error, logtype_cnid, "cnid_open: Checking %s gave %s", old_dbfiles[i], strerror(errno));
112 /* --------------- */
113 int dbif_stamp(void *buffer, int size)
121 if ((rc = stat(db_table[0].name, &st)) < 0) {
122 LOG(log_error, logtype_cnid, "error stating database %s: %s", db_table[0].name, db_strerror(rc));
125 memset(buffer, 0, size);
126 memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
131 /* --------------- */
133 * We assume our current directory is already the BDB homedir. Otherwise
134 * opening the databases will not work as expected. If we use transactions,
135 * dbif_env_init(), dbif_close() and dbif_stamp() are the only interface
136 * functions that can be called without a valid transaction handle in db_txn.
138 int dbif_env_init(struct db_param *dbp)
141 #ifdef CNID_BACKEND_DBD_TXN
142 char **logfiles = NULL;
146 /* Refuse to do anything if this is an old version of the CNID database */
147 if (upgrade_required()) {
148 LOG(log_error, logtype_cnid, "Found version 1 of the CNID database. Please upgrade to version 2");
152 if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
153 LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
155 #ifdef CNID_BACKEND_DBD_TXN
156 if ((ret = db_env_create(&db_env, 0))) {
157 LOG(log_error, logtype_cnid, "error creating DB environment: %s",
162 if (db_errlog != NULL)
163 db_env->set_errfile(db_env, db_errlog);
164 db_env->set_verbose(db_env, DB_VERB_RECOVERY, 1);
165 db_env->set_verbose(db_env, DB_VERB_CHKPOINT, 1);
166 if ((ret = db_env->open(db_env, ".", DBOPTIONS | DB_PRIVATE | DB_RECOVER, 0))) {
167 LOG(log_error, logtype_cnid, "error opening DB environment: %s",
169 db_env->close(db_env, 0);
174 if (db_errlog != NULL)
177 if ((ret = db_env->close(db_env, 0))) {
178 LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s",
184 if ((ret = db_env_create(&db_env, 0))) {
185 LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
190 if ((ret = db_env->set_cachesize(db_env, 0, 1024 * dbp->cachesize, 0))) {
191 LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
192 dbp->cachesize, db_strerror(ret));
193 db_env->close(db_env, 0);
198 if (db_errlog != NULL)
199 db_env->set_errfile(db_env, db_errlog);
200 if ((ret = db_env->open(db_env, ".", DBOPTIONS , 0))) {
201 LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
203 db_env->close(db_env, 0);
208 #ifdef CNID_BACKEND_DBD_TXN
209 if (dbp->nosync && (ret = db_env->set_flags(db_env, DB_TXN_NOSYNC, 1))) {
210 LOG(log_error, logtype_cnid, "error setting TXN_NOSYNC flag: %s",
212 db_env->close(db_env, 0);
216 if (dbp->logfile_autoremove && db_env->log_archive(db_env, &logfiles, 0)) {
217 LOG(log_error, logtype_cnid, "error getting list of stale logfiles: %s",
219 db_env->close(db_env, 0);
223 if (logfiles != NULL) {
224 for (file = logfiles; *file != NULL; file++) {
225 if (unlink(*file) < 0)
226 LOG(log_warning, logtype_cnid, "Error removing stale logfile %s: %s", *file, strerror(errno));
234 /* --------------- */
235 int dbif_open(struct db_param *dbp)
240 for (i = 0; i != DBIF_DB_CNT; i++) {
241 if ((ret = db_create(&(db_table[i].db), db_env, 0))) {
242 LOG(log_error, logtype_cnid, "error creating handle for database %s: %s",
243 db_table[i].name, db_strerror(ret));
246 if (db_table[i].general_flags) {
247 if ((ret = db_table[i].db->set_flags(db_table[i].db, db_table[i].general_flags))) {
248 LOG(log_error, logtype_cnid, "error setting flags for database %s: %s",
249 db_table[i].name, db_strerror(ret));
254 #ifndef CNID_BACKEND_DBD_TXN
255 if ((ret = db_table[i].db->set_cachesize(db_table[i].db, 0, 1024 * dbp->cachesize, 0))) {
256 LOG(log_error, logtype_cnid, "error setting DB cachesize to %i for database %s: %s",
257 dbp->cachesize, db_table[i].name, db_strerror(ret));
260 #endif /* CNID_BACKEND_DBD_TXN */
262 if (db_compat_open(db_table[i].db, db_table[0].name, db_table[i].name, db_table[i].type, 0664) < 0)
264 if (db_errlog != NULL)
265 db_table[i].db->set_errfile(db_table[i].db, db_errlog);
268 /* TODO: Implement CNID DB versioning info on new databases. */
269 /* TODO: Make transaction support a runtime option. */
270 /* Associate the secondary with the primary. */
271 if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DIDNAME].db, didname, 0)) != 0) {
272 LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
276 if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DEVINO].db, devino, 0)) != 0) {
277 LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
284 /* ------------------------ */
291 for (i = DBIF_DB_CNT -1; i >= 0; i--) {
292 if (db_table[i].db != NULL && (ret = db_table[i].db->close(db_table[i].db, 0))) {
293 LOG(log_error, logtype_cnid, "error closing database %s: %s", db_table[i].name, db_strerror(ret));
297 if (db_env != NULL && (ret = db_env->close(db_env, 0))) {
298 LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
301 if (db_errlog != NULL && fclose(db_errlog) == EOF) {
302 LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
312 * The following three functions are wrappers for DB->get(), DB->put() and
313 * DB->del(). We define them here because we want access to the db_txn
314 * transaction handle and the database handles limited to the functions in this
315 * file. A consequence is that there is always only one transaction in
316 * progress. For nontransactional access db_txn is NULL. All three return -1 on
317 * error. dbif_get()/dbif_del return 1 if the key was found and 0
318 * otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
319 * the DB_NOOVERWRITE flag was specified and the key already exists.
321 * All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
322 * functions are not expected and therefore error conditions.
325 int dbif_get(const int dbi, DBT *key, DBT *val, u_int32_t flags)
328 DB *db = db_table[dbi].db;
330 ret = db->get(db, db_txn, key, val, flags);
332 if (ret == DB_NOTFOUND)
335 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
341 /* search by secondary return primary */
342 int dbif_pget(const int dbi, DBT *key, DBT *pkey, DBT *val, u_int32_t flags)
345 DB *db = db_table[dbi].db;
347 ret = db->pget(db, db_txn, key, pkey, val, flags);
349 #if DB_VERSION_MAJOR >= 4
350 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD) {
352 if (ret == DB_NOTFOUND) {
357 LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
363 /* -------------------------- */
364 int dbif_put(const int dbi, DBT *key, DBT *val, u_int32_t flags)
367 DB *db = db_table[dbi].db;
369 ret = db->put(db, db_txn, key, val, flags);
372 if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
375 LOG(log_error, logtype_cnid, "error setting key/value in %s: %s", db_table[dbi].name, db_strerror(errno));
382 int dbif_del(const int dbi, DBT *key, u_int32_t flags)
385 DB *db = db_table[dbi].db;
387 ret = db->del(db, db_txn, key, flags);
389 #if DB_VERSION_MAJOR > 3
390 if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
392 if (ret == DB_NOTFOUND)
396 LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s", db_table[dbi].name, db_strerror(errno));
402 #ifdef CNID_BACKEND_DBD_TXN
407 #if DB_VERSION_MAJOR >= 4
408 ret = db_env->txn_begin(db_env, NULL, &db_txn, 0);
410 ret = txn_begin(db_env, NULL, &db_txn, 0);
413 LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(errno));
419 int dbif_txn_commit()
422 #if DB_VERSION_MAJOR >= 4
423 ret = db_txn->commit(db_txn, 0);
425 ret = txn_commit(db_txn, 0);
428 LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(errno));
437 #if DB_VERSION_MAJOR >= 4
438 ret = db_txn->abort(db_txn);
440 ret = txn_abort(db_txn);
443 LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(errno));
449 int dbif_txn_checkpoint(u_int32_t kbyte, u_int32_t min, u_int32_t flags)
452 #if DB_VERSION_MAJOR >= 4
453 ret = db_env->txn_checkpoint(db_env, kbyte, min, flags);
455 ret = txn_checkpoint(db_env, kbyte, min, flags);
458 LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(errno));
472 for (i = 0; i != /* DBIF_DB_CNT*/ 1; i++) {
473 if ((ret = db_table[i].db->sync(db_table[i].db, 0))) {
474 LOG(log_error, logtype_cnid, "error syncing database %s: %s", db_table[i].name, db_strerror(ret));
485 #endif /* CNID_BACKEND_DBD_TXN */