2 * $Id: cnid_open.c,v 1.26 2001-12-13 02:39:37 jmarcus 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 * so, CNID_START begins at 3.
38 #endif /* HAVE_CONFIG_H */
46 #endif /* HAVE_UNISTD_H */
49 #endif /* HAVE_FCNTL_H */
50 #include <sys/param.h>
56 #include <atalk/adouble.h>
57 #include <atalk/cnid.h>
58 #include <atalk/util.h>
60 #include "cnid_private.h"
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
66 #define DBHOME ".AppleDB"
67 #define DBCNID "cnid.db"
68 #define DBDEVINO "devino.db"
69 #define DBDIDNAME "didname.db" /* did/full name mapping */
70 #define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
71 #define DBMACNAME "macname.db" /* did/31 mapping */
72 #define DBLONGNAME "longname.db" /* did/unicode mapping */
73 #define DBLOCKFILE "cnid.lock"
74 #define DBRECOVERFILE "cnid.dbrecover"
79 /* we version the did/name database so that we can change the format
80 * if necessary. the key is in the form of a did/name pair. in this case,
82 #define DBVERSION_KEY "\0\0\0\0\0"
83 #define DBVERSION_KEYLEN 5
84 #define DBVERSION1 0x00000001U
85 #define DBVERSION DBVERSION1
87 #if DB_VERSION_MINOR > 1
88 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
89 DB_INIT_LOG | DB_INIT_TXN)
90 #else /* DB_VERSION_MINOR < 1 */
91 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
92 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
93 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
94 DB_INIT_LOG | DB_INIT_TXN)*/
95 #endif /* DB_VERSION_MINOR */
97 /* Let's try and use the random deadlock decider if available. This adds
98 * a bit of entropy to the mix that might be beneficial. If random isn't
99 * available, we'll decide deadlocks by kicking off the youngest process.
100 * If we can't do that, then let DB3 use its default deadlock detector. */
101 #ifdef DB_LOCK_RANDOM
102 #define DEAD_LOCK_DETECT DB_LOCK_RANDOM
103 #elif defined DB_LOCK_YOUNGEST
104 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
105 #else /* DB_LOCK_RANDOM */
106 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
107 #endif /* DB_LOCK_RANDOM */
109 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
112 /* the first compare that's always done. */
113 static __inline__ int compare_did(const DBT *a, const DBT *b)
115 u_int32_t dida, didb;
117 memcpy(&dida, a->data, sizeof(dida));
118 memcpy(&didb, b->data, sizeof(didb));
122 /* sort did's and then names. this is for unix paths.
123 * i.e., did/unixname lookups. */
124 #if DB_VERSION_MINOR > 1
125 static int compare_unix(DB *db, const DBT *a, const DBT *b)
126 #else /* DB_VERSION_MINOR < 1 */
127 static int compare_unix(const DBT *a, const DBT *b)
128 #endif /* DB_VERSION_MINOR */
134 if ((ret = compare_did(a, b)))
137 sa = (u_int8_t *) a->data + 4; /* shift past did */
138 sb = (u_int8_t *) b->data + 4;
139 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
140 if ((ret = (*sa - *sb)))
141 return ret; /* sort by lexical ordering */
143 return a->size - b->size; /* sort by length */
146 /* sort did's and then names. this is for macified paths (i.e.,
147 * did/macname, and did/shortname. i think did/longname needs a
148 * unicode table to work. also, we can't use strdiacasecmp as that
149 * returns a match if a < b. */
150 #if DB_VERSION_MINOR > 1
151 static int compare_mac(DB *db, const DBT *a, const DBT *b)
152 #else /* DB_VERSION_MINOR < 1 */
153 static int compare_mac(const DBT *a, const DBT *b)
154 #endif /* DB_VERSION_MINOR */
160 if ((ret = compare_did(a, b)))
163 sa = (u_int8_t *) a->data + 4;
164 sb = (u_int8_t *) b->data + 4;
165 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
166 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
167 return ret; /* sort by lexical ordering */
169 return a->size - b->size; /* sort by length */
173 /* for unicode names -- right now it's the same as compare_mac. */
174 #if DB_VERSION_MINOR > 1
175 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
176 #else /* DB_VERSION_MINOR < 1 */
177 static int compare_unicode(const DBT *a, const DBT *b)
178 #endif /* DB_VERSION_MINOR */
180 #if DB_VERSION_MINOR > 1
181 return compare_mac(db,a,b);
182 #else /* DB_VERSION_MINOR < 1 */
183 return compare_mac(a,b);
184 #endif /* DB_VERSION_MINOR */
187 static int have_lock = 0;
189 void *cnid_open(const char *dir) {
192 char path[MAXPATHLEN + 1];
193 char recover_file[MAXPATHLEN + 1];
197 u_int32_t DBEXTRAS = 0;
205 /* this checks .AppleDB */
206 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
207 syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
211 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
212 syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
216 db->magic = CNID_DB_MAGIC;
219 if (path[len - 1] != '/') {
224 lock.l_type = F_WRLCK;
225 lock.l_whence = SEEK_SET;
227 strcpy(path + len, DBHOME);
228 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
229 syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
233 /* Make sure cnid.lock goes in .AppleDB. */
237 strcpy(recover_file, path);
238 strcat(recover_file, DBRECOVERFILE);
240 /* Search for a byte lock. This allows us to cleanup the log files
241 * at cnid_close() in a clean fashion.
243 * NOTE: This won't work if multiple volumes for the same user refer
244 * to the sahe directory. */
245 strcat(path, DBLOCKFILE);
246 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
249 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
250 if (++lock.l_start > MAXITER) {
251 syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
259 syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
262 /* Create a file to represent database recovery. While this file
263 * exists, the database is being recovered, and all other clients will
264 * sleep until recovery is complete, and this file goes away. */
265 if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
266 if (stat(recover_file, &rsb) == 0) {
267 (void)remove(recover_file);
269 if ((rfd = open(recover_file, O_RDWR | O_CREAT, 0666)) > -1) {
270 DBEXTRAS |= DB_RECOVER;
274 else if (!have_lock) {
275 while (stat(recover_file, &rsb) == 0) {
280 path[len + DBHOMELEN] = '\0';
281 open_flag = DB_CREATE;
283 /* We need to be able to open the database environment with full
284 * transaction, logging, and locking support if we ever hope to
285 * be a true multi-acess file server. */
286 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
287 syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
291 /* Setup internal deadlock detection. */
292 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
293 syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
297 #if DB_VERSION_MINOR > 1
298 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
299 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
300 syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
303 #endif /* DB_VERSION_MINOR > 1 */
305 /* Open the database environment. */
306 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
307 if (rc == DB_RUNRECOVERY) {
308 /* This is the mother of all errors. We _must_ fail here. */
309 syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
313 /* We can't get a full transactional environment, so multi-access
314 * is out of the question. Let's assume a read-only environment,
315 * and try to at least get a shared memory pool. */
316 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
317 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
318 * open the environment with no flags. */
319 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
320 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
321 path, db_strerror(rc));
325 db->flags |= CNIDFLAG_DB_RO;
326 open_flag = DB_RDONLY;
327 syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
330 /* If we have the recovery lock, close the file, remove it, so other
331 * clients can proceed opening the DB environment. */
333 (void)remove(recover_file);
339 syslog(LOG_ERR, "cnid_open: Unable to remove %s: %s",
340 recover_file, strerror(errno));
346 /* did/name reverse mapping. We use a BTree for this one. */
347 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
348 syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
353 db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
354 if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
355 DB_BTREE, open_flag, 0666))) {
356 syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
361 /* Check for version. This way we can update the database if we need
362 * to change the format in any way. */
363 memset(&key, 0, sizeof(key));
364 memset(&data, 0, sizeof(data));
365 key.data = DBVERSION_KEY;
366 key.size = DBVERSION_KEYLEN;
369 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
370 syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
372 db->db_didname->close(db->db_didname, 0);
376 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
379 case DB_LOCK_DEADLOCK:
380 if ((ret = txn_abort(tid)) != 0) {
381 syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
382 db->db_didname->close(db->db_didname, 0);
385 goto dbversion_retry;
388 u_int32_t version = htonl(DBVERSION);
390 data.data = &version;
391 data.size = sizeof(version);
394 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
396 if (ret == DB_LOCK_DEADLOCK) {
397 if ((ret = txn_abort(tid)) != 0) {
398 syslog(LOG_ERR, "cnid_open: txn_abort: %s",
400 db->db_didname->close(db->db_didname, 0);
403 goto dbversion_retry;
405 else if (ret == DB_RUNRECOVERY) {
406 /* At this point, we don't care if the transaction aborts
407 * successfully or not. */
409 syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
411 db->db_didname->close(db->db_didname, 0);
415 break; /* while loop */
418 syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
420 db->db_didname->close(db->db_didname, 0);
425 if ((rc = txn_commit(tid, 0)) != 0) {
426 syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
428 db->db_didname->close(db->db_didname, 0);
432 /* TODO In the future we might check for version number here. */
434 memcpy(&version, data.data, sizeof(version));
435 if (version != ntohl(DBVERSION)) {
441 /* did/macname (31 character) mapping. Use a BTree for this one. */
442 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
443 syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
445 db->db_didname->close(db->db_didname, 0);
449 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
450 if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
451 syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
453 db->db_didname->close(db->db_didname, 0);
457 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
458 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
459 syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
461 db->db_didname->close(db->db_didname, 0);
462 db->db_macname->close(db->db_macname, 0);
466 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
467 if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
468 syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
470 db->db_didname->close(db->db_didname, 0);
471 db->db_macname->close(db->db_macname, 0);
475 /* did/longname (Unicode) mapping. Use a BTree for this one. */
476 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
477 syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
479 db->db_didname->close(db->db_didname, 0);
480 db->db_macname->close(db->db_macname, 0);
481 db->db_shortname->close(db->db_shortname, 0);
485 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
486 if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
487 syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
489 db->db_didname->close(db->db_didname, 0);
490 db->db_macname->close(db->db_macname, 0);
491 db->db_shortname->close(db->db_shortname, 0);
494 #endif /* EXTENDED_DB */
496 /* dev/ino reverse mapping. Use a hash for this one. */
497 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
498 syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
500 db->db_didname->close(db->db_didname, 0);
502 db->db_macname->close(db->db_macname, 0);
503 db->db_shortname->close(db->db_shortname, 0);
504 db->db_longname->close(db->db_longname, 0);
505 #endif /* EXTENDED_DB */
509 if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
510 syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
512 db->db_didname->close(db->db_didname, 0);
514 db->db_macname->close(db->db_macname, 0);
515 db->db_shortname->close(db->db_shortname, 0);
516 db->db_longname->close(db->db_longname, 0);
517 #endif /* EXTENDED_DB */
521 /* Main CNID database. Use a hash for this one. */
522 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
523 syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
525 db->db_didname->close(db->db_didname, 0);
527 db->db_macname->close(db->db_macname, 0);
528 db->db_shortname->close(db->db_shortname, 0);
529 db->db_longname->close(db->db_longname, 0);
530 #endif /* EXTENDED_DB */
531 db->db_devino->close(db->db_devino, 0);
536 if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
537 syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
539 db->db_didname->close(db->db_didname, 0);
541 db->db_macname->close(db->db_macname, 0);
542 db->db_shortname->close(db->db_shortname, 0);
543 db->db_longname->close(db->db_longname, 0);
544 #endif /* EXTENDED_DB */
551 syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
552 db->dbenv->close(db->dbenv, 0);
555 if (db->lockfd > -1) {