2 * $Id: cnid_open.c,v 1.40 2002-05-30 06:41:52 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 * 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
93 #if DB_VERSION_MINOR > 1
94 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
95 DB_INIT_LOG | DB_INIT_TXN)
96 #else /* DB_VERSION_MINOR < 1 */
97 /*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
99 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
100 DB_INIT_LOG | DB_INIT_TXN)
101 #endif /* DB_VERSION_MINOR */
103 /* Let's try and use the youngest lock detector if present.
104 * If we can't do that, then let DB3 use its default deadlock detector. */
105 #if defined DB_LOCK_YOUNGEST
106 #define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
107 #else /* DB_LOCK_YOUNGEST */
108 #define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
109 #endif /* DB_LOCK_YOUNGEST */
111 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
114 /* the first compare that's always done. */
115 static __inline__ int compare_did(const DBT *a, const DBT *b)
117 u_int32_t dida, didb;
119 memcpy(&dida, a->data, sizeof(dida));
120 memcpy(&didb, b->data, sizeof(didb));
124 /* sort did's and then names. this is for unix paths.
125 * i.e., did/unixname lookups. */
126 #if DB_VERSION_MINOR > 1
127 static int compare_unix(DB *db, const DBT *a, const DBT *b)
128 #else /* DB_VERSION_MINOR < 1 */
129 static int compare_unix(const DBT *a, const DBT *b)
130 #endif /* DB_VERSION_MINOR */
136 if ((ret = compare_did(a, b)))
139 sa = (u_int8_t *) a->data + 4; /* shift past did */
140 sb = (u_int8_t *) b->data + 4;
141 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
142 if ((ret = (*sa - *sb)))
143 return ret; /* sort by lexical ordering */
145 return a->size - b->size; /* sort by length */
148 /* sort did's and then names. this is for macified paths (i.e.,
149 * did/macname, and did/shortname. i think did/longname needs a
150 * unicode table to work. also, we can't use strdiacasecmp as that
151 * returns a match if a < b. */
152 #if DB_VERSION_MINOR > 1
153 static int compare_mac(DB *db, const DBT *a, const DBT *b)
154 #else /* DB_VERSION_MINOR < 1 */
155 static int compare_mac(const DBT *a, const DBT *b)
156 #endif /* DB_VERSION_MINOR */
162 if ((ret = compare_did(a, b)))
165 sa = (u_int8_t *) a->data + 4;
166 sb = (u_int8_t *) b->data + 4;
167 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
168 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
169 return ret; /* sort by lexical ordering */
171 return a->size - b->size; /* sort by length */
175 /* for unicode names -- right now it's the same as compare_mac. */
176 #if DB_VERSION_MINOR > 1
177 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
178 #else /* DB_VERSION_MINOR < 1 */
179 static int compare_unicode(const DBT *a, const DBT *b)
180 #endif /* DB_VERSION_MINOR */
182 #if DB_VERSION_MINOR > 1
183 return compare_mac(db,a,b);
184 #else /* DB_VERSION_MINOR < 1 */
185 return compare_mac(a,b);
186 #endif /* DB_VERSION_MINOR */
189 void *cnid_open(const char *dir) {
190 struct stat st, rsb, lsb, csb;
192 char path[MAXPATHLEN + 1];
203 /* this checks .AppleDB */
204 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
205 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
209 if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
210 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
214 db->magic = CNID_DB_MAGIC;
217 if (path[len - 1] != '/') {
222 lock.l_type = F_WRLCK;
223 lock.l_whence = SEEK_SET;
225 strcpy(path + len, DBHOME);
226 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
227 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
231 /* Make sure cnid.lock goes in .AppleDB. */
235 /* Search for a byte lock. This allows us to cleanup the log files
236 * at cnid_close() in a clean fashion.
238 * NOTE: This won't work if multiple volumes for the same user refer
239 * to the sahe directory. */
240 strcat(path, DBLOCKFILE);
241 strcpy(db->lock_file, path);
242 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
245 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
246 if (++lock.l_start > MAXITER) {
247 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
255 LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
258 path[len + DBHOMELEN] = '\0';
259 open_flag = DB_CREATE;
261 /* We need to be able to open the database environment with full
262 * transaction, logging, and locking support if we ever hope to
263 * be a true multi-acess file server. */
264 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
265 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
269 /* Setup internal deadlock detection. */
270 if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
271 LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
275 #if DB_VERSION_MINOR > 1
277 /* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
278 if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
279 LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
283 #endif /* DB_VERSION_MINOR > 1 */
285 /* Open the database environment. */
286 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) != 0) {
287 if (rc == DB_RUNRECOVERY) {
288 /* This is the mother of all errors. We _must_ fail here. */
289 LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
293 /* We can't get a full transactional environment, so multi-access
294 * is out of the question. Let's assume a read-only environment,
295 * and try to at least get a shared memory pool. */
296 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) != 0) {
297 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
298 * open the environment with no flags. */
299 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666)) != 0) {
300 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
301 path, db_strerror(rc));
305 db->flags |= CNIDFLAG_DB_RO;
306 open_flag = DB_RDONLY;
307 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
310 /* did/name reverse mapping. We use a BTree for this one. */
311 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
312 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
317 /*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
318 if ((rc = db->db_didname->open(db->db_didname, DBDIDNAME, NULL,
319 DB_HASH, open_flag, 0666))) {
320 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
325 /* Check for version. This way we can update the database if we need
326 * to change the format in any way. */
327 memset(&key, 0, sizeof(key));
328 memset(&data, 0, sizeof(data));
329 key.data = DBVERSION_KEY;
330 key.size = DBVERSION_KEYLEN;
333 if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
334 LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
336 db->db_didname->close(db->db_didname, 0);
340 while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
343 case DB_LOCK_DEADLOCK:
344 if ((ret = txn_abort(tid)) != 0) {
345 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
346 db->db_didname->close(db->db_didname, 0);
349 goto dbversion_retry;
352 u_int32_t version = htonl(DBVERSION);
354 data.data = &version;
355 data.size = sizeof(version);
358 if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
360 if (ret == DB_LOCK_DEADLOCK) {
361 if ((ret = txn_abort(tid)) != 0) {
362 LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
364 db->db_didname->close(db->db_didname, 0);
367 goto dbversion_retry;
369 else if (ret == DB_RUNRECOVERY) {
370 /* At this point, we don't care if the transaction aborts
371 * successfully or not. */
373 LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
375 db->db_didname->close(db->db_didname, 0);
379 break; /* while loop */
382 LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
384 db->db_didname->close(db->db_didname, 0);
389 if ((rc = txn_commit(tid, 0)) != 0) {
390 LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
392 db->db_didname->close(db->db_didname, 0);
396 /* TODO In the future we might check for version number here. */
398 memcpy(&version, data.data, sizeof(version));
399 if (version != ntohl(DBVERSION)) {
405 /* did/macname (31 character) mapping. Use a BTree for this one. */
406 if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
407 LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
409 db->db_didname->close(db->db_didname, 0);
413 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
414 if ((rc = db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
415 LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
417 db->db_didname->close(db->db_didname, 0);
421 /* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
422 if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
423 LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
425 db->db_didname->close(db->db_didname, 0);
426 db->db_macname->close(db->db_macname, 0);
430 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
431 if ((rc = db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
432 LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
434 db->db_didname->close(db->db_didname, 0);
435 db->db_macname->close(db->db_macname, 0);
439 /* did/longname (Unicode) mapping. Use a BTree for this one. */
440 if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
441 LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
443 db->db_didname->close(db->db_didname, 0);
444 db->db_macname->close(db->db_macname, 0);
445 db->db_shortname->close(db->db_shortname, 0);
449 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
450 if ((rc = db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) != 0) {
451 LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
453 db->db_didname->close(db->db_didname, 0);
454 db->db_macname->close(db->db_macname, 0);
455 db->db_shortname->close(db->db_shortname, 0);
458 #endif /* EXTENDED_DB */
460 /* dev/ino reverse mapping. Use a hash for this one. */
461 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
462 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
464 db->db_didname->close(db->db_didname, 0);
466 db->db_macname->close(db->db_macname, 0);
467 db->db_shortname->close(db->db_shortname, 0);
468 db->db_longname->close(db->db_longname, 0);
469 #endif /* EXTENDED_DB */
473 if ((rc = db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) != 0) {
474 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
476 db->db_didname->close(db->db_didname, 0);
478 db->db_macname->close(db->db_macname, 0);
479 db->db_shortname->close(db->db_shortname, 0);
480 db->db_longname->close(db->db_longname, 0);
481 #endif /* EXTENDED_DB */
485 /* Main CNID database. Use a hash for this one. */
486 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
487 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
489 db->db_didname->close(db->db_didname, 0);
491 db->db_macname->close(db->db_macname, 0);
492 db->db_shortname->close(db->db_shortname, 0);
493 db->db_longname->close(db->db_longname, 0);
494 #endif /* EXTENDED_DB */
495 db->db_devino->close(db->db_devino, 0);
500 if ((rc = db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) != 0) {
501 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
503 db->db_didname->close(db->db_didname, 0);
505 db->db_macname->close(db->db_macname, 0);
506 db->db_shortname->close(db->db_shortname, 0);
507 db->db_longname->close(db->db_longname, 0);
508 #endif /* EXTENDED_DB */
509 db->db_devino->close(db->db_devino, 0);
514 /* filename mangling database. Use a hash for this one. */
515 if ((rc = db_create(&db->db_mangle, db->dbenv, 0)) != 0) {
516 LOG(log_error, logtype_default, "cnid_open: Failed to create mangle database: %s", db_strerror(rc));
517 db->db_didname->close(db->db_didname, 0);
518 db->db_devino->close(db->db_devino, 0);
519 db->db_cnid->close(db->db_cnid, 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_mangle->open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666)) != 0) {
529 LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
530 db->db_didname->close(db->db_didname, 0);
531 db->db_devino->close(db->db_devino, 0);
532 db->db_cnid->close(db->db_cnid, 0);
534 db->db_macname->close(db->db_macname, 0);
535 db->db_shortname->close(db->db_shortname, 0);
536 db->db_longname->close(db->db_longname, 0);
537 #endif /* EXTENDED_DB */
540 #endif /* FILE_MANGLING */
542 /* Print out the version of DB3 we're linked against. */
543 LOG(log_info, logtype_default, "CNID DB initialized using %s",
544 db_version(NULL, NULL, NULL));
549 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
550 db->dbenv->close(db->dbenv, 0);
553 if (db->lockfd > -1) {
555 (void)remove(db->lock_file);