2 * $Id: cnid_open.c,v 1.18 2001-11-04 22:13:50 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"
78 /* we version the did/name database so that we can change the format
79 * if necessary. the key is in the form of a did/name pair. in this case,
81 #define DBVERSION_KEY "\0\0\0\0\0"
82 #define DBVERSION_KEYLEN 5
83 #define DBVERSION1 0x00000001U
84 #define DBVERSION DBVERSION1
86 #if DB_VERSION_MINOR > 1
87 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
88 DB_INIT_LOG | DB_INIT_TXN)
89 #else /* DB_VERSION_MINOR < 1 */
90 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
91 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
92 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
93 DB_INIT_LOG | DB_INIT_TXN)
94 #endif /* DB_VERSION_MINOR */
96 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
99 /* the first compare that's always done. */
100 static __inline__ int compare_did(const DBT *a, const DBT *b)
102 u_int32_t dida, didb;
104 memcpy(&dida, a->data, sizeof(dida));
105 memcpy(&didb, b->data, sizeof(didb));
109 /* sort did's and then names. this is for unix paths.
110 * i.e., did/unixname lookups. */
111 #if DB_VERSION_MINOR > 1
112 static int compare_unix(DB *db, const DBT *a, const DBT *b)
113 #else /* DB_VERSION_MINOR < 1 */
114 static int compare_unix(const DBT *a, const DBT *b)
115 #endif /* DB_VERSION_MINOR */
121 if ((ret = compare_did(a, b)))
124 sa = (u_int8_t *) a->data + 4; /* shift past did */
125 sb = (u_int8_t *) b->data + 4;
126 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
127 if ((ret = (*sa - *sb)))
128 return ret; /* sort by lexical ordering */
130 return a->size - b->size; /* sort by length */
133 /* sort did's and then names. this is for macified paths (i.e.,
134 * did/macname, and did/shortname. i think did/longname needs a
135 * unicode table to work. also, we can't use strdiacasecmp as that
136 * returns a match if a < b. */
137 #if DB_VERSION_MINOR > 1
138 static int compare_mac(DB *db, const DBT *a, const DBT *b)
139 #else /* DB_VERSION_MINOR < 1 */
140 static int compare_mac(const DBT *a, const DBT *b)
141 #endif /* DB_VERSION_MINOR */
147 if ((ret = compare_did(a, b)))
150 sa = (u_int8_t *) a->data + 4;
151 sb = (u_int8_t *) b->data + 4;
152 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
153 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
154 return ret; /* sort by lexical ordering */
156 return a->size - b->size; /* sort by length */
160 /* for unicode names -- right now it's the same as compare_mac. */
161 #if DB_VERSION_MINOR > 1
162 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
163 #else /* DB_VERSION_MINOR < 1 */
164 static int compare_unicode(const DBT *a, const DBT *b)
165 #endif /* DB_VERSION_MINOR */
167 #if DB_VERSION_MINOR > 1
168 return compare_mac(db,a,b);
169 #else /* DB_VERSION_MINOR < 1 */
170 return compare_mac(a,b);
171 #endif /* DB_VERSION_MINOR */
174 static int have_lock = 0;
176 void *cnid_open(const char *dir) {
179 char path[MAXPATHLEN + 1];
183 u_int32_t DBEXTRAS = 0;
191 /* this checks .AppleDB */
192 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
193 syslog(LOG_ERR, "cnid_open: Pathname too large: %s", dir);
197 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
198 syslog(LOG_ERR, "cnid_open: Unable to allocate memory for database");
202 db->magic = CNID_DB_MAGIC;
205 if (path[len - 1] != '/') {
210 lock.l_type = F_WRLCK;
211 lock.l_whence = SEEK_SET;
213 strcpy(path + len, DBHOME);
214 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
215 syslog(LOG_ERR, "cnid_open: DBHOME mkdir failed");
219 /* Search for a byte lock. This allows us to cleanup the log files
220 * at cnid_close() in a clean fashion.
222 * NOTE: This won't work if multiple volumes for the same user refer
223 * to the sahe directory. */
224 strcat(path, DBLOCKFILE);
225 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
228 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
229 if (++lock.l_start > MAXITER) {
230 syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock (lock failed)");
238 syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock (open() failed)");
241 if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
242 /* We test to see if we have exclusive database access. If we do, we
243 * will open the database with the DB_RECOVER flag.
246 syslog(LOG_INFO, "cnid_open: Opening database with DB_RECOVER flag");
248 DBEXTRAS |= DB_RECOVER;
253 path[len + DBHOMELEN] = '\0';
254 open_flag = DB_CREATE;
256 /* We need to be able to open the database environment with full
257 * transaction, logging, and locking support if we ever hope to
258 * be a true multi-acess file server. */
259 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
260 syslog(LOG_ERR, "cnid_open: db_env_create: %s", db_strerror(rc));
264 /* Setup internal deadlock detection. */
265 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
266 syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
270 #if DB_VERSION_MINOR > 1
271 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
272 /* if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
273 syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
276 #endif /* DB_VERSION_MINOR > 1 */
278 /* Open the database environment. */
279 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS | DBEXTRAS, 0666)) != 0) {
280 if (rc == DB_RUNRECOVERY) {
281 /* This is the mother of all errors. We _must_ fail here. */
282 syslog(LOG_ERR, "cnid_open: CATASTROPHIC ERROR opening database environment. Run db_recovery -c immediately");
286 /* We can't get a full transactional environment, so multi-access
287 * is out of the question. Let's assume a read-only environment,
288 * and trry to at least get a shared memory pool. */
289 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
290 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
291 * open the environment with no flags. */
292 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
293 syslog(LOG_ERR, "cnid_open: dbenv->open of %s failed: %s",
294 path, db_strerror(rc));
298 db->flags |= CNIDFLAG_DB_RO;
299 open_flag = DB_RDONLY;
300 syslog(LOG_INFO, "cnid_open: Obtained read-only database environment");
303 /* did/name reverse mapping. We use a BTree for this one. */
304 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
305 syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
310 db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
311 if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
312 DB_BTREE, open_flag, 0666))) {
313 syslog(LOG_ERR, "cnid_open: Failed to open did/name database: %s",
318 /* Check for version. This way we can update the database if we need
319 * to change the format in any way. */
320 memset(&key, 0, sizeof(key));
321 memset(&data, 0, sizeof(data));
322 key.data = DBVERSION_KEY;
323 key.size = DBVERSION_KEYLEN;
326 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
327 syslog(LOG_ERR, "cnid_open: txn_begin: failed to check db version: %s",
329 db->db_didname->close(db->db_didname, 0);
333 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
336 case DB_LOCK_DEADLOCK:
337 if ((ret = txn_abort(tid)) != 0) {
338 syslog(LOG_ERR, "cnid_open: txn_abort: %s", db_strerror(ret));
339 db->db_didname->close(db->db_didname, 0);
342 goto dbversion_retry;
345 u_int32_t version = htonl(DBVERSION);
347 data.data = &version;
348 data.size = sizeof(version);
351 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
353 if (ret == DB_LOCK_DEADLOCK) {
354 if ((ret = txn_abort(tid)) != 0) {
355 syslog(LOG_ERR, "cnid_open: txn_abort: %s",
357 db->db_didname->close(db->db_didname, 0);
360 goto dbversion_retry;
362 else if (ret == DB_RUNRECOVERY) {
363 /* At this point, we don't care if the transaction aborts
364 * successfully or not. */
366 syslog(LOG_ERR, "cnid_open: Error putting new version: %s",
368 db->db_didname->close(db->db_didname, 0);
372 break; /* while loop */
375 syslog(LOG_ERR, "cnid_open: Failed to check db version: %s",
377 db->db_didname->close(db->db_didname, 0);
382 if ((rc = txn_commit(tid, 0)) != 0) {
383 syslog(LOG_ERR, "cnid_open: Failed to commit db version: %s",
385 db->db_didname->close(db->db_didname, 0);
389 /* TODO In the future we might check for version number here. */
391 memcpy(&version, data.data, sizeof(version));
392 if (version != ntohl(DBVERSION)) {
398 /* did/macname (31 character) mapping. Use a BTree for this one. */
399 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
400 syslog(LOG_ERR, "cnid_open: Failed to create did/macname database: %s",
402 db->db_didname->close(db->db_didname, 0);
406 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
407 if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
408 syslog(LOG_ERR, "cnid_open: Failed to open did/macname database: %s",
410 db->db_didname->close(db->db_didname, 0);
414 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
415 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
416 syslog(LOG_ERR, "cnid_open: Failed to create did/shortname database: %s",
418 db->db_didname->close(db->db_didname, 0);
419 db->db_macname->close(db->db_macname, 0);
423 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
424 if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
425 syslog(LOG_ERR, "cnid_open: Failed to open did/shortname database: %s",
427 db->db_didname->close(db->db_didname, 0);
428 db->db_macname->close(db->db_macname, 0);
432 /* did/longname (Unicode) mapping. Use a BTree for this one. */
433 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
434 syslog(LOG_ERR, "cnid_open: Failed to create did/longname database: %s",
436 db->db_didname->close(db->db_didname, 0);
437 db->db_macname->close(db->db_macname, 0);
438 db->db_shortname->close(db->db_shortname, 0);
442 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
443 if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
444 syslog(LOG_ERR, "cnid_open: Failed to open did/longname database: %s",
446 db->db_didname->close(db->db_didname, 0);
447 db->db_macname->close(db->db_macname, 0);
448 db->db_shortname->close(db->db_shortname, 0);
451 #endif /* EXTENDED_DB */
453 /* dev/ino reverse mapping. Use a hash for this one. */
454 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
455 syslog(LOG_ERR, "cnid_open: Failed to create dev/ino database: %s",
457 db->db_didname->close(db->db_didname, 0);
459 db->db_macname->close(db->db_macname, 0);
460 db->db_shortname->close(db->db_shortname, 0);
461 db->db_longname->close(db->db_longname, 0);
462 #endif /* EXTENDED_DB */
466 if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
467 syslog(LOG_ERR, "cnid_open: Failed to open devino database: %s",
469 db->db_didname->close(db->db_didname, 0);
471 db->db_macname->close(db->db_macname, 0);
472 db->db_shortname->close(db->db_shortname, 0);
473 db->db_longname->close(db->db_longname, 0);
474 #endif /* EXTENDED_DB */
478 /* Main CNID database. Use a hash for this one. */
479 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
480 syslog(LOG_ERR, "cnid_open: Failed to create cnid database: %s",
482 db->db_didname->close(db->db_didname, 0);
484 db->db_macname->close(db->db_macname, 0);
485 db->db_shortname->close(db->db_shortname, 0);
486 db->db_longname->close(db->db_longname, 0);
487 #endif /* EXTENDED_DB */
488 db->db_devino->close(db->db_devino, 0);
493 if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
494 syslog(LOG_ERR, "cnid_open: Failed to open dev/ino database: %s",
496 db->db_didname->close(db->db_didname, 0);
498 db->db_macname->close(db->db_macname, 0);
499 db->db_shortname->close(db->db_shortname, 0);
500 db->db_longname->close(db->db_longname, 0);
501 #endif /* EXTENDED_DB */
508 syslog(LOG_ERR, "cnid_open: Failed to setup CNID DB environment");
509 db->dbenv->close(db->dbenv, 0);
512 if (db->lockfd > -1) {