3 * $Id: cnid_db3_open.c,v 1.1.4.4.2.1 2008-11-25 15:16:34 didg Exp $
5 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
8 * CNID database support.
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 --
16 * cnid = cnid_open(root_dir);
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
24 * given an id, get a did/name and dev/ino pair.
25 * name = cnid_get(cnid, &id); given an id, return the corresponding
27 * return code = cnid_delete(cnid, id); delete an entry.
29 * with AFP, CNIDs 0-2 have special meanings. here they are:
31 * 1 -- parent of root directory (handled by afpd)
32 * 2 -- root directory (handled by afpd)
34 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
35 * CNID_START begins at 17.
40 #endif /* HAVE_CONFIG_H */
42 #ifdef CNID_BACKEND_DB3
49 #endif /* HAVE_UNISTD_H */
52 #endif /* HAVE_FCNTL_H */
53 #include <sys/param.h>
55 #include <atalk/logger.h>
56 #ifdef HAVE_SYS_TIME_H
58 #endif /* HAVE_SYS_TIME_H */
66 #include <atalk/adouble.h>
67 #include <atalk/cnid.h>
69 #include <atalk/util.h>
71 #include "cnid_db3_private.h"
74 #define MIN(a, b) ((a) < (b) ? (a) : (b))
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"
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,
91 #define DBVERSION_KEY "\0\0\0\0\0"
92 #define DBVERSION_KEYLEN 5
93 #define DBVERSION1 0x00000001U
94 #define DBVERSION DBVERSION1
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 */
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 */
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 */
115 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
118 /* -----------------------
119 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
121 static int my_yield(void)
128 ret = select(0, NULL, NULL, NULL, &t);
132 /* --------------- */
133 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
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);
138 return p->open(p, f, d, t, flags, mode);
142 /* --------------- */
144 /* the first compare that's always done. */
145 static int compare_did(const DBT * a, const DBT * b)
147 u_int32_t dida, didb;
149 memcpy(&dida, a->data, sizeof(dida));
150 memcpy(&didb, b->data, sizeof(didb));
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 */
166 if ((ret = compare_did(a, b)))
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 */
175 return a->size - b->size; /* sort by length */
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 */
192 if ((ret = compare_did(a, b)))
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 */
201 return a->size - b->size; /* sort by length */
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 */
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 */
219 static struct _cnid_db *cnid_db3_new(const char *volpath)
221 struct _cnid_db *cdb;
223 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
226 if ((cdb->volpath = strdup(volpath)) == NULL) {
231 cdb->flags = CNID_FLAG_PERSISTENT;
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;
245 struct _cnid_db *cnid_db3_open(const char *dir, mode_t mask)
249 char path[MAXPATHLEN + 1];
251 struct _cnid_db *cdb;
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);
267 if ((cdb = cnid_db3_new(dir)) == NULL) {
268 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
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");
277 cdb->_private = (void *) db;
278 db->magic = CNID_DB_MAGIC;
281 if (path[len - 1] != '/') {
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);
292 lock.l_type = F_WRLCK;
293 lock.l_whence = SEEK_SET;
294 /* Make sure cnid.lock goes in .AppleDB. */
298 /* Search for a byte lock. This allows us to cleanup the log files
299 * at cnid_close() in a clean fashion.
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) {
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);
318 LOG(log_error, logtype_default,
319 "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
322 path[len + DBHOMELEN] = '\0';
323 open_flag = DB_CREATE;
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));
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));
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));
342 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
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));
350 #endif /* DB_VERSION_MINOR > 1 */
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);
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));
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);
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));
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));
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;
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));
401 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
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));
410 goto dbversion_retry;
413 u_int32_t version = htonl(DBVERSION);
415 data.data = &version;
416 data.size = sizeof(version);
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));
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. */
430 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s", db_strerror(ret));
434 break; /* while loop */
437 LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s", db_strerror(rc));
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));
447 /* TODO In the future we might check for version number here. */
449 memcpy(&version, data.data, sizeof(version));
450 if (version != ntohl(DBVERSION)) {
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));
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));
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));
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));
478 db_env_set_func_yield(my_yield);
484 db->db_didname->close(db->db_didname, 0);
486 db->db_devino->close(db->db_devino, 0);
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);
493 if (db->lockfd > -1) {
495 (void) remove(db->lock_file);
504 if (cdb->volpath != NULL)
511 struct _cnid_module cnid_db3_module = {
518 #endif /* CNID_BACKEND_DB3 */