3 * $Id: cnid_db3_open.c,v 1.5 2009-11-24 12:18:19 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
119 /* -----------------------
120 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
122 static int my_yield(void)
129 ret = select(0, NULL, NULL, NULL, &t);
134 /* --------------- */
135 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
137 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
138 return p->open(p, NULL, f, d, t, flags | DB_AUTO_COMMIT, mode);
140 return p->open(p, f, d, t, flags, mode);
149 /* the first compare that's always done. */
150 static int compare_did(const DBT * a, const DBT * b)
152 u_int32_t dida, didb;
154 memcpy(&dida, a->data, sizeof(dida));
155 memcpy(&didb, b->data, sizeof(didb));
159 /* sort did's and then names. this is for unix paths.
160 * i.e., did/unixname lookups. */
161 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
162 static int compare_unix(DB * db _U_, const DBT * a, const DBT * b)
163 #else /* DB_VERSION_MINOR < 1 */
164 static int compare_unix(const DBT * a, const DBT * b)
165 #endif /* DB_VERSION_MINOR */
171 if ((ret = compare_did(a, b)))
174 sa = (u_int8_t *) a->data + 4; /* shift past did */
175 sb = (u_int8_t *) b->data + 4;
176 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
177 if ((ret = (*sa - *sb)))
178 return ret; /* sort by lexical ordering */
180 return a->size - b->size; /* sort by length */
183 /* sort did's and then names. this is for macified paths (i.e.,
184 * did/macname, and did/shortname. i think did/longname needs a
185 * unicode table to work. also, we can't use strdiacasecmp as that
186 * returns a match if a < b. */
187 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
188 static int compare_mac(DB * db _U_, const DBT * a, const DBT * b)
189 #else /* DB_VERSION_MINOR < 1 */
190 static int compare_mac(const DBT * a, const DBT * b)
191 #endif /* DB_VERSION_MINOR */
197 if ((ret = compare_did(a, b)))
200 sa = (u_int8_t *) a->data + 4;
201 sb = (u_int8_t *) b->data + 4;
202 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
203 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
204 return ret; /* sort by lexical ordering */
206 return a->size - b->size; /* sort by length */
209 /* for unicode names -- right now it's the same as compare_mac. */
210 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
211 static int compare_unicode(DB * db, const DBT * a, const DBT * b)
212 #else /* DB_VERSION_MINOR < 1 */
213 static int compare_unicode(const DBT * a, const DBT * b)
214 #endif /* DB_VERSION_MINOR */
216 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
217 return compare_mac(db, a, b);
218 #else /* DB_VERSION_MINOR < 1 */
219 return compare_mac(a, b);
220 #endif /* DB_VERSION_MINOR */
224 static struct _cnid_db *cnid_db3_new(const char *volpath)
226 struct _cnid_db *cdb;
228 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
231 if ((cdb->volpath = strdup(volpath)) == NULL) {
236 cdb->flags = CNID_FLAG_PERSISTENT;
238 cdb->cnid_add = cnid_db3_add;
239 cdb->cnid_delete = cnid_db3_delete;
240 cdb->cnid_get = cnid_db3_get;
241 cdb->cnid_lookup = cnid_db3_lookup;
242 cdb->cnid_nextid = NULL; /*cnid_db3_nextid;*/
243 cdb->cnid_resolve = cnid_db3_resolve;
244 cdb->cnid_update = cnid_db3_update;
245 cdb->cnid_close = cnid_db3_close;
250 struct _cnid_db *cnid_db3_open(const char *dir, mode_t mask, u_int32_t flags _U_)
254 char path[MAXPATHLEN + 1];
256 struct _cnid_db *cdb;
266 /* this checks .AppleDB */
267 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
268 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
272 if ((cdb = cnid_db3_new(dir)) == NULL) {
273 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
277 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
278 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
282 cdb->_private = (void *) db;
283 db->magic = CNID_DB_MAGIC;
286 if (path[len - 1] != '/') {
291 strcpy(path + len, DBHOME);
292 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
293 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
297 lock.l_type = F_WRLCK;
298 lock.l_whence = SEEK_SET;
299 /* Make sure cnid.lock goes in .AppleDB. */
303 /* Search for a byte lock. This allows us to cleanup the log files
304 * at cnid_close() in a clean fashion.
306 * NOTE: This won't work if multiple volumes for the same user refer
307 * to the sahe directory. */
308 strcat(path, DBLOCKFILE);
309 strcpy(db->lock_file, path);
310 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
313 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
314 if (++lock.l_start > MAXITER) {
315 LOG(log_error, logtype_default,
316 "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
323 LOG(log_error, logtype_default,
324 "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
327 path[len + DBHOMELEN] = '\0';
328 open_flag = DB_CREATE;
330 /* Print out the version of BDB we're linked against. */
331 LOG(log_info, logtype_default, "CNID DB initializing using %s", db_version(NULL, NULL, NULL));
333 /* We need to be able to open the database environment with full
334 * transaction, logging, and locking support if we ever hope to
335 * be a true multi-acess file server. */
336 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
337 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
341 /* Setup internal deadlock detection. */
342 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
343 LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
347 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
349 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
350 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
351 LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
355 #endif /* DB_VERSION_MINOR > 1 */
357 /* Open the database environment. */
358 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
359 if (rc == DB_RUNRECOVERY) {
360 /* This is the mother of all errors. We _must_ fail here. */
361 LOG(log_error, logtype_default,
362 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
366 /* We can't get a full transactional environment, so multi-access
367 * is out of the question. Let's assume a read-only environment,
368 * and try to at least get a shared memory pool. */
369 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
370 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
371 * open the environment with no flags. */
372 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
373 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
377 db->flags |= CNIDFLAG_DB_RO;
378 open_flag = DB_RDONLY;
379 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
381 /* did/name reverse mapping. We use a BTree for this one. */
382 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
383 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s", db_strerror(rc));
387 /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix); */
388 if ((rc = my_open(db->db_didname, DBDIDNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
389 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s", db_strerror(rc));
393 /* Check for version. This way we can update the database if we need
394 * to change the format in any way. */
395 memset(&key, 0, sizeof(key));
396 memset(&data, 0, sizeof(data));
397 key.data = DBVERSION_KEY;
398 key.size = DBVERSION_KEYLEN;
401 if ((rc = db3_txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
402 LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s", db_strerror(rc));
406 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
410 case DB_LOCK_DEADLOCK:
411 if ((ret = db3_txn_abort(tid)) != 0) {
412 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
415 goto dbversion_retry;
418 u_int32_t version = htonl(DBVERSION);
420 data.data = &version;
421 data.size = sizeof(version);
424 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data, DB_NOOVERWRITE))) {
425 if (ret == DB_LOCK_DEADLOCK) {
426 if ((ret = db3_txn_abort(tid)) != 0) {
427 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
430 goto dbversion_retry;
431 } else if (ret == DB_RUNRECOVERY) {
432 /* At this point, we don't care if the transaction aborts
433 * successfully or not. */
435 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s", db_strerror(ret));
439 break; /* while loop */
442 LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s", db_strerror(rc));
447 if ((rc = db3_txn_commit(tid, 0)) != 0) {
448 LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s", db_strerror(rc));
452 /* TODO In the future we might check for version number here. */
454 memcpy(&version, data.data, sizeof(version));
455 if (version != ntohl(DBVERSION)) {
460 /* dev/ino reverse mapping. Use a hash for this one. */
461 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
462 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s", db_strerror(rc));
466 if ((rc = my_open(db->db_devino, DBDEVINO, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
467 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s", db_strerror(rc));
471 /* Main CNID database. Use a hash for this one. */
472 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
473 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s", db_strerror(rc));
477 if ((rc = my_open(db->db_cnid, DBCNID, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
478 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s", db_strerror(rc));
483 db_env_set_func_yield(my_yield);
489 db->db_didname->close(db->db_didname, 0);
491 db->db_devino->close(db->db_devino, 0);
493 db->db_cnid->close(db->db_cnid, 0);
494 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
495 db->dbenv->close(db->dbenv, 0);
498 if (db->lockfd > -1) {
500 (void) remove(db->lock_file);
508 if (cdb->volpath != NULL)
515 struct _cnid_module cnid_db3_module = {
522 #endif /* CNID_BACKEND_DB3 */