2 * $Id: cnid_open.c,v 1.2 2001-06-29 14:14:46 rufustfirefly 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 */
44 #endif /* HAVE_UNISTD_H */
47 #endif /* HAVE_FCNTL_H */
48 #include <sys/param.h>
54 #include <atalk/adouble.h>
55 #include <atalk/cnid.h>
56 #include <atalk/util.h>
58 #include "cnid_private.h"
61 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define ROOTINFO "RootInfo"
65 #define ROOTINFO_LEN 8
67 #define DBHOME ".AppleDB"
68 #define DBCNID "cnid.db"
69 #define DBDEVINO "devino.db"
70 #define DBDIDNAME "didname.db" /* did/full name mapping */
71 #define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
72 #define DBMACNAME "macname.db" /* did/31 mapping */
73 #define DBLONGNAME "longname.db" /* did/unicode mapping */
74 #define DBLOCKFILE "/cnid.lock"
79 /* we version the did/name database so that we can change the format
80 * if necessary. the key is in the form of a did/name pair. in this case,
82 #define DBVERSION_KEY "\0\0\0\0\0"
83 #define DBVERSION_KEYLEN 5
84 #define DBVERSION1 0x00000001U
85 #define DBVERSION DBVERSION1
87 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
88 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC | DB_RECOVER)
90 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
93 /* the first compare that's always done. */
94 static __inline__ int compare_did(const DBT *a, const DBT *b)
98 memcpy(&dida, a->data, sizeof(dida));
99 memcpy(&didb, b->data, sizeof(didb));
103 /* sort did's and then names. this is for unix paths.
104 * i.e., did/unixname lookups. */
105 static int compare_unix(const DBT *a, const DBT *b)
111 if (ret = compare_did(a, b))
114 sa = a->data + 4; /* shift past did */
116 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
117 if (ret = (*sa - *sb))
118 return ret; /* sort by lexical ordering */
119 return a->size - b->size; /* sort by length */
122 /* sort did's and then names. this is for macified paths (i.e.,
123 * did/macname, and did/shortname. i think did/longname needs a
124 * unicode table to work. also, we can't use strdiacasecmp as that
125 * returns a match if a < b. */
126 static int compare_mac(const DBT *a, const DBT *b)
132 if (ret = compare_did(a, b))
137 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
138 if (ret = (_diacasemap[*sa] - _diacasemap[*sb]))
139 return ret; /* sort by lexical ordering */
140 return a->size - b->size; /* sort by length */
144 /* for unicode names -- right now it's the same as compare_mac. */
145 #define compare_unicode(a, b) compare_mac((a), (b))
147 void *cnid_open(const char *dir)
151 char path[MAXPATHLEN + 1];
160 /* this checks both RootInfo and .AppleDB */
161 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
162 syslog(LOG_ERR, "cnid_open: path too large");
166 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
167 syslog(LOG_ERR, "cnid_open: unable to allocate memory");
170 db->magic = CNID_DB_MAGIC;
173 if (path[len - 1] != '/') {
178 lock.l_type = F_WRLCK;
179 lock.l_whence = SEEK_SET;
181 /* we create and initialize RootInfo if it doesn't exist. */
182 strcat(path, ROOTINFO);
183 if (ad_open(path, ADFLAGS_HF, O_RDWR, 0666, &db->rootinfo) < 0) {
186 /* see if we can open it read-only. if it's read-only, we can't
188 memset(&db->rootinfo, 0, sizeof(db->rootinfo));
189 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0666, &db->rootinfo) == 0) {
190 db->flags = CNIDFLAG_ROOTINFO_RO;
191 syslog(LOG_INFO, "cnid_open: read-only RootInfo");
195 /* create the file */
196 memset(&db->rootinfo, 0, sizeof(db->rootinfo));
197 if (ad_open(path, ADFLAGS_HF, O_CREAT | O_RDWR, 0666,
198 &db->rootinfo) < 0) {
199 syslog(LOG_ERR, "cnid_open: ad_open(RootInfo)");
203 /* lock the RootInfo file. this and cnid_add are the only places
204 * that should fiddle with RootInfo. */
205 lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
206 lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
207 if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW, &lock) < 0) {
208 syslog(LOG_ERR, "cnid_open: can't establish lock: %m");
212 /* store the beginning CNID */
213 id = htonl(CNID_START);
214 memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
215 ad_flush(&db->rootinfo, ADFLAGS_HF);
218 lock.l_type = F_UNLCK;
219 fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
220 lock.l_type = F_WRLCK;
224 strcpy(path + len, DBHOME);
225 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
226 syslog(LOG_ERR, "cnid_open: mkdir failed");
230 /* search for a byte lock. this allows us to cleanup the log files
231 * at cnid_close() in a clean fashion.
233 * NOTE: this won't work if multiple volumes for the same user refer
234 * to the same directory. */
235 strcat(path, DBLOCKFILE);
236 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
239 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
240 if (++lock.l_start > MAXITER) {
241 syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
249 path[len + DBHOMELEN] = '\0';
250 open_flag = DB_CREATE;
251 /* try a full-blown transactional environment */
252 if (db_appinit(path, NULL, &db->dbenv, DBOPTIONS)) {
254 /* try with a shared memory pool */
255 memset(&db->dbenv, 0, sizeof(db->dbenv));
256 if (db_appinit(path, NULL, &db->dbenv, DB_INIT_MPOOL)) {
258 /* try without any options. */
259 memset(&db->dbenv, 0, sizeof(db->dbenv));
260 if (db_appinit(path, NULL, &db->dbenv, 0)) {
261 syslog(LOG_ERR, "cnid_open: db_appinit failed");
265 db->flags |= CNIDFLAG_DB_RO;
266 open_flag = DB_RDONLY;
267 syslog(LOG_INFO, "cnid_open: read-only CNID database");
270 memset(&dbi, 0, sizeof(dbi));
272 /* did/name reverse mapping. we use a btree for this one. */
273 dbi.bt_compare = compare_unix;
274 if (db_open(DBDIDNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
279 /* check for version. this way we can update the database if we need
280 to change the format in any way. */
281 memset(&key, 0, sizeof(key));
282 memset(&data, 0, sizeof(data));
283 key.data = DBVERSION_KEY;
284 key.len = DBVERSION_KEY_LEN;
285 while (errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) {
291 u_int32_t version = htonl(DBVERSION);
293 data.data = &version;
294 data.len = sizeof(version);
296 if (db->db_didname->put(db->db_didname, NULL, &key, &data,
299 goto dbversion_retry;
302 /* uh oh. something bad happened. bail. */
303 db->db_didname->close(db->db_didname, 0);
308 /* XXX: in the future, we might check for version number here. */
310 memcpy(&version, data.data, sizeof(version));
311 if (version != htonl(DBVERSION)) {
316 /* did/macname mapping. btree this one. */
317 dbi.bt_compare = compare_mac;
318 if (db_open(DBMACNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
320 db->db_didname->close(db->db_didname, 0);
324 /* did/shortname mapping */
325 if (db_open(DBSHORTNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
326 &db->db_shortname)) {
327 db->db_didname->close(db->db_didname, 0);
328 db->db_macname->close(db->db_macname, 0);
332 /* did/longname mapping */
333 dbi.bt_compare = compare_unicode;
334 if (db_open(DBLONGNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
336 db->db_didname->close(db->db_didname, 0);
337 db->db_macname->close(db->db_macname, 0);
338 db->db_shortname->close(db->db_shortname, 0);
342 /* dev/ino reverse mapping. we hash this one. */
343 dbi.bt_compare = NULL;
344 if (db_open(DBDEVINO, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
346 db->db_didname->close(db->db_didname, 0);
347 db->db_macname->close(db->db_macname, 0);
348 db->db_shortname->close(db->db_shortname, 0);
349 db->db_longname->close(db->db_longname, 0);
353 /* main cnid database. we hash this one as well. */
354 if (db_open(DBCNID, DB_HASH, open_flag, 0666, &db->dbenv, &dbi,
356 db->db_didname->close(db->db_didname, 0);
357 db->db_macname->close(db->db_macname, 0);
358 db->db_shortname->close(db->db_shortname, 0);
359 db->db_longname->close(db->db_longname, 0);
360 db->db_devino->close(db->db_devino, 0);
366 syslog(LOG_ERR, "cnid_open: db_open failed");
367 db_appexit(&db->dbenv);
374 ad_close(&db->rootinfo, ADFLAGS_HF);