2 * $Id: cnid_open.c,v 1.30 2002-01-04 04:45:48 sibaz 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>
52 #include <atalk/logger.h>
53 #ifdef HAVE_SYS_TIME_H
55 #endif /* HAVE_SYS_TIME_H */
59 #include <atalk/adouble.h>
60 #include <atalk/cnid.h>
61 #include <atalk/util.h>
63 #include "cnid_private.h"
66 #define MIN(a, b) ((a) < (b) ? (a) : (b))
69 #define DBHOME ".AppleDB"
70 #define DBCNID "cnid.db"
71 #define DBDEVINO "devino.db"
72 #define DBDIDNAME "didname.db" /* did/full name mapping */
73 #define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
74 #define DBMACNAME "macname.db" /* did/31 mapping */
75 #define DBLONGNAME "longname.db" /* did/unicode mapping */
76 #define DBLOCKFILE "cnid.lock"
77 #define DBRECOVERFILE "cnid.dbrecover"
78 #define DBCLOSEFILE "cnid.close"
83 /* we version the did/name database so that we can change the format
84 * if necessary. the key is in the form of a did/name pair. in this case,
86 #define DBVERSION_KEY "\0\0\0\0\0"
87 #define DBVERSION_KEYLEN 5
88 #define DBVERSION1 0x00000001U
89 #define DBVERSION DBVERSION1
91 #if DB_VERSION_MINOR > 1
92 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
93 DB_INIT_LOG | DB_INIT_TXN)
94 #else /* DB_VERSION_MINOR < 1 */
95 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
96 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
97 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98 DB_INIT_LOG | DB_INIT_TXN)*/
99 #endif /* DB_VERSION_MINOR */
101 /* Let's try and use the random deadlock decider if available. This adds
102 * a bit of entropy to the mix that might be beneficial. If random isn't
103 * available, we'll decide deadlocks by kicking off the youngest process.
104 * If we can't do that, then let DB3 use its default deadlock detector. */
105 #ifdef DB_LOCK_RANDOM
106 #define DEAD_LOCK_DETECT DB_LOCK_RANDOM
107 #elif defined DB_LOCK_YOUNGEST
108 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
109 #else /* DB_LOCK_RANDOM */
110 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
111 #endif /* DB_LOCK_RANDOM */
113 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
116 /* the first compare that's always done. */
117 static __inline__ int compare_did(const DBT *a, const DBT *b)
119 u_int32_t dida, didb;
121 memcpy(&dida, a->data, sizeof(dida));
122 memcpy(&didb, b->data, sizeof(didb));
126 /* sort did's and then names. this is for unix paths.
127 * i.e., did/unixname lookups. */
128 #if DB_VERSION_MINOR > 1
129 static int compare_unix(DB *db, const DBT *a, const DBT *b)
130 #else /* DB_VERSION_MINOR < 1 */
131 static int compare_unix(const DBT *a, const DBT *b)
132 #endif /* DB_VERSION_MINOR */
138 if ((ret = compare_did(a, b)))
141 sa = (u_int8_t *) a->data + 4; /* shift past did */
142 sb = (u_int8_t *) b->data + 4;
143 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
144 if ((ret = (*sa - *sb)))
145 return ret; /* sort by lexical ordering */
147 return a->size - b->size; /* sort by length */
150 /* sort did's and then names. this is for macified paths (i.e.,
151 * did/macname, and did/shortname. i think did/longname needs a
152 * unicode table to work. also, we can't use strdiacasecmp as that
153 * returns a match if a < b. */
154 #if DB_VERSION_MINOR > 1
155 static int compare_mac(DB *db, const DBT *a, const DBT *b)
156 #else /* DB_VERSION_MINOR < 1 */
157 static int compare_mac(const DBT *a, const DBT *b)
158 #endif /* DB_VERSION_MINOR */
164 if ((ret = compare_did(a, b)))
167 sa = (u_int8_t *) a->data + 4;
168 sb = (u_int8_t *) b->data + 4;
169 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
170 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
171 return ret; /* sort by lexical ordering */
173 return a->size - b->size; /* sort by length */
177 /* for unicode names -- right now it's the same as compare_mac. */
178 #if DB_VERSION_MINOR > 1
179 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
180 #else /* DB_VERSION_MINOR < 1 */
181 static int compare_unicode(const DBT *a, const DBT *b)
182 #endif /* DB_VERSION_MINOR */
184 #if DB_VERSION_MINOR > 1
185 return compare_mac(db,a,b);
186 #else /* DB_VERSION_MINOR < 1 */
187 return compare_mac(a,b);
188 #endif /* DB_VERSION_MINOR */
191 static int have_lock = 0;
193 void *cnid_open(const char *dir) {
194 struct stat st, rsb, csb;
196 char path[MAXPATHLEN + 1];
197 char recover_file[MAXPATHLEN + 1];
201 u_int32_t DBEXTRAS = 0;
209 /* this checks .AppleDB */
210 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
211 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
215 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
216 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
220 db->magic = CNID_DB_MAGIC;
223 if (path[len - 1] != '/') {
228 lock.l_type = F_WRLCK;
229 lock.l_whence = SEEK_SET;
231 strcpy(path + len, DBHOME);
232 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
233 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
237 /* Make sure cnid.lock goes in .AppleDB. */
241 strcpy(db->close_file, path);
242 strcat(db->close_file, DBCLOSEFILE);
244 /* Check to make sure that a client isn't in the process of closing
245 * the database environment. To do this, select on the close file. */
246 while(stat(db->close_file, &csb) == 0) {
250 (void)select(0, NULL, NULL, NULL, &ct);
253 strcpy(recover_file, path);
254 strcat(recover_file, DBRECOVERFILE);
256 /* Search for a byte lock. This allows us to cleanup the log files
257 * at cnid_close() in a clean fashion.
259 * NOTE: This won't work if multiple volumes for the same user refer
260 * to the sahe directory. */
261 strcat(path, DBLOCKFILE);
262 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
265 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
266 if (++lock.l_start > MAXITER) {
267 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
275 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
278 /* Create a file to represent database recovery. While this file
279 * exists, the database is being recovered, and all other clients will
280 * select until recovery is complete, and this file goes away. */
281 if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
282 if (stat(recover_file, &rsb) == 0) {
283 (void)remove(recover_file);
285 if ((rfd = open(recover_file, O_RDWR | O_CREAT, 0666)) > -1) {
286 DBEXTRAS |= DB_RECOVER;
290 else if (!have_lock) {
291 while (stat(recover_file, &rsb) == 0) {
295 (void)select(0, NULL, NULL, NULL, &rt);
299 path[len + DBHOMELEN] = '\0';
300 open_flag = DB_CREATE;
302 /* We need to be able to open the database environment with full
303 * transaction, logging, and locking support if we ever hope to
304 * be a true multi-acess file server. */
305 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
306 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
310 /* Setup internal deadlock detection. */
311 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
312 LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
316 #if DB_VERSION_MINOR > 1
317 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
318 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
319 LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
322 #endif /* DB_VERSION_MINOR > 1 */
324 /* Open the database environment. */
325 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
326 if (rc == DB_RUNRECOVERY) {
327 /* This is the mother of all errors. We _must_ fail here. */
328 LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
332 /* We can't get a full transactional environment, so multi-access
333 * is out of the question. Let's assume a read-only environment,
334 * and try to at least get a shared memory pool. */
335 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
336 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
337 * open the environment with no flags. */
338 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
339 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
340 path, db_strerror(rc));
344 db->flags |= CNIDFLAG_DB_RO;
345 open_flag = DB_RDONLY;
346 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
349 /* If we have the recovery lock, close the file, remove it, so other
350 * clients can proceed opening the DB environment. */
352 (void)remove(recover_file);
358 LOG(log_error, logtype_default, "cnid_open: Unable to remove %s: %s",
359 recover_file, strerror(errno));
365 /* did/name reverse mapping. We use a BTree for this one. */
366 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
367 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
372 db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
373 if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
374 DB_BTREE, open_flag, 0666))) {
375 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
380 /* Check for version. This way we can update the database if we need
381 * to change the format in any way. */
382 memset(&key, 0, sizeof(key));
383 memset(&data, 0, sizeof(data));
384 key.data = DBVERSION_KEY;
385 key.size = DBVERSION_KEYLEN;
388 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
389 LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
391 db->db_didname->close(db->db_didname, 0);
395 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
398 case DB_LOCK_DEADLOCK:
399 if ((ret = txn_abort(tid)) != 0) {
400 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
401 db->db_didname->close(db->db_didname, 0);
404 goto dbversion_retry;
407 u_int32_t version = htonl(DBVERSION);
409 data.data = &version;
410 data.size = sizeof(version);
413 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
415 if (ret == DB_LOCK_DEADLOCK) {
416 if ((ret = txn_abort(tid)) != 0) {
417 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
419 db->db_didname->close(db->db_didname, 0);
422 goto dbversion_retry;
424 else if (ret == DB_RUNRECOVERY) {
425 /* At this point, we don't care if the transaction aborts
426 * successfully or not. */
428 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
430 db->db_didname->close(db->db_didname, 0);
434 break; /* while loop */
437 LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
439 db->db_didname->close(db->db_didname, 0);
444 if ((rc = txn_commit(tid, 0)) != 0) {
445 LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
447 db->db_didname->close(db->db_didname, 0);
451 /* TODO In the future we might check for version number here. */
453 memcpy(&version, data.data, sizeof(version));
454 if (version != ntohl(DBVERSION)) {
460 /* did/macname (31 character) mapping. Use a BTree for this one. */
461 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
462 LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
464 db->db_didname->close(db->db_didname, 0);
468 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
469 if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
470 LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
472 db->db_didname->close(db->db_didname, 0);
476 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
477 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
478 LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
480 db->db_didname->close(db->db_didname, 0);
481 db->db_macname->close(db->db_macname, 0);
485 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
486 if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
487 LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
489 db->db_didname->close(db->db_didname, 0);
490 db->db_macname->close(db->db_macname, 0);
494 /* did/longname (Unicode) mapping. Use a BTree for this one. */
495 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
496 LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
498 db->db_didname->close(db->db_didname, 0);
499 db->db_macname->close(db->db_macname, 0);
500 db->db_shortname->close(db->db_shortname, 0);
504 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
505 if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
506 LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
508 db->db_didname->close(db->db_didname, 0);
509 db->db_macname->close(db->db_macname, 0);
510 db->db_shortname->close(db->db_shortname, 0);
513 #endif /* EXTENDED_DB */
515 /* dev/ino reverse mapping. Use a hash for this one. */
516 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
517 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
519 db->db_didname->close(db->db_didname, 0);
521 db->db_macname->close(db->db_macname, 0);
522 db->db_shortname->close(db->db_shortname, 0);
523 db->db_longname->close(db->db_longname, 0);
524 #endif /* EXTENDED_DB */
528 if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
529 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
531 db->db_didname->close(db->db_didname, 0);
533 db->db_macname->close(db->db_macname, 0);
534 db->db_shortname->close(db->db_shortname, 0);
535 db->db_longname->close(db->db_longname, 0);
536 #endif /* EXTENDED_DB */
540 /* Main CNID database. Use a hash for this one. */
541 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
542 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
544 db->db_didname->close(db->db_didname, 0);
546 db->db_macname->close(db->db_macname, 0);
547 db->db_shortname->close(db->db_shortname, 0);
548 db->db_longname->close(db->db_longname, 0);
549 #endif /* EXTENDED_DB */
550 db->db_devino->close(db->db_devino, 0);
555 if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
556 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
558 db->db_didname->close(db->db_didname, 0);
560 db->db_macname->close(db->db_macname, 0);
561 db->db_shortname->close(db->db_shortname, 0);
562 db->db_longname->close(db->db_longname, 0);
563 #endif /* EXTENDED_DB */
570 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
571 db->dbenv->close(db->dbenv, 0);
574 if (db->lockfd > -1) {
578 (void)remove(recover_file);