2 * Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
5 * CNID database support.
8 * 1) afpd already caches did's.
9 * 2) the database stores cnid's as both did/name and dev/ino pairs.
10 * 3) RootInfo holds the value of the NextID.
11 * 4) the cnid database gets called in the following manner --
13 * cnid = cnid_open(root_dir);
16 * newid = cnid_add(cnid, dev, ino, parent did,
17 * name, id); id is a hint for a specific id. pass 0 if you don't
18 * care. if the id is already assigned, you won't get what you
21 * given an id, get a did/name and dev/ino pair.
22 * name = cnid_get(cnid, &id); given an id, return the corresponding
24 * return code = cnid_delete(cnid, id); delete an entry.
26 * with AFP, CNIDs 0-2 have special meanings. here they are:
28 * 1 -- parent of root directory (handled by afpd)
29 * 2 -- root directory (handled by afpd)
31 * so, CNID_START begins at 3.
38 #include <sys/param.h>
44 #include <atalk/adouble.h>
45 #include <atalk/cnid.h>
46 #include <atalk/util.h>
48 #include "cnid_private.h"
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #define ROOTINFO "RootInfo"
55 #define ROOTINFO_LEN 8
57 #define DBHOME ".AppleDB"
58 #define DBCNID "cnid.db"
59 #define DBDEVINO "devino.db"
60 #define DBDIDNAME "didname.db" /* did/full name mapping */
61 #define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
62 #define DBMACNAME "macname.db" /* did/31 mapping */
63 #define DBLONGNAME "longname.db" /* did/unicode mapping */
64 #define DBLOCKFILE "/cnid.lock"
69 /* we version the did/name database so that we can change the format
70 * if necessary. the key is in the form of a did/name pair. in this case,
72 #define DBVERSION_KEY "\0\0\0\0\0"
73 #define DBVERSION_KEYLEN 5
74 #define DBVERSION1 0x00000001U
75 #define DBVERSION DBVERSION1
77 #define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
78 DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC | DB_RECOVER)
80 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
83 /* the first compare that's always done. */
84 static __inline__ int compare_did(const DBT *a, const DBT *b)
88 memcpy(&dida, a->data, sizeof(dida));
89 memcpy(&didb, b->data, sizeof(didb));
93 /* sort did's and then names. this is for unix paths.
94 * i.e., did/unixname lookups. */
95 static int compare_unix(const DBT *a, const DBT *b)
101 if (ret = compare_did(a, b))
104 sa = a->data + 4; /* shift past did */
106 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
107 if (ret = (*sa - *sb))
108 return ret; /* sort by lexical ordering */
109 return a->size - b->size; /* sort by length */
112 /* sort did's and then names. this is for macified paths (i.e.,
113 * did/macname, and did/shortname. i think did/longname needs a
114 * unicode table to work. also, we can't use strdiacasecmp as that
115 * returns a match if a < b. */
116 static int compare_mac(const DBT *a, const DBT *b)
122 if (ret = compare_did(a, b))
127 for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
128 if (ret = (_diacasemap[*sa] - _diacasemap[*sb]))
129 return ret; /* sort by lexical ordering */
130 return a->size - b->size; /* sort by length */
134 /* for unicode names -- right now it's the same as compare_mac. */
135 #define compare_unicode(a, b) compare_mac((a), (b))
137 void *cnid_open(const char *dir)
141 char path[MAXPATHLEN + 1];
150 /* this checks both RootInfo and .AppleDB */
151 if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
152 syslog(LOG_ERR, "cnid_open: path too large");
156 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
157 syslog(LOG_ERR, "cnid_open: unable to allocate memory");
160 db->magic = CNID_DB_MAGIC;
163 if (path[len - 1] != '/') {
168 lock.l_type = F_WRLCK;
169 lock.l_whence = SEEK_SET;
171 /* we create and initialize RootInfo if it doesn't exist. */
172 strcat(path, ROOTINFO);
173 if (ad_open(path, ADFLAGS_HF, O_RDWR, 0666, &db->rootinfo) < 0) {
176 /* see if we can open it read-only. if it's read-only, we can't
178 memset(&db->rootinfo, 0, sizeof(db->rootinfo));
179 if (ad_open(path, ADFLAGS_HF, O_RDONLY, 0666, &db->rootinfo) == 0) {
180 db->flags = CNIDFLAG_ROOTINFO_RO;
181 syslog(LOG_INFO, "cnid_open: read-only RootInfo");
185 /* create the file */
186 memset(&db->rootinfo, 0, sizeof(db->rootinfo));
187 if (ad_open(path, ADFLAGS_HF, O_CREAT | O_RDWR, 0666,
188 &db->rootinfo) < 0) {
189 syslog(LOG_ERR, "cnid_open: ad_open(RootInfo)");
193 /* lock the RootInfo file. this and cnid_add are the only places
194 * that should fiddle with RootInfo. */
195 lock.l_start = ad_getentryoff(&db->rootinfo, ADEID_DID);
196 lock.l_len = ad_getentrylen(&db->rootinfo, ADEID_DID);
197 if (fcntl(ad_hfileno(&db->rootinfo), F_SETLKW, &lock) < 0) {
198 syslog(LOG_ERR, "cnid_open: can't establish lock: %m");
202 /* store the beginning CNID */
203 id = htonl(CNID_START);
204 memcpy(ad_entry(&db->rootinfo, ADEID_DID), &id, sizeof(id));
205 ad_flush(&db->rootinfo, ADFLAGS_HF);
208 lock.l_type = F_UNLCK;
209 fcntl(ad_hfileno(&db->rootinfo), F_SETLK, &lock);
210 lock.l_type = F_WRLCK;
214 strcpy(path + len, DBHOME);
215 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777) < 0)) {
216 syslog(LOG_ERR, "cnid_open: mkdir failed");
220 /* search for a byte lock. this allows us to cleanup the log files
221 * at cnid_close() in a clean fashion.
223 * NOTE: this won't work if multiple volumes for the same user refer
224 * to the same directory. */
225 strcat(path, DBLOCKFILE);
226 if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666)) > -1) {
229 while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
230 if (++lock.l_start > MAXITER) {
231 syslog(LOG_INFO, "cnid_open: can't establish logfile cleanup lock.");
239 path[len + DBHOMELEN] = '\0';
240 open_flag = DB_CREATE;
241 /* try a full-blown transactional environment */
242 if (db_appinit(path, NULL, &db->dbenv, DBOPTIONS)) {
244 /* try with a shared memory pool */
245 memset(&db->dbenv, 0, sizeof(db->dbenv));
246 if (db_appinit(path, NULL, &db->dbenv, DB_INIT_MPOOL)) {
248 /* try without any options. */
249 memset(&db->dbenv, 0, sizeof(db->dbenv));
250 if (db_appinit(path, NULL, &db->dbenv, 0)) {
251 syslog(LOG_ERR, "cnid_open: db_appinit failed");
255 db->flags |= CNIDFLAG_DB_RO;
256 open_flag = DB_RDONLY;
257 syslog(LOG_INFO, "cnid_open: read-only CNID database");
260 memset(&dbi, 0, sizeof(dbi));
262 /* did/name reverse mapping. we use a btree for this one. */
263 dbi.bt_compare = compare_unix;
264 if (db_open(DBDIDNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
269 /* check for version. this way we can update the database if we need
270 to change the format in any way. */
271 memset(&key, 0, sizeof(key));
272 memset(&data, 0, sizeof(data));
273 key.data = DBVERSION_KEY;
274 key.len = DBVERSION_KEY_LEN;
275 while (errno = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) {
281 u_int32_t version = htonl(DBVERSION);
283 data.data = &version;
284 data.len = sizeof(version);
286 if (db->db_didname->put(db->db_didname, NULL, &key, &data,
289 goto dbversion_retry;
292 /* uh oh. something bad happened. bail. */
293 db->db_didname->close(db->db_didname, 0);
298 /* XXX: in the future, we might check for version number here. */
300 memcpy(&version, data.data, sizeof(version));
301 if (version != htonl(DBVERSION)) {
306 /* did/macname mapping. btree this one. */
307 dbi.bt_compare = compare_mac;
308 if (db_open(DBMACNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
310 db->db_didname->close(db->db_didname, 0);
314 /* did/shortname mapping */
315 if (db_open(DBSHORTNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
316 &db->db_shortname)) {
317 db->db_didname->close(db->db_didname, 0);
318 db->db_macname->close(db->db_macname, 0);
322 /* did/longname mapping */
323 dbi.bt_compare = compare_unicode;
324 if (db_open(DBLONGNAME, DB_BTREE, open_flag, 0666, &db->dbenv, &dbi,
326 db->db_didname->close(db->db_didname, 0);
327 db->db_macname->close(db->db_macname, 0);
328 db->db_shortname->close(db->db_shortname, 0);
332 /* dev/ino reverse mapping. we hash this one. */
333 dbi.bt_compare = NULL;
334 if (db_open(DBDEVINO, DB_HASH, 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);
339 db->db_longname->close(db->db_longname, 0);
343 /* main cnid database. we hash this one as well. */
344 if (db_open(DBCNID, 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);
350 db->db_devino->close(db->db_devino, 0);
356 syslog(LOG_ERR, "cnid_open: db_open failed");
357 db_appexit(&db->dbenv);
364 ad_close(&db->rootinfo, ADFLAGS_HF);