2
* $Id: cnid_open.c,v 1.43.2.2 2003/03/19 11:50:18 didg 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
94
#define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
95
#else /* !CNID_DB_CDB */
96
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
97
#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
98
DB_INIT_LOG | DB_INIT_TXN)
99
#else /* DB_VERSION_MINOR < 1 */
100
/*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
101
DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
102
#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
103
DB_INIT_LOG | DB_INIT_TXN)
104
#endif /* DB_VERSION_MINOR */
105
#endif /* CNID_DB_CDB */
108
/* Let's try and use the youngest lock detector if present.
109
* If we can't do that, then let BDB use its default deadlock detector. */
110
#if defined DB_LOCK_YOUNGEST
111
#define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
112
#else /* DB_LOCK_YOUNGEST */
113
#define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
114
#endif /* DB_LOCK_YOUNGEST */
115
#endif /* CNID_DB_CDB */
117
#define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
120
/* -----------------------
121
* bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
123
static int my_yield(void)
130
ret = select(0, NULL, NULL, NULL, &t);
134
/* --------------- */
135
static int my_open(DB *p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
137
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
138
return p->open(p, NULL, f, d, t, flags, mode);
140
return p->open(p, f, d, t, flags, mode);
144
/* --------------- */
145
/* the first compare that's always done. */
146
static __inline__ int compare_did(const DBT *a, const DBT *b)
148
u_int32_t dida, didb;
150
memcpy(&dida, a->data, sizeof(dida));
151
memcpy(&didb, b->data, sizeof(didb));
155
/* sort did's and then names. this is for unix paths.
156
* i.e., did/unixname lookups. */
157
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
158
static int compare_unix(DB *db, const DBT *a, const DBT *b)
159
#else /* DB_VERSION_MINOR < 1 */
160
static int compare_unix(const DBT *a, const DBT *b)
161
#endif /* DB_VERSION_MINOR */
167
if ((ret = compare_did(a, b)))
170
sa = (u_int8_t *) a->data + 4; /* shift past did */
171
sb = (u_int8_t *) b->data + 4;
172
for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
173
if ((ret = (*sa - *sb)))
174
return ret; /* sort by lexical ordering */
176
return a->size - b->size; /* sort by length */
179
/* sort did's and then names. this is for macified paths (i.e.,
180
* did/macname, and did/shortname. i think did/longname needs a
181
* unicode table to work. also, we can't use strdiacasecmp as that
182
* returns a match if a < b. */
183
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
184
static int compare_mac(DB *db, const DBT *a, const DBT *b)
185
#else /* DB_VERSION_MINOR < 1 */
186
static int compare_mac(const DBT *a, const DBT *b)
187
#endif /* DB_VERSION_MINOR */
193
if ((ret = compare_did(a, b)))
196
sa = (u_int8_t *) a->data + 4;
197
sb = (u_int8_t *) b->data + 4;
198
for (len = MIN(a->size, b->size); len-- > 4; sa++, sb++)
199
if ((ret = (_diacasemap[*sa] - _diacasemap[*sb])))
200
return ret; /* sort by lexical ordering */
202
return a->size - b->size; /* sort by length */
206
/* for unicode names -- right now it's the same as compare_mac. */
207
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
208
static int compare_unicode(DB *db, const DBT *a, const DBT *b)
209
#else /* DB_VERSION_MINOR < 1 */
210
static int compare_unicode(const DBT *a, const DBT *b)
211
#endif /* DB_VERSION_MINOR */
213
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
214
return compare_mac(db,a,b);
215
#else /* DB_VERSION_MINOR < 1 */
216
return compare_mac(a,b);
217
#endif /* DB_VERSION_MINOR */
220
void *cnid_open(const char *dir, mode_t mask) {
224
#endif /* CNID_DB_CDB */
225
char path[MAXPATHLEN + 1];
236
/* this checks .AppleDB */
237
if ((len = strlen(dir)) > (MAXPATHLEN - DBLEN - 1)) {
238
LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", dir);
242
if ((db = (CNID_private *)calloc(1, sizeof(CNID_private))) == NULL) {
243
LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
247
db->magic = CNID_DB_MAGIC;
250
if (path[len - 1] != '/') {
255
strcpy(path + len, DBHOME);
256
if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~mask) < 0)) {
257
LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
262
lock.l_type = F_WRLCK;
263
lock.l_whence = SEEK_SET;
264
/* Make sure cnid.lock goes in .AppleDB. */
268
/* Search for a byte lock. This allows us to cleanup the log files
269
* at cnid_close() in a clean fashion.
271
* NOTE: This won't work if multiple volumes for the same user refer
272
* to the sahe directory. */
273
strcat(path, DBLOCKFILE);
274
strcpy(db->lock_file, path);
275
if ((db->lockfd = open(path, O_RDWR | O_CREAT, 0666 & ~mask)) > -1) {
278
while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
279
if (++lock.l_start > MAXITER) {
280
LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
288
LOG(log_error, logtype_default, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
290
#endif /* CNID_DB_CDB */
292
path[len + DBHOMELEN] = '\0';
293
open_flag = DB_CREATE;
295
/* We need to be able to open the database environment with full
296
* transaction, logging, and locking support if we ever hope to
297
* be a true multi-acess file server. */
298
if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
299
LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
304
/* Setup internal deadlock detection. */
305
if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
306
LOG(log_error, logtype_default, "cnid_open: set_lk_detect: %s", db_strerror(rc));
309
#endif /* CNID_DB_CDB */
312
#if DB_VERSION_MAJOR >= 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR > 1)
314
/* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
315
if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
316
LOG(log_error, logtype_default, "cnid_open: set_flags: %s", db_strerror(rc));
320
#endif /* DB_VERSION_MINOR > 1 */
321
#endif /* CNID_DB_CDB */
323
/* Open the database environment. */
324
if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~mask)) != 0) {
325
if (rc == DB_RUNRECOVERY) {
326
/* This is the mother of all errors. We _must_ fail here. */
327
LOG(log_error, logtype_default, "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
331
/* We can't get a full transactional environment, so multi-access
332
* is out of the question. Let's assume a read-only environment,
333
* and try to at least get a shared memory pool. */
334
if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~mask)) != 0) {
335
/* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
336
* open the environment with no flags. */
337
if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~mask)) != 0) {
338
LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s",
339
path, db_strerror(rc));
343
db->flags |= CNIDFLAG_DB_RO;
344
open_flag = DB_RDONLY;
345
LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
348
/* did/name reverse mapping. We use a BTree for this one. */
349
if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
350
LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
355
/*db->db_didname->set_bt_compare(db->db_didname, &compare_unix);*/
356
if ((rc = my_open(db->db_didname, DBDIDNAME, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
357
LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
362
/* Check for version. This way we can update the database if we need
363
* to change the format in any way. */
364
memset(&key, 0, sizeof(key));
365
memset(&data, 0, sizeof(data));
366
key.data = DBVERSION_KEY;
367
key.size = DBVERSION_KEYLEN;
370
if ((rc = db->db_didname->get(db->db_didname, NULL, &key, &data, 0)) != 0) {
373
u_int32_t version = htonl(DBVERSION);
375
data.data = &version;
376
data.size = sizeof(version);
378
if ((ret = db->db_didname->put(db->db_didname, NULL, &key, &data,
380
LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
385
#else /* CNID_DB_CDB */
387
if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
388
LOG(log_error, logtype_default, "cnid_open: txn_begin: failed to check db version: %s",
393
while ((rc = db->db_didname->get(db->db_didname, tid, &key, &data, DB_RMW))) {
396
case DB_LOCK_DEADLOCK:
397
if ((ret = txn_abort(tid)) != 0) {
398
LOG(log_error, logtype_default, "cnid_open: txn_abort: %s", db_strerror(ret));
401
goto dbversion_retry;
404
u_int32_t version = htonl(DBVERSION);
406
data.data = &version;
407
data.size = sizeof(version);
410
if ((ret = db->db_didname->put(db->db_didname, tid, &key, &data,
412
if (ret == DB_LOCK_DEADLOCK) {
413
if ((ret = txn_abort(tid)) != 0) {
414
LOG(log_error, logtype_default, "cnid_open: txn_abort: %s",
418
goto dbversion_retry;
420
else if (ret == DB_RUNRECOVERY) {
421
/* At this point, we don't care if the transaction aborts
422
* successfully or not. */
424
LOG(log_error, logtype_default, "cnid_open: Error putting new version: %s",
429
break; /* while loop */
432
LOG(log_error, logtype_default, "cnid_open: Failed to check db version: %s",
438
if ((rc = txn_commit(tid, 0)) != 0) {
439
LOG(log_error, logtype_default, "cnid_open: Failed to commit db version: %s",
443
#endif /* CNID_DB_CDB */
445
/* TODO In the future we might check for version number here. */
447
memcpy(&version, data.data, sizeof(version));
448
if (version != ntohl(DBVERSION)) {
454
/* did/macname (31 character) mapping. Use a BTree for this one. */
455
if ((rc = db_create(&db->db_macname, db->dbenv, 0)) != 0) {
456
LOG(log_error, logtype_default, "cnid_open: Failed to create did/macname database: %s",
461
db->db_macname->set_bt_compare(db->db_macname, &compare_mac);
462
if ((rc = my_open(db->db_macname, DBMACNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
463
LOG(log_error, logtype_default, "cnid_open: Failed to open did/macname database: %s",
468
/* did/shortname (DOS 8.3) mapping. Use a BTree for this one. */
469
if ((rc = db_create(&db->db_shortname, db->dbenv, 0)) != 0) {
470
LOG(log_error, logtype_default, "cnid_open: Failed to create did/shortname database: %s",
475
db->db_shortname->set_bt_compare(db->db_shortname, &compare_mac);
476
if ((rc = my_open(db->db_shortname, DBSHORTNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
477
LOG(log_error, logtype_default, "cnid_open: Failed to open did/shortname database: %s",
482
/* did/longname (Unicode) mapping. Use a BTree for this one. */
483
if ((rc = db_create(&db->db_longname, db->dbenv, 0)) != 0) {
484
LOG(log_error, logtype_default, "cnid_open: Failed to create did/longname database: %s",
489
db->db_longname->set_bt_compare(db->db_longname, &compare_unicode);
490
if ((rc = my_open(db->db_longname, DBLONGNAME, NULL, DB_BTREE, open_flag, 0666 & ~mask))) {
491
LOG(log_error, logtype_default, "cnid_open: Failed to open did/longname database: %s",
495
#endif /* EXTENDED_DB */
497
/* dev/ino reverse mapping. Use a hash for this one. */
498
if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
499
LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
504
if ((rc = my_open(db->db_devino, DBDEVINO, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
505
LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
510
/* Main CNID database. Use a hash for this one. */
511
if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
512
LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
517
if ((rc = my_open(db->db_cnid, DBCNID, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
518
LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
524
/* filename mangling database. Use a hash for this one. */
525
if ((rc = db_create(&db->db_mangle, db->dbenv, 0)) != 0) {
526
LOG(log_error, logtype_default, "cnid_open: Failed to create mangle database: %s", db_strerror(rc));
530
if ((rc = my_open(db->db_mangle, DBMANGLE, NULL, DB_HASH, open_flag, 0666 & ~mask))) {
531
LOG(log_error, logtype_default, "cnid_open: Failed to open mangle database: %s", db_strerror(rc));
534
#endif /* FILE_MANGLING */
536
/* Print out the version of BDB we're linked against. */
537
LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
539
db_env_set_func_yield(my_yield);
544
if (db->db_didname) db->db_didname->close(db->db_didname, 0);
545
if (db->db_devino) db->db_devino->close(db->db_devino, 0);
546
if (db->db_cnid) db->db_cnid->close(db->db_cnid, 0);
548
if (db->db_macname) db->db_macname->close(db->db_macname, 0);
549
if (db->db_shortname) db->db_shortname->close(db->db_shortname, 0);
550
if (db->db_longname) db->db_longname->close(db->db_longname, 0);
551
#endif /* EXTENDED_DB */
552
LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
553
db->dbenv->close(db->dbenv, 0);
557
if (db->lockfd > -1) {
559
(void)remove(db->lock_file);
561
#endif /* CNID_DB_CDB */