2 * $Id: cnid_open.c,v 1.28 2001-12-14 03:10: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"
75 #define DBCLOSEFILE "cnid.close"
80 /* we version the did/name database so that we can change the format
81 * if necessary. the key is in the form of a did/name pair. in this case,
83 #define DBVERSION_KEY "\0\0\0\0\0"
84 #define DBVERSION_KEYLEN 5
85 #define DBVERSION1 0x00000001U
86 #define DBVERSION DBVERSION1
88 #if DB_VERSION_MINOR > 1
89 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
90 DB_INIT_LOG | DB_INIT_TXN)
91 #else /* DB_VERSION_MINOR < 1 */
92 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
93 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
94 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
95 DB_INIT_LOG | DB_INIT_TXN)*/
96 #endif /* DB_VERSION_MINOR */
98 /* Let's try and use the random deadlock decider if available. This adds
99 * a bit of entropy to the mix that might be beneficial. If random isn't
100 * available, we'll decide deadlocks by kicking off the youngest process.
101 * If we can't do that, then let DB3 use its default deadlock detector. */
102 #ifdef DB_LOCK_RANDOM
103 #define DEAD_LOCK_DETECT DB_LOCK_RANDOM
104 #elif defined DB_LOCK_YOUNGEST
105 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
106 #else /* DB_LOCK_RANDOM */
107 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
108 #endif /* DB_LOCK_RANDOM */
110 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
113 /* the first compare that's always done. */
114 static __inline__ int compare_did(const DBT *a, const DBT *b)
116 u_int32_t dida, didb;
118 memcpy(&dida, a->data, sizeof(dida));
119 memcpy(&didb, b->data, sizeof(didb));
123 /* sort did's and then names. this is for unix paths.
124 * i.e., did/unixname lookups. */
125 #if DB_VERSION_MINOR > 1
126 static int compare_unix(DB *db, const DBT *a, const DBT *b)
127 #else /* DB_VERSION_MINOR < 1 */
128 static int compare_unix(const DBT *a, const DBT *b)
129 #endif /* DB_VERSION_MINOR */
135 if ((ret = compare_did(a, b)))
138 sa = (u_int8_t *) a->data + 4; /* shift past did */
139 sb = (u_int8_t *) b->data + 4;
140 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
141 if ((ret = (*sa - *sb)))
142 return ret; /* sort by lexical ordering */
144 return a->size - b->size; /* sort by length */
147 /* sort did's and then names. this is for macified paths (i.e.,
148 * did/macname, and did/shortname. i think did/longname needs a
149 * unicode table to work. also, we can't use strdiacasecmp as that
150 * returns a match if a < b. */
151 #if DB_VERSION_MINOR > 1
152 static int compare_mac(DB *db, const DBT *a, const DBT *b)
153 #else /* DB_VERSION_MINOR < 1 */
154 static int compare_mac(const DBT *a, const DBT *b)
155 #endif /* DB_VERSION_MINOR */
161 if ((ret = compare_did(a, b)))
164 sa = (u_int8_t *) a->data + 4;
165 sb = (u_int8_t *) b->data + 4;
166 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
167 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
168 return ret; /* sort by lexical ordering */
170 return a->size - b->size; /* sort by length */
174 /* for unicode names -- right now it's the same as compare_mac. */
175 #if DB_VERSION_MINOR > 1
176 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
177 #else /* DB_VERSION_MINOR < 1 */
178 static int compare_unicode(const DBT *a, const DBT *b)
179 #endif /* DB_VERSION_MINOR */
181 #if DB_VERSION_MINOR > 1
182 return compare_mac(db,a,b);
183 #else /* DB_VERSION_MINOR < 1 */
184 return compare_mac(a,b);
185 #endif /* DB_VERSION_MINOR */
188 static int have_lock = 0;
190 void *cnid_open(const char *dir) {
191 struct stat st, rsb, csb;
193 char path[MAXPATHLEN + 1];
194 char recover_file[MAXPATHLEN + 1];
198 u_int32_t DBEXTRAS = 0;
206 /* this checks .AppleDB */
207 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
208 syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
212 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
213 syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
217 db->magic = CNID_DB_MAGIC;
220 if (path[len - 1] != '/') {
225 lock.l_type = F_WRLCK;
226 lock.l_whence = SEEK_SET;
228 strcpy(path + len, DBHOME);
229 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
230 syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed for %s", path);
234 /* Make sure cnid.lock goes in .AppleDB. */
238 strcpy(db->close_file, path);
239 strcat(db->close_file, DBCLOSEFILE);
241 /* Check to make sure that a client isn't in the process of closing
242 * the database environment. To do this, sleep on the close file. */
243 while(stat(db->close_file, &csb) == 0) {
247 strcpy(recover_file, path);
248 strcat(recover_file, DBRECOVERFILE);
250 /* Search for a byte lock. This allows us to cleanup the log files
251 * at cnid_close() in a clean fashion.
253 * NOTE: This won't work if multiple volumes for the same user refer
254 * to the sahe directory. */
255 strcat(path, DBLOCKFILE);
256 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
259 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
260 if (++lock.l_start > MAXITER) {
261 syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
269 syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
272 /* Create a file to represent database recovery. While this file
273 * exists, the database is being recovered, and all other clients will
274 * sleep until recovery is complete, and this file goes away. */
275 if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
276 if (stat(recover_file, &rsb) == 0) {
277 (void)remove(recover_file);
279 if ((rfd = open(recover_file, O_RDWR | O_CREAT, 0666)) > -1) {
280 DBEXTRAS |= DB_RECOVER;
284 else if (!have_lock) {
285 while (stat(recover_file, &rsb) == 0) {
290 path[len + DBHOMELEN] = '\0';
291 open_flag = DB_CREATE;
293 /* We need to be able to open the database environment with full
294 * transaction, logging, and locking support if we ever hope to
295 * be a true multi-acess file server. */
296 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
297 syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
301 /* Setup internal deadlock detection. */
302 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
303 syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
307 #if DB_VERSION_MINOR > 1
308 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
309 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
310 syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
313 #endif /* DB_VERSION_MINOR > 1 */
315 /* Open the database environment. */
316 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
317 if (rc == DB_RUNRECOVERY) {
318 /* This is the mother of all errors. We _must_ fail here. */
319 syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
323 /* We can't get a full transactional environment, so multi-access
324 * is out of the question. Let's assume a read-only environment,
325 * and try to at least get a shared memory pool. */
326 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
327 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
328 * open the environment with no flags. */
329 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
330 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
331 path, db_strerror(rc));
335 db->flags |= CNIDFLAG_DB_RO;
336 open_flag = DB_RDONLY;
337 syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
340 /* If we have the recovery lock, close the file, remove it, so other
341 * clients can proceed opening the DB environment. */
343 (void)remove(recover_file);
349 syslog(LOG_ERR, "cnid_open: Unable to remove %s: %s",
350 recover_file, strerror(errno));
356 /* did/name reverse mapping. We use a BTree for this one. */
357 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
358 syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
363 db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
364 if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
365 DB_BTREE, open_flag, 0666))) {
366 syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
371 /* Check for version. This way we can update the database if we need
372 * to change the format in any way. */
373 memset(&key, 0, sizeof(key));
374 memset(&data, 0, sizeof(data));
375 key.data = DBVERSION_KEY;
376 key.size = DBVERSION_KEYLEN;
379 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
380 syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
382 db->db_didname->close(db->db_didname, 0);
386 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
389 case DB_LOCK_DEADLOCK:
390 if ((ret = txn_abort(tid)) != 0) {
391 syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
392 db->db_didname->close(db->db_didname, 0);
395 goto dbversion_retry;
398 u_int32_t version = htonl(DBVERSION);
400 data.data = &version;
401 data.size = sizeof(version);
404 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
406 if (ret == DB_LOCK_DEADLOCK) {
407 if ((ret = txn_abort(tid)) != 0) {
408 syslog(LOG_ERR, "cnid_open: txn_abort: %s",
410 db->db_didname->close(db->db_didname, 0);
413 goto dbversion_retry;
415 else if (ret == DB_RUNRECOVERY) {
416 /* At this point, we don't care if the transaction aborts
417 * successfully or not. */
419 syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
421 db->db_didname->close(db->db_didname, 0);
425 break; /* while loop */
428 syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
430 db->db_didname->close(db->db_didname, 0);
435 if ((rc = txn_commit(tid, 0)) != 0) {
436 syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
438 db->db_didname->close(db->db_didname, 0);
442 /* TODO In the future we might check for version number here. */
444 memcpy(&version, data.data, sizeof(version));
445 if (version != ntohl(DBVERSION)) {
451 /* did/macname (31 character) mapping. Use a BTree for this one. */
452 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
453 syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
455 db->db_didname->close(db->db_didname, 0);
459 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
460 if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
461 syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
463 db->db_didname->close(db->db_didname, 0);
467 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
468 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
469 syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
471 db->db_didname->close(db->db_didname, 0);
472 db->db_macname->close(db->db_macname, 0);
476 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
477 if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
478 syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
480 db->db_didname->close(db->db_didname, 0);
481 db->db_macname->close(db->db_macname, 0);
485 /* did/longname (Unicode) mapping. Use a BTree for this one. */
486 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
487 syslog(LOG_ERR, "cnid_open: Failed to create 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);
495 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
496 if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
497 syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
499 db->db_didname->close(db->db_didname, 0);
500 db->db_macname->close(db->db_macname, 0);
501 db->db_shortname->close(db->db_shortname, 0);
504 #endif /* EXTENDED_DB */
506 /* dev/ino reverse mapping. Use a hash for this one. */
507 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
508 syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
510 db->db_didname->close(db->db_didname, 0);
512 db->db_macname->close(db->db_macname, 0);
513 db->db_shortname->close(db->db_shortname, 0);
514 db->db_longname->close(db->db_longname, 0);
515 #endif /* EXTENDED_DB */
519 if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
520 syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
522 db->db_didname->close(db->db_didname, 0);
524 db->db_macname->close(db->db_macname, 0);
525 db->db_shortname->close(db->db_shortname, 0);
526 db->db_longname->close(db->db_longname, 0);
527 #endif /* EXTENDED_DB */
531 /* Main CNID database. Use a hash for this one. */
532 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
533 syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
535 db->db_didname->close(db->db_didname, 0);
537 db->db_macname->close(db->db_macname, 0);
538 db->db_shortname->close(db->db_shortname, 0);
539 db->db_longname->close(db->db_longname, 0);
540 #endif /* EXTENDED_DB */
541 db->db_devino->close(db->db_devino, 0);
546 if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
547 syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
549 db->db_didname->close(db->db_didname, 0);
551 db->db_macname->close(db->db_macname, 0);
552 db->db_shortname->close(db->db_shortname, 0);
553 db->db_longname->close(db->db_longname, 0);
554 #endif /* EXTENDED_DB */
561 syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
562 db->dbenv->close(db->dbenv, 0);
565 if (db->lockfd > -1) {
569 (void)remove(recover_file);