]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/dbif.c
6eff7947392863295dee0e7c05942f12fb100ed7
[netatalk.git] / etc / cnid_dbd / dbif.c
1 /*
2  * $Id: dbif.c,v 1.1.4.11 2004-01-10 06:18:43 didg Exp $
3  *
4  * Copyright (C) Joerg Lenneis 2003
5  * All Rights Reserved.  See COPYING.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif /* HAVE_CONFIG_H */
11
12 #include <stdio.h>
13 #include <errno.h>
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif /* HAVE_SYS_TYPES_H */
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/cdefs.h>
21 #include <unistd.h>
22 #include <atalk/logger.h>
23 #include <db.h>
24 #include "db_param.h"
25 #include "dbif.h"
26
27 #define DB_ERRLOGFILE "db_errlog"
28
29
30 static DB_ENV *db_env = NULL;
31 static DB_TXN *db_txn = NULL;
32 static FILE   *db_errlog = NULL;
33
34 #ifdef CNID_BACKEND_DBD_TXN
35 #define DBOPTIONS    (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
36 #else
37 #define DBOPTIONS    (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL) 
38 #endif
39
40 static struct db_table {
41      char            *name;
42      DB              *db;
43      u_int32_t       general_flags;
44      DBTYPE          type;
45 } db_table[] =
46 {
47      { "cnid2.db",       NULL,      0, DB_HASH},
48      { "devino.db",      NULL,      0, DB_HASH},
49      { "didname.db",     NULL,      0, DB_HASH},
50 };
51
52 static char *old_dbfiles[] = {"cnid.db", NULL};
53
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);
56
57 /* --------------- */
58 static int  db_compat_associate (DB *p, DB *s,
59                    int (*callback)(DB *, const DBT *,const DBT *, DBT *),
60                    u_int32_t flags)
61 {
62 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
63     return p->associate(p, db_txn, s, callback, flags);
64 #else
65 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
66     return p->associate(p,       s, callback, flags);
67 #else
68     return 0;
69 #endif
70 #endif
71 }
72
73 /* --------------- */
74 static int db_compat_open(DB *db, char *file, char *name, DBTYPE type, int mode)
75 {
76     int ret;
77
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); 
80 #else
81     ret = db->open(db,       file, name, type, DB_CREATE, mode); 
82 #endif
83
84     if (ret) {
85         LOG(log_error, logtype_cnid, "error opening database %s: %s", name, db_strerror(ret));
86         return -1;
87     } else {
88         return 0;
89     }
90 }
91
92 /* --------------- */
93 static int upgrade_required()
94 {
95     int i;
96     int found = 0;
97     struct stat st;
98     
99     for (i = 0; old_dbfiles[i] != NULL; i++) {
100         if ( !(stat(old_dbfiles[i], &st) < 0) ) {
101             found++;
102             continue;
103         }
104         if (errno != ENOENT) {
105             LOG(log_error, logtype_cnid, "cnid_open: Checking %s gave %s", old_dbfiles[i], strerror(errno));
106             found++;
107         }
108     }
109     return found;
110 }
111
112 /* --------------- */
113 int dbif_stamp(void *buffer, int size)
114 {
115     struct stat st;
116     int         rc;
117
118     if (size < 8)
119         return -1;
120
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));
123         return -1;
124     }
125     memset(buffer, 0, size);
126     memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
127
128     return 0;
129 }
130
131 /* --------------- */
132 /*
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.
137  */
138 int dbif_env_init(struct db_param *dbp)
139 {
140     int ret;
141
142     /* Refuse to do anything if this is an old version of the CNID database */
143     if (upgrade_required()) {
144         LOG(log_error, logtype_cnid, "Found version 1 of the CNID database. Please upgrade to version 2");
145         return -1;
146     }
147
148     if ((db_errlog = fopen(DB_ERRLOGFILE, "a")) == NULL)
149         LOG(log_warning, logtype_cnid, "error creating/opening DB errlogfile: %s", strerror(errno));
150
151 #ifdef CNID_BACKEND_DBD_TXN
152     if ((ret = db_env_create(&db_env, 0))) {
153         LOG(log_error, logtype_cnid, "error creating DB environment: %s", 
154             db_strerror(ret));
155         db_env = NULL;
156         return -1;
157     }    
158     if (db_errlog != NULL)
159         db_env->set_errfile(db_env, db_errlog); 
160     db_env->set_verbose(db_env, DB_VERB_RECOVERY, 1);
161     db_env->set_verbose(db_env, DB_VERB_CHKPOINT, 1);
162     if (ret = db_env->open(db_env, ".", DBOPTIONS | DB_PRIVATE | DB_RECOVER, 0)) {
163         LOG(log_error, logtype_cnid, "error opening DB environment: %s", 
164             db_strerror(ret));
165         db_env->close(db_env, 0);
166         db_env = NULL;
167         return -1;
168     }
169
170     if (db_errlog != NULL)
171         fflush(db_errlog);
172
173     if (ret = db_env->close(db_env, 0)) {
174         LOG(log_error, logtype_cnid, "error closing DB environment after recovery: %s", 
175             db_strerror(ret));
176         db_env = NULL;
177         return -1;
178     }
179 #endif
180     if ((ret = db_env_create(&db_env, 0))) {
181         LOG(log_error, logtype_cnid, "error creating DB environment after recovery: %s",
182             db_strerror(ret));
183         db_env = NULL;
184         return -1;
185     }
186     if ((ret = db_env->set_cachesize(db_env, 0, 1024 * dbp->cachesize, 0))) {
187         LOG(log_error, logtype_cnid, "error setting DB environment cachesize to %i: %s",
188             dbp->cachesize, db_strerror(ret));
189         db_env->close(db_env, 0);
190         db_env = NULL;
191         return -1;
192     }
193     
194     if (db_errlog != NULL)
195         db_env->set_errfile(db_env, db_errlog);
196     if ((ret = db_env->open(db_env, ".", DBOPTIONS , 0))) {
197         LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
198             db_strerror(ret));
199         db_env->close(db_env, 0);
200         db_env = NULL;
201         return -1;      
202     }
203
204 #ifdef CNID_BACKEND_DBD_TXN
205     if (dbp->nosync && (ret = db_env->set_flags(db_env, DB_TXN_NOSYNC, 1))) {
206         LOG(log_error, logtype_cnid, "error setting TXN_NOSYNC flag: %s",
207             db_strerror(ret));
208         db_env->close(db_env, 0);
209         db_env = NULL;
210         return -1;      
211     }
212 #endif
213     return 0;
214 }
215
216 /* --------------- */
217 int dbif_open(struct db_param *dbp)
218 {
219     int ret;
220     int i;
221
222     for (i = 0; i != DBIF_DB_CNT; i++) {
223         if ((ret = db_create(&(db_table[i].db), db_env, 0))) {
224             LOG(log_error, logtype_cnid, "error creating handle for database %s: %s", 
225                 db_table[i].name, db_strerror(ret));
226             return -1;
227         }
228         if (db_table[i].general_flags) { 
229             if ((ret = db_table[i].db->set_flags(db_table[i].db, db_table[i].general_flags))) {
230                 LOG(log_error, logtype_cnid, "error setting flags for database %s: %s", 
231                     db_table[i].name, db_strerror(ret));
232                 return -1;
233             }
234         }
235 #if 0
236 #ifndef CNID_BACKEND_DBD_TXN
237         if ((ret = db_table[i].db->set_cachesize(db_table[i].db, 0, 1024 * dbp->cachesize, 0))) {
238             LOG(log_error, logtype_cnid, "error setting DB cachesize to %i for database %s: %s",
239                 dbp->cachesize, db_table[i].name, db_strerror(ret));
240             return -1;
241         }
242 #endif /* CNID_BACKEND_DBD_TXN */
243 #endif
244         if (db_compat_open(db_table[i].db, db_table[0].name, db_table[i].name, db_table[i].type, 0664) < 0)
245             return -1;
246         if (db_errlog != NULL)
247             db_table[i].db->set_errfile(db_table[i].db, db_errlog);
248     }
249     
250     /* TODO: Implement CNID DB versioning info on new databases. */
251     /* TODO: Make transaction support a runtime option. */
252     /* Associate the secondary with the primary. */
253     if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DIDNAME].db, didname, 0)) != 0) {
254         LOG(log_error, logtype_cnid, "Failed to associate didname database: %s",db_strerror(ret));
255         return -1;
256     }
257  
258     if ((ret = db_compat_associate(db_table[0].db, db_table[DBIF_IDX_DEVINO].db, devino, 0)) != 0) {
259         LOG(log_error, logtype_cnid, "Failed to associate devino database: %s",db_strerror(ret));
260         return -1;
261     }
262
263     return 0;
264 }
265
266 /* ------------------------ */
267 int dbif_close()
268 {
269     int i;
270     int ret;
271     int err = 0;
272      
273     for (i = DBIF_DB_CNT -1; i >= 0; i--) {
274         if (db_table[i].db != NULL && (ret = db_table[i].db->close(db_table[i].db, 0))) {
275             LOG(log_error, logtype_cnid, "error closing database %s: %s", db_table[i].name, db_strerror(ret));
276             err++;
277         }
278     }
279     if (db_env != NULL && (ret = db_env->close(db_env, 0))) { 
280         LOG(log_error, logtype_cnid, "error closing DB environment: %s", db_strerror(ret));
281         err++;
282     }
283     if (db_errlog != NULL && fclose(db_errlog) == EOF) {
284         LOG(log_error, logtype_cnid, "error closing DB logfile: %s", strerror(errno));
285         err++;
286     }
287     if (err)
288         return -1;
289     else
290         return 0;
291 }
292
293 /*
294  *  The following three functions are wrappers for DB->get(), DB->put() and
295  *  DB->del(). We define them here because we want access to the db_txn
296  *  transaction handle and the database handles limited to the functions in this
297  *  file. A consequence is that there is always only one transaction in
298  *  progress. For nontransactional access db_txn is NULL. All three return -1 on
299  *  error. dbif_get()/dbif_del return 1 if the key was found and 0
300  *  otherwise. dbif_put() returns 0 if key/val was successfully updated and 1 if
301  *  the DB_NOOVERWRITE flag was specified and the key already exists.
302  *  
303  *  All return codes other than DB_NOTFOUND and DB_KEYEXIST from the DB->()
304  *  functions are not expected and therefore error conditions.
305  */
306
307 int dbif_get(const int dbi, DBT *key, DBT *val, u_int32_t flags)
308 {
309     int ret;
310     DB *db = db_table[dbi].db;
311
312     ret = db->get(db, db_txn, key, val, flags);
313      
314     if (ret == DB_NOTFOUND)
315         return 0;
316     if (ret) {
317         LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
318         return -1;
319     } else 
320         return 1;
321 }
322
323 /* search by secondary return primary */
324 int dbif_pget(const int dbi, DBT *key, DBT *pkey, DBT *val, u_int32_t flags)
325 {
326     int ret;
327     DB *db = db_table[dbi].db;
328
329     ret = db->pget(db, db_txn, key, pkey, val, flags);
330
331 #if DB_VERSION_MAJOR >= 4
332     if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD) {
333 #else
334     if (ret == DB_NOTFOUND) {
335 #endif     
336         return 0;
337     }
338     if (ret) {
339         LOG(log_error, logtype_cnid, "error retrieving value from %s: %s", db_table[dbi].name, db_strerror(errno));
340         return -1;
341     } else 
342         return 1;
343 }
344
345 /* -------------------------- */
346 int dbif_put(const int dbi, DBT *key, DBT *val, u_int32_t flags)
347 {
348     int ret;
349     DB *db = db_table[dbi].db;
350
351     ret = db->put(db, db_txn, key, val, flags);
352      
353     if (ret) {
354         if ((flags & DB_NOOVERWRITE) && ret == DB_KEYEXIST) {
355             return 1;
356         } else {
357             LOG(log_error, logtype_cnid, "error setting key/value in %s: %s", db_table[dbi].name, db_strerror(errno));
358             return -1;
359         }
360     } else
361         return 0;
362 }
363
364 int dbif_del(const int dbi, DBT *key, u_int32_t flags)
365 {
366     int ret;
367     DB *db = db_table[dbi].db;
368
369     ret = db->del(db, db_txn, key, flags);
370
371 #if DB_VERSION_MAJOR > 3
372     if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
373 #else
374     if (ret == DB_NOTFOUND)
375 #endif
376         return 0;
377     if (ret) {
378         LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s", db_table[dbi].name, db_strerror(errno));
379         return -1;
380     } else
381         return 1;
382 }
383
384 #ifdef CNID_BACKEND_DBD_TXN
385
386 int dbif_txn_begin()
387 {
388     int ret;
389 #if DB_VERSION_MAJOR >= 4
390     ret = db_env->txn_begin(db_env, NULL, &db_txn, 0);
391 #else     
392     ret = txn_begin(db_env, NULL, &db_txn, 0);
393 #endif
394     if (ret) {
395         LOG(log_error, logtype_cnid, "error starting transaction: %s", db_strerror(errno));
396         return -1;
397     } else 
398         return 0;
399 }
400
401 int dbif_txn_commit()
402 {
403     int ret;
404 #if DB_VERSION_MAJOR >= 4
405     ret = db_txn->commit(db_txn, 0);
406 #else
407     ret = txn_commit(db_txn, 0);
408 #endif
409     if (ret) {
410         LOG(log_error, logtype_cnid, "error committing transaction: %s", db_strerror(errno));
411         return -1;
412     } else 
413         return 0;
414 }
415
416 int dbif_txn_abort()
417 {
418     int ret;
419 #if DB_VERSION_MAJOR >= 4
420     ret = db_txn->abort(db_txn);
421 #else
422     ret = txn_abort(db_txn);
423 #endif
424     if (ret) {
425         LOG(log_error, logtype_cnid, "error aborting transaction: %s", db_strerror(errno));
426         return -1;
427     } else
428         return 0;
429 }
430
431 int dbif_txn_checkpoint(u_int32_t kbyte, u_int32_t min, u_int32_t flags)
432 {
433     int ret;
434 #if DB_VERSION_MAJOR >= 4
435     ret = db_env->txn_checkpoint(db_env, kbyte, min, flags);
436 #else 
437     ret = txn_checkpoint(db_env, kbyte, min, flags);
438 #endif
439     if (ret) {
440         LOG(log_error, logtype_cnid, "error checkpointing transaction susystem: %s", db_strerror(errno));
441         return -1;
442     } else 
443         return 0;
444 }
445
446 #else
447
448 int dbif_sync()
449 {
450     int i;
451     int ret;
452     int err = 0;
453      
454     for (i = 0; i != /* DBIF_DB_CNT*/ 1; i++) {
455         if ((ret = db_table[i].db->sync(db_table[i].db, 0))) {
456             LOG(log_error, logtype_cnid, "error syncing database %s: %s", db_table[i].name, db_strerror(ret));
457             err++;
458         }
459     }
460  
461     if (err)
462         return -1;
463     else
464         return 0;
465 }
466
467 #endif /* CNID_BACKEND_DBD_TXN */
468