2 * $Id: cnid_open.c,v 1.52 2003-06-06 20:25:32 srittau Exp $
4 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
7 * CNID database support.
10 * 1) afpd already caches did's.
11 * 2) the database stores cnid's as both did/name and dev/ino pairs.
12 * 3) RootInfo holds the value of the NextID.
13 * 4) the cnid database gets called in the following manner --
15 * cnid = cnid_open(root_dir);
18 * newid = cnid_add(cnid, dev, ino, parent did,
19 * name, id); id is a hint for a specific id. pass 0 if you don't
20 * care. if the id is already assigned, you won't get what you
23 * given an id, get a did/name and dev/ino pair.
24 * name = cnid_get(cnid, &id); given an id, return the corresponding
26 * return code = cnid_delete(cnid, id); delete an entry.
28 * with AFP, CNIDs 0-2 have special meanings. here they are:
30 * 1 -- parent of root directory (handled by afpd)
31 * 2 -- root directory (handled by afpd)
33 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
34 * CNID_START begins at 17.
39 #endif /* HAVE_CONFIG_H */
47 #endif /* HAVE_UNISTD_H */
50 #endif /* HAVE_FCNTL_H */
51 #include <sys/param.h>
53 #include <atalk/logger.h>
54 #ifdef HAVE_SYS_TIME_H
56 #endif /* HAVE_SYS_TIME_H */
60 #include <atalk/adouble.h>
61 #include <atalk/cnid.h>
62 #include <atalk/util.h>
64 #include "cnid_private.h"
67 #define MIN(a, b) ((a) < (b) ? (a) : (b))
70 #define DBHOME ".AppleDB"
71 #define DBCNID "cnid.db"
72 #define DBDEVINO "devino.db"
73 #define DBDIDNAME "didname.db" /* did/full name mapping */
74 #define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
75 #define DBMACNAME "macname.db" /* did/31 mapping */
76 #define DBMANGLE "mangle.db" /* filename mangling */
77 #define DBLONGNAME "longname.db" /* did/unicode mapping */
78 #define DBLOCKFILE "cnid.lock"
79 #define DBRECOVERFILE "cnid.dbrecover"
80 #define DBCLOSEFILE "cnid.close"
85 /* we version the did/name database so that we can change the format
86 * if necessary. the key is in the form of a did/name pair. in this case,
88 #define DBVERSION_KEY "\0\0\0\0\0"
89 #define DBVERSION_KEYLEN 5
90 #define DBVERSION1 0x00000001U
91 #define DBVERSION DBVERSION1
94 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
95 #else /* !CNID_DB_CDB */
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 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
101 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
102 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
103 DB_INIT_LOG | DB_INIT_TXN)
104 #endif /* DB_VERSION_MINOR */
105 #endif /* CNID_DB_CDB */
108 /* Let's try and use the youngest lock detector if present.
109 * If we can't do that, then let BDB use its default deadlock detector. */
110 #if defined DB_LOCK_YOUNGEST
111 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
112 #else /* DB_LOCK_YOUNGEST */
113 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
114 #endif /* DB_LOCK_YOUNGEST */
115 #endif /* CNID_DB_CDB */
117 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
121 /* -----------------------
122 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
124 static int my_yield(void)
131 ret = select(0, NULL, NULL, NULL, &t);
136 /* --------------- */
137 static int my_open(DB *p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
139 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
140 return p->open(p, NULL, f, d, t, flags, mode);
142 return p->open(p, f, d, t, flags, mode);
146 /* --------------- */
148 /* the first compare that's always done. */
149 static __inline__ int compare_did(const DBT *a, const DBT *b)
151 u_int32_t dida, didb;
153 memcpy(&dida, a->data, sizeof(dida));
154 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, 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 */
184 /* sort did's and then names. this is for macified paths (i.e.,
185 * did/macname, and did/shortname. i think did/longname needs a
186 * unicode table to work. also, we can't use strdiacasecmp as that
187 * returns a match if a < b. */
188 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
189 static int compare_mac(DB *db, const DBT *a, const DBT *b)
190 #else /* DB_VERSION_MINOR < 1 */
191 static int compare_mac(const DBT *a, const DBT *b)
192 #endif /* DB_VERSION_MINOR */
198 if ((ret = compare_did(a, b)))
201 sa = (u_int8_t *) a->data + 4;
202 sb = (u_int8_t *) b->data + 4;
203 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
204 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
205 return ret; /* sort by lexical ordering */
207 return a->size - b->size; /* sort by length */
211 /* for unicode names -- right now it's the same as compare_mac. */
212 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
213 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
214 #else /* DB_VERSION_MINOR < 1 */
215 static int compare_unicode(const DBT *a, const DBT *b)
216 #endif /* DB_VERSION_MINOR */
218 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
219 return compare_mac(db,a,b);
220 #else /* DB_VERSION_MINOR < 1 */
221 return compare_mac(a,b);
222 #endif /* DB_VERSION_MINOR */
224 #endif /* EXTENDED_DB */
226 void *cnid_open(const char *dir, mode_t mask) {
230 #endif /* CNID_DB_CDB */
231 char path[MAXPATHLEN + 1];
241 /* this checks .AppleDB */
242 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
243 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
247 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
248 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
252 db->magic = CNID_DB_MAGIC;
255 if (path[len - 1] != '/') {
260 strcpy(path + len, DBHOME);
261 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
262 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
267 lock.l_type = F_WRLCK;
268 lock.l_whence = SEEK_SET;
269 /* Make sure cnid.lock goes in .AppleDB. */
273 /* Search for a byte lock. This allows us to cleanup the log files
274 * at cnid_close() in a clean fashion.
276 * NOTE: This won't work if multiple volumes for the same user refer
277 * to the sahe directory. */
278 strcat(path, DBLOCKFILE);
279 strcpy(db->lock_file, path);
280 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
283 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
284 if (++lock.l_start > MAXITER) {
285 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
293 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
295 #endif /* CNID_DB_CDB */
297 path[len + DBHOMELEN] = '\0';
298 open_flag = DB_CREATE;
300 /* We need to be able to open the database environment with full
301 * transaction, logging, and locking support if we ever hope to
302 * be a true multi-acess file server. */
303 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
304 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
309 /* Setup internal deadlock detection. */
310 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
311 LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
314 #endif /* CNID_DB_CDB */
317 #if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
319 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
320 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
321 LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
325 #endif /* DB_VERSION_MINOR > 1 */
326 #endif /* CNID_DB_CDB */
328 /* Open the database environment. */
329 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
330 if (rc == DB_RUNRECOVERY) {
331 /* This is the mother of all errors. We _must_ fail here. */
332 LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
336 /* We can't get a full transactional environment, so multi-access
337 * is out of the question. Let's assume a read-only environment,
338 * and try to at least get a shared memory pool. */
339 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
340 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
341 * open the environment with no flags. */
342 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
343 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
344 path, db_strerror(rc));
348 db->flags |= CNIDFLAG_DB_RO;
349 open_flag = DB_RDONLY;
350 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
353 /* did/name reverse mapping. We use a BTree for this one. */
354 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
355 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
360 /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
361 if ((rc = my_open(db->db_didname, DBDIDNAME, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
362 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
367 /* Check for version. This way we can update the database if we need
368 * to change the format in any way. */
369 memset(&key, 0, sizeof(key));
370 memset(&data, 0, sizeof(data));
371 key.data = DBVERSION_KEY;
372 key.size = DBVERSION_KEYLEN;
375 if ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) != 0) {
378 u_int32_t version = htonl(DBVERSION);
380 data.data = &version;
381 data.size = sizeof(version);
383 if ((ret = db->db_didname->put(db->db_didname, NULL, &key, &data,
385 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
390 #else /* CNID_DB_CDB */
392 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
393 LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
398 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
401 case DB_LOCK_DEADLOCK:
402 if ((ret = txn_abort(tid)) != 0) {
403 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
406 goto dbversion_retry;
409 u_int32_t version = htonl(DBVERSION);
411 data.data = &version;
412 data.size = sizeof(version);
415 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
417 if (ret == DB_LOCK_DEADLOCK) {
418 if ((ret = txn_abort(tid)) != 0) {
419 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
423 goto dbversion_retry;
425 else if (ret == DB_RUNRECOVERY) {
426 /* At this point, we don't care if the transaction aborts
427 * successfully or not. */
429 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
434 break; /* while loop */
437 LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
443 if ((rc = txn_commit(tid, 0)) != 0) {
444 LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
448 #endif /* CNID_DB_CDB */
450 /* TODO In the future we might check for version number here. */
452 memcpy(&version, data.data, sizeof(version));
453 if (version != ntohl(DBVERSION)) {
459 /* did/macname (31 character) mapping. Use a BTree for this one. */
460 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
461 LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
466 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
467 if ((rc = my_open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
468 LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
473 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
474 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
475 LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
480 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
481 if ((rc = my_open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
482 LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
487 /* did/longname (Unicode) mapping. Use a BTree for this one. */
488 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
489 LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
494 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
495 if ((rc = my_open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
496 LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
500 #endif /* EXTENDED_DB */
502 /* dev/ino reverse mapping. Use a hash for this one. */
503 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
504 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
509 if ((rc = my_open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
510 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
515 /* Main CNID database. Use a hash for this one. */
516 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
517 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
522 if ((rc = my_open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
523 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
529 /* filename mangling database. Use a hash for this one. */
530 if ((rc = db_create(&db->db_mangle, db->dbenv, 0)) != 0) {
531 LOG(log_error, logtype_default, "cnid_open: Failed to create mangle database: %s", db_strerror(rc));
535 if ((rc = my_open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
536 LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
539 #endif /* FILE_MANGLING */
541 /* Print out the version of BDB we're linked against. */
542 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
544 db_env_set_func_yield(my_yield);
549 if (db->db_didname) db->db_didname->close(db->db_didname, 0);
550 if (db->db_devino) db->db_devino->close(db->db_devino, 0);
551 if (db->db_cnid) db->db_cnid->close(db->db_cnid, 0);
553 if (db->db_macname) db->db_macname->close(db->db_macname, 0);
554 if (db->db_shortname) db->db_shortname->close(db->db_shortname, 0);
555 if (db->db_longname) db->db_longname->close(db->db_longname, 0);
556 #endif /* EXTENDED_DB */
557 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
558 db->dbenv->close(db->dbenv, 0);
562 if (db->lockfd > -1) {
564 (void)remove(db->lock_file);
566 #endif /* CNID_DB_CDB */