2 * $Id: cnid_open.c,v 1.12 2001-09-23 19:08:23 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 #endif /* DB_VERSION_MINOR */
94 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
97 /* the first compare that's always done. */
98 static __inline__ int compare_did(const DBT *a, const DBT *b)
100 u_int32_t dida, didb;
102 memcpy(&dida, a->data, sizeof(dida));
103 memcpy(&didb, b->data, sizeof(didb));
107 /* sort did's and then names. this is for unix paths.
108 * i.e., did/unixname lookups. */
109 #if DB_VERSION_MINOR > 1
110 static int compare_unix(DB *db, const DBT *a, const DBT *b)
111 #else /* DB_VERSION_MINOR < 1 */
112 static int compare_unix(const DBT *a, const DBT *b)
113 #endif /* DB_VERSION_MINOR */
119 if ((ret = compare_did(a, b)))
122 sa = (u_int8_t *) a->data + 4; /* shift past did */
123 sb = (u_int8_t *) b->data + 4;
124 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
125 if ((ret = (*sa - *sb)))
126 return ret; /* sort by lexical ordering */
128 return a->size - b->size; /* sort by length */
131 /* sort did's and then names. this is for macified paths (i.e.,
132 * did/macname, and did/shortname. i think did/longname needs a
133 * unicode table to work. also, we can't use strdiacasecmp as that
134 * returns a match if a < b. */
135 #if DB_VERSION_MINOR > 1
136 static int compare_mac(DB *db, const DBT *a, const DBT *b)
137 #else /* DB_VERSION_MINOR < 1 */
138 static int compare_mac(const DBT *a, const DBT *b)
139 #endif /* DB_VERSION_MINOR */
145 if ((ret = compare_did(a, b)))
148 sa = (u_int8_t *) a->data + 4;
149 sb = (u_int8_t *) b->data + 4;
150 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
151 if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
152 return ret; /* sort by lexical ordering */
154 return a->size - b->size; /* sort by length */
158 /* for unicode names -- right now it's the same as compare_mac. */
159 #if DB_VERSION_MINOR > 1
160 static int compare_unicode(DB *db, const DBT *a, const DBT *b)
161 #else /* DB_VERSION_MINOR < 1 */
162 static int compare_unicode(const DBT *a, const DBT *b)
163 #endif /* DB_VERSION_MINOR */
165 #if DB_VERSION_MINOR > 1
166 return compare_mac(db,a,b);
167 #else /* DB_VERSION_MINOR < 1 */
168 return compare_mac(a,b);
169 #endif /* DB_VERSION_MINOR */
172 void *cnid_open(const char *dir)
176 char path[MAXPATHLEN + 1];
185 /* this checks .AppleDB */
186 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
187 syslog(LOG_ERR, "cnid_open: path too large");
191 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
192 syslog(LOG_ERR, "cnid_open: unable to allocate memory");
195 db->magic = CNID_DB_MAGIC;
198 if (path[len - 1] != '/') {
203 lock.l_type = F_WRLCK;
204 lock.l_whence = SEEK_SET;
207 strcpy(path + len, DBHOME);
208 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
209 syslog(LOG_ERR, "cnid_open: mkdir failed");
213 /* search for a byte lock. this allows us to cleanup the log files
214 * at cnid_close() in a clean fashion.
216 * NOTE: this won't work if multiple volumes for the same user refer
217 * to the same directory. */
218 strcat(path, DBLOCKFILE);
219 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
222 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
223 if (++lock.l_start > MAXITER) {
224 syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
232 path[len + DBHOMELEN] = '\0';
233 open_flag = DB_CREATE;
234 /* try a full-blown transactional environment */
235 if (db_env_create(&db->dbenv, 0)) {
236 syslog(LOG_ERR, "cnid_open: db_env_create failed");
240 #if DB_VERSION_MINOR > 1
241 db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1);
242 #endif /* DB_VERSION_MINOR > 1 */
244 /* Check to see if a DBENV already exists. If it does, join it. */
245 /* if (db->dbenv->open(db->dbenv, path, DB_JOINENV, 0666)) {*/
246 if (db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666)) {
248 /* try with a shared memory pool */
249 if (db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666)) {
251 /* try without any options. */
252 if (db->dbenv->open(db->dbenv, path, 0, 0666)) {
253 syslog(LOG_ERR, "cnid_open: db_env_open failed");
257 db->flags |= CNIDFLAG_DB_RO;
258 open_flag = DB_RDONLY;
259 syslog(LOG_INFO, "cnid_open: read-only CNID database");
264 /* did/name reverse mapping. we use a btree for this one. */
265 if (db_create(&db->db_didname, db->dbenv, 0))
268 db->db_didname->set_bt_compare(db->db_didname, &compare_unix);
269 if (db->db_didname->open(db->db_didname, DBDIDNAME, NULL, DB_BTREE, open_flag, 0666)) {
273 /* check for version. this way we can update the database if we need
274 to change the format in any way. */
275 memset(&key, 0, sizeof(key));
276 memset(&data, 0, sizeof(data));
277 key.data = DBVERSION_KEY;
278 key.size = DBVERSION_KEYLEN;
279 while ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0))) {
281 case DB_LOCK_DEADLOCK:
286 u_int32_t version = htonl(DBVERSION);
288 data.data = &version;
289 data.size = sizeof(version);
292 if ((rc = db->db_didname->put(db->db_didname, NULL, &key, &data,
294 if (rc == DB_LOCK_DEADLOCK)
295 goto dbversion_retry;
298 /* uh oh. something bad happened. bail. */
299 db->db_didname->close(db->db_didname, 0);
304 /* XXX: in the future, we might check for version number here. */
306 memcpy(&version, data.data, sizeof(version));
307 if (version != htonl(DBVERSION)) {
313 /* did/macname mapping. btree this one. */
314 if (db_create(&db->db_macname, db->dbenv, 0))
317 db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
318 if (db->db_macname->open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666)) {
319 db->db_didname->close(db->db_didname, 0);
323 /* did/shortname mapping */
324 if (db_create(&db->db_shortname, db->dbenv, 0))
327 db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
328 if (db->db_shortname->open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666)) {
329 db->db_didname->close(db->db_didname, 0);
330 db->db_macname->close(db->db_macname, 0);
334 /* did/longname mapping */
335 if (db_create(&db->db_longname, db->dbenv, 0))
338 db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
339 if (db->db_longname->open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666)) {
340 db->db_didname->close(db->db_didname, 0);
341 db->db_macname->close(db->db_macname, 0);
342 db->db_shortname->close(db->db_shortname, 0);
345 #endif /* EXTENDED_DB */
347 /* dev/ino reverse mapping. we hash this one. */
348 if (db_create(&db->db_devino, db->dbenv, 0))
351 if (db->db_devino->open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666)) {
352 db->db_didname->close(db->db_didname, 0);
354 db->db_macname->close(db->db_macname, 0);
355 db->db_shortname->close(db->db_shortname, 0);
356 db->db_longname->close(db->db_longname, 0);
357 #endif /* EXTENDED_DB */
361 /* main cnid database. we hash this one as well. */
362 if (db_create(&db->db_cnid, db->dbenv, 0))
365 if (db->db_cnid->open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666)) {
366 db->db_didname->close(db->db_didname, 0);
368 db->db_macname->close(db->db_macname, 0);
369 db->db_shortname->close(db->db_shortname, 0);
370 db->db_longname->close(db->db_longname, 0);
371 #endif /* EXTENDED_DB */
372 db->db_devino->close(db->db_devino, 0);
379 syslog(LOG_ERR, "cnid_open: db_open failed");
380 db->dbenv->close(db->dbenv, 0);