]> arthur.barton.de Git - netatalk.git/blob - libatalk/cnid/db3/cnid_db3_open.c
remove gcc warnings and cleanup inline mess
[netatalk.git] / libatalk / cnid / db3 / cnid_db3_open.c
1
2 /*
3  * $Id: cnid_db3_open.c,v 1.1.4.4.2.1 2008-11-25 15:16:34 didg Exp $
4  *
5  * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
6  * All Rights Reserved. See COPYRIGHT.
7  *
8  * CNID database support. 
9  *
10  * here's the deal:
11  *  1) afpd already caches did's. 
12  *  2) the database stores cnid's as both did/name and dev/ino pairs. 
13  *  3) RootInfo holds the value of the NextID.
14  *  4) the cnid database gets called in the following manner --
15  *     start a database:
16  *     cnid = cnid_open(root_dir);
17  *
18  *     allocate a new id: 
19  *     newid = cnid_add(cnid, dev, ino, parent did,
20  *     name, id); id is a hint for a specific id. pass 0 if you don't
21  *     care. if the id is already assigned, you won't get what you
22  *     requested.
23  *
24  *     given an id, get a did/name and dev/ino pair.
25  *     name = cnid_get(cnid, &id); given an id, return the corresponding
26  *     info.
27  *     return code = cnid_delete(cnid, id); delete an entry. 
28  *
29  * with AFP, CNIDs 0-2 have special meanings. here they are:
30  * 0 -- invalid cnid
31  * 1 -- parent of root directory (handled by afpd) 
32  * 2 -- root directory (handled by afpd)
33  *
34  * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so, 
35  * CNID_START begins at 17.
36  */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif /* HAVE_CONFIG_H */
41
42 #ifdef CNID_BACKEND_DB3
43
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif /* HAVE_UNISTD_H */
50 #ifdef HAVE_FCNTL_H
51 #include <fcntl.h>
52 #endif /* HAVE_FCNTL_H */
53 #include <sys/param.h>
54 #include <sys/stat.h>
55 #include <atalk/logger.h>
56 #ifdef HAVE_SYS_TIME_H
57 #include <sys/time.h>
58 #endif /* HAVE_SYS_TIME_H */
59
60 #ifdef HAVE_DB4_DB_H
61 #include <db4/db.h>
62 #else
63 #include <db.h>
64 #endif
65
66 #include <atalk/adouble.h>
67 #include <atalk/cnid.h>
68 #include "cnid_db3.h"
69 #include <atalk/util.h>
70
71 #include "cnid_db3_private.h"
72
73 #ifndef MIN
74 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
75 #endif /* ! MIN */
76
77 #define DBHOME        ".AppleDB"
78 #define DBCNID        "cnid.db"
79 #define DBDEVINO      "devino.db"
80 #define DBDIDNAME     "didname.db"      /* did/full name mapping */
81 #define DBLOCKFILE    "cnid.lock"
82 #define DBRECOVERFILE "cnid.dbrecover"
83 #define DBCLOSEFILE   "cnid.close"
84
85 #define DBHOMELEN    8
86 #define DBLEN        10
87
88 /* we version the did/name database so that we can change the format
89  * if necessary. the key is in the form of a did/name pair. in this case,
90  * we use 0/0. */
91 #define DBVERSION_KEY    "\0\0\0\0\0"
92 #define DBVERSION_KEYLEN 5
93 #define DBVERSION1       0x00000001U
94 #define DBVERSION        DBVERSION1
95
96 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
97 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98 DB_INIT_LOG | DB_INIT_TXN)
99 #else /* DB_VERSION_MINOR < 1 */
100
101 /*#define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
102 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
103 #define DBOPTIONS    (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
104 DB_INIT_LOG | DB_INIT_TXN)
105 #endif /* DB_VERSION_MINOR */
106
107 /* Let's try and use the youngest lock detector if present.
108  * If we can't do that, then let BDB use its default deadlock detector. */
109 #if defined DB_LOCK_YOUNGEST
110 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
111 #else /* DB_LOCK_YOUNGEST */
112 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
113 #endif /* DB_LOCK_YOUNGEST */
114
115 #define MAXITER     0xFFFF      /* maximum number of simultaneously open CNID
116                                  * databases. */
117
118 /* -----------------------
119  * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
120 */
121 static int my_yield(void)
122 {
123     struct timeval t;
124     int ret;
125
126     t.tv_sec = 0;
127     t.tv_usec = 1000;
128     ret = select(0, NULL, NULL, NULL, &t);
129     return 0;
130 }
131
132 /* --------------- */
133 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
134 {
135 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
136     return p->open(p, NULL, f, d, t, flags | DB_AUTO_COMMIT, mode);
137 #else
138     return p->open(p, f, d, t, flags, mode);
139 #endif
140 }
141
142 /* --------------- */
143
144 /* the first compare that's always done. */
145 static int compare_did(const DBT * a, const DBT * b)
146 {
147     u_int32_t dida, didb;
148
149     memcpy(&dida, a->data, sizeof(dida));
150     memcpy(&didb, b->data, sizeof(didb));
151     return dida - didb;
152 }
153
154 /* sort did's and then names. this is for unix paths.
155  * i.e., did/unixname lookups. */
156 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
157 static int compare_unix(DB * db, const DBT * a, const DBT * b)
158 #else /* DB_VERSION_MINOR < 1 */
159 static int compare_unix(const DBT * a, const DBT * b)
160 #endif                          /* DB_VERSION_MINOR */
161 {
162     u_int8_t *sa, *sb;
163     int len, ret;
164
165     /* sort by did */
166     if ((ret = compare_did(a, b)))
167         return ret;
168
169     sa = (u_int8_t *) a->data + 4;      /* shift past did */
170     sb = (u_int8_t *) b->data + 4;
171     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
172         if ((ret = (*sa - *sb)))
173             return ret;         /* sort by lexical ordering */
174
175     return a->size - b->size;   /* sort by length */
176 }
177
178 /* sort did's and then names. this is for macified paths (i.e.,
179  * did/macname, and did/shortname. i think did/longname needs a
180  * unicode table to work. also, we can't use strdiacasecmp as that
181  * returns a match if a < b. */
182 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
183 static int compare_mac(DB * db, const DBT * a, const DBT * b)
184 #else /* DB_VERSION_MINOR < 1 */
185 static int compare_mac(const DBT * a, const DBT * b)
186 #endif                          /* DB_VERSION_MINOR */
187 {
188     u_int8_t *sa, *sb;
189     int len, ret;
190
191     /* sort by did */
192     if ((ret = compare_did(a, b)))
193         return ret;
194
195     sa = (u_int8_t *) a->data + 4;
196     sb = (u_int8_t *) b->data + 4;
197     for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
198         if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
199             return ret;         /* sort by lexical ordering */
200
201     return a->size - b->size;   /* sort by length */
202 }
203
204
205 /* for unicode names -- right now it's the same as compare_mac. */
206 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
207 static int compare_unicode(DB * db, const DBT * a, const DBT * b)
208 #else /* DB_VERSION_MINOR < 1 */
209 static int compare_unicode(const DBT * a, const DBT * b)
210 #endif                          /* DB_VERSION_MINOR */
211 {
212 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
213     return compare_mac(db, a, b);
214 #else /* DB_VERSION_MINOR < 1 */
215     return compare_mac(a, b);
216 #endif /* DB_VERSION_MINOR */
217 }
218
219 static struct _cnid_db *cnid_db3_new(const char *volpath)
220 {
221     struct _cnid_db *cdb;
222
223     if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
224         return NULL;
225
226     if ((cdb->volpath = strdup(volpath)) == NULL) {
227         free(cdb);
228         return NULL;
229     }
230
231     cdb->flags = CNID_FLAG_PERSISTENT;
232
233     cdb->cnid_add = cnid_db3_add;
234     cdb->cnid_delete = cnid_db3_delete;
235     cdb->cnid_get = cnid_db3_get;
236     cdb->cnid_lookup = cnid_db3_lookup;
237     cdb->cnid_nextid = NULL;    /*cnid_db3_nextid;*/
238     cdb->cnid_resolve = cnid_db3_resolve;
239     cdb->cnid_update = cnid_db3_update;
240     cdb->cnid_close = cnid_db3_close;
241
242     return cdb;
243 }
244
245 struct _cnid_db *cnid_db3_open(const char *dir, mode_t mask)
246 {
247     struct stat st;
248     struct flock lock;
249     char path[MAXPATHLEN + 1];
250     CNID_private *db;
251     struct _cnid_db *cdb;
252     DBT key, data;
253     DB_TXN *tid = NULL;
254     int open_flag, len;
255     int rc;
256
257     if (!dir) {
258         return NULL;
259     }
260
261     /* this checks .AppleDB */
262     if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
263         LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
264         return NULL;
265     }
266
267     if ((cdb = cnid_db3_new(dir)) == NULL) {
268         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
269         return NULL;
270     }
271
272     if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
273         LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
274         goto fail_cdb;
275     }
276
277     cdb->_private = (void *) db;
278     db->magic = CNID_DB_MAGIC;
279
280     strcpy(path, dir);
281     if (path[len - 1] != '/') {
282         strcat(path, "/");
283         len++;
284     }
285
286     strcpy(path + len, DBHOME);
287     if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
288         LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
289         goto fail_adouble;
290     }
291
292     lock.l_type = F_WRLCK;
293     lock.l_whence = SEEK_SET;
294     /* Make sure cnid.lock goes in .AppleDB. */
295     strcat(path, "/");
296     len++;
297
298     /* Search for a byte lock.  This allows us to cleanup the log files
299      * at cnid_close() in a clean fashion.
300      *
301      * NOTE: This won't work if multiple volumes for the same user refer
302      * to the sahe directory. */
303     strcat(path, DBLOCKFILE);
304     strcpy(db->lock_file, path);
305     if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
306         lock.l_start = 0;
307         lock.l_len = 1;
308         while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
309             if (++lock.l_start > MAXITER) {
310                 LOG(log_error, logtype_default,
311                     "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
312                 close(db->lockfd);
313                 db->lockfd = -1;
314                 break;
315             }
316         }
317     } else {
318         LOG(log_error, logtype_default,
319             "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
320     }
321
322     path[len + DBHOMELEN] = '\0';
323     open_flag = DB_CREATE;
324
325     /* Print out the version of BDB we're linked against. */
326     LOG(log_info, logtype_default, "CNID DB initializing using %s", db_version(NULL, NULL, NULL));
327
328     /* We need to be able to open the database environment with full
329      * transaction, logging, and locking support if we ever hope to 
330      * be a true multi-acess file server. */
331     if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
332         LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
333         goto fail_lock;
334     }
335
336     /* Setup internal deadlock detection. */
337     if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
338         LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
339         goto fail_lock;
340     }
341
342 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
343 #if 0
344     /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
345     if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
346         LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
347         goto fail_lock;
348     }
349 #endif
350 #endif /* DB_VERSION_MINOR > 1 */
351
352     /* Open the database environment. */
353     if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
354         if (rc == DB_RUNRECOVERY) {
355             /* This is the mother of all errors.  We _must_ fail here. */
356             LOG(log_error, logtype_default,
357                 "cnid_open: CATASTROPHIC ERROR opening database environment %s.  Run db_recovery -c immediately", path);
358             goto fail_lock;
359         }
360
361         /* We can't get a full transactional environment, so multi-access
362          * is out of the question.  Let's assume a read-only environment,
363          * and try to at least get a shared memory pool. */
364         if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
365             /* Nope, not a MPOOL, either.  Last-ditch effort: we'll try to
366              * open the environment with no flags. */
367             if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
368                 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
369                 goto fail_lock;
370             }
371         }
372         db->flags |= CNIDFLAG_DB_RO;
373         open_flag = DB_RDONLY;
374         LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
375     }
376     /* did/name reverse mapping.  We use a BTree for this one. */
377     if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
378         LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s", db_strerror(rc));
379         goto fail_appinit;
380     }
381
382     /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix); */
383     if ((rc = my_open(db->db_didname, DBDIDNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
384         LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s", db_strerror(rc));
385         goto fail_appinit;
386     }
387
388     /* Check for version.  This way we can update the database if we need
389      * to change the format in any way. */
390     memset(&key, 0, sizeof(key));
391     memset(&data, 0, sizeof(data));
392     key.data = DBVERSION_KEY;
393     key.size = DBVERSION_KEYLEN;
394
395   dbversion_retry:
396     if ((rc = db3_txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
397         LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s", db_strerror(rc));
398         goto fail_appinit;
399     }
400
401     while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
402         int ret;
403
404         switch (rc) {
405         case DB_LOCK_DEADLOCK:
406             if ((ret = db3_txn_abort(tid)) != 0) {
407                 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
408                 goto fail_appinit;
409             }
410             goto dbversion_retry;
411         case DB_NOTFOUND:
412             {
413                 u_int32_t version = htonl(DBVERSION);
414
415                 data.data = &version;
416                 data.size = sizeof(version);
417             }
418
419             if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data, DB_NOOVERWRITE))) {
420                 if (ret == DB_LOCK_DEADLOCK) {
421                     if ((ret = db3_txn_abort(tid)) != 0) {
422                         LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
423                         goto fail_appinit;
424                     }
425                     goto dbversion_retry;
426                 } else if (ret == DB_RUNRECOVERY) {
427                     /* At this point, we don't care if the transaction aborts
428                      * successfully or not. */
429                     db3_txn_abort(tid);
430                     LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s", db_strerror(ret));
431                     goto fail_appinit;
432                 }
433             }
434             break;              /* while loop */
435         default:
436             db3_txn_abort(tid);
437             LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s", db_strerror(rc));
438             goto fail_appinit;
439         }
440     }
441
442     if ((rc = db3_txn_commit(tid, 0)) != 0) {
443         LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s", db_strerror(rc));
444         goto fail_appinit;
445     }
446
447     /* TODO In the future we might check for version number here. */
448 #if 0
449     memcpy(&version, data.data, sizeof(version));
450     if (version != ntohl(DBVERSION)) {
451         /* Do stuff here. */
452     }
453 #endif /* 0 */
454
455     /* dev/ino reverse mapping.  Use a hash for this one. */
456     if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
457         LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s", db_strerror(rc));
458         goto fail_appinit;
459     }
460
461     if ((rc = my_open(db->db_devino, DBDEVINO, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
462         LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s", db_strerror(rc));
463         goto fail_appinit;
464     }
465
466     /* Main CNID database.  Use a hash for this one. */
467     if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
468         LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s", db_strerror(rc));
469         goto fail_appinit;
470     }
471
472     if ((rc = my_open(db->db_cnid, DBCNID, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
473         LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s", db_strerror(rc));
474         goto fail_appinit;
475     }
476
477 #if 0
478     db_env_set_func_yield(my_yield);
479 #endif
480     return cdb;
481
482   fail_appinit:
483     if (db->db_didname)
484         db->db_didname->close(db->db_didname, 0);
485     if (db->db_devino)
486         db->db_devino->close(db->db_devino, 0);
487     if (db->db_cnid)
488         db->db_cnid->close(db->db_cnid, 0);
489     LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
490     db->dbenv->close(db->dbenv, 0);
491
492   fail_lock:
493     if (db->lockfd > -1) {
494         close(db->lockfd);
495         (void) remove(db->lock_file);
496     }
497
498   fail_adouble:
499
500   fail_db:
501     free(db);
502
503   fail_cdb:
504     if (cdb->volpath != NULL)
505         free(cdb->volpath);
506     free(cdb);
507
508     return NULL;
509 }
510
511 struct _cnid_module cnid_db3_module = {
512     "db3",
513     {NULL, NULL},
514     cnid_db3_open,
515     CNID_FLAG_BLOCK
516 };
517
518 #endif /* CNID_BACKEND_DB3 */