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 * CNIDs 4-16 are reserved according to page 31 of the AFP 3.0 spec so,
32 * CNID_START begins at 17.
37 #endif /* HAVE_CONFIG_H */
39 #ifdef CNID_BACKEND_CDB
41 #include <atalk/cnid_private.h>
42 #include "cnid_cdb_private.h"
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
48 #define DBHOME ".AppleDB"
49 #define DBCNID "cnid2.db"
50 #define DBDEVINO "devino.db"
51 #define DBDIDNAME "didname.db" /* did/full name mapping */
52 #define DBLOCKFILE "cnid.lock"
57 #define DBOPTIONS (DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL)
59 #define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
62 static char *old_dbfiles[] = {"cnid.db", NULL};
64 /* -----------------------
65 * bandaid for LanTest performance pb. for now not used, cf. ifdef 0 below
67 static int my_yield(void)
74 ret = select(0, NULL, NULL, NULL, &t);
79 static int didname(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
83 memset(skey, 0, sizeof(DBT));
84 skey->data = (char *)pdata->data + CNID_DID_OFS;
85 len = strlen((char *)skey->data + CNID_DID_LEN);
86 skey->size = CNID_DID_LEN + len + 1;
91 static int devino(DB *dbp _U_, const DBT *pkey _U_, const DBT *pdata, DBT *skey)
93 memset(skey, 0, sizeof(DBT));
94 skey->data = (char *)pdata->data + CNID_DEVINO_OFS;
95 skey->size = CNID_DEVINO_LEN;
100 static int my_associate (DB *p, DB *s,
101 int (*callback)(DB *, const DBT *,const DBT *, DBT *),
104 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
105 return p->associate(p, NULL, s, callback, flags);
107 #if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
108 return p->associate(p, s, callback, flags);
110 return 0; /* FIXME */
115 /* --------------- */
116 static int my_open(DB * p, const char *f, const char *d, DBTYPE t, u_int32_t flags, int mode)
118 #if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
119 return p->open(p, NULL, f, d, t, flags, mode);
121 return p->open(p, f, d, t, flags, mode);
125 /* --------------- */
126 static struct _cnid_db *cnid_cdb_new(const char *volpath)
128 struct _cnid_db *cdb;
129 int major, minor, patch;
132 version_str = db_version(&major, &minor, &patch);
134 /* check library match, ignore if only patch level changed */
135 if ( major != DB_VERSION_MAJOR || minor != DB_VERSION_MINOR)
137 LOG(log_error, logtype_cnid, "cnid_cdb_new: the Berkeley DB library version used does not match the version compiled with: (%u.%u)/(%u.%u)", DB_VERSION_MAJOR, DB_VERSION_MINOR, major, minor);
141 if ((cdb = (struct _cnid_db *) calloc(1, sizeof(struct _cnid_db))) == NULL)
144 if ((cdb->volpath = strdup(volpath)) == NULL) {
149 cdb->flags = CNID_FLAG_PERSISTENT;
151 cdb->cnid_add = cnid_cdb_add;
152 cdb->cnid_delete = cnid_cdb_delete;
153 cdb->cnid_get = cnid_cdb_get;
154 cdb->cnid_lookup = cnid_cdb_lookup;
155 cdb->cnid_nextid = NULL; /*cnid_cdb_nextid;*/
156 cdb->cnid_resolve = cnid_cdb_resolve;
157 cdb->cnid_update = cnid_cdb_update;
158 cdb->cnid_close = cnid_cdb_close;
159 cdb->cnid_getstamp = cnid_cdb_getstamp;
160 cdb->cnid_rebuild_add = cnid_cdb_rebuild_add;
165 /* --------------- */
166 static int upgrade_required(char *dbdir)
168 char path[MAXPATHLEN + 1];
176 if (path[len - 1] != '/') {
181 for (i = 0; old_dbfiles[i] != NULL; i++) {
182 strcpy(path + len, old_dbfiles[i]);
183 if ( !(stat(path, &st) < 0) ) {
187 if (errno != ENOENT) {
188 LOG(log_error, logtype_default, "cnid_open: Checking %s gave %s", path, strerror(errno));
195 /* --------------- */
196 struct _cnid_db *cnid_cdb_open(struct cnid_open_args *args)
199 char path[MAXPATHLEN + 1];
201 struct _cnid_db *cdb;
203 static int first = 0;
206 if (!args->dir || *args->dir == 0) {
210 /* this checks .AppleDB.
211 We need space for dir + '/' + DBHOMELEN + '/' + DBLEN */
212 if ((len = strlen(args->dir)) > (MAXPATHLEN - DBHOMELEN - DBLEN - 2)) {
213 LOG(log_error, logtype_default, "cnid_open: Pathname too large: %s", args->dir);
217 if ((cdb = cnid_cdb_new(args->dir)) == NULL) {
218 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
222 if ((db = (CNID_private *) calloc(1, sizeof(CNID_private))) == NULL) {
223 LOG(log_error, logtype_default, "cnid_open: Unable to allocate memory for database");
227 cdb->_private = (void *) db;
228 db->magic = CNID_DB_MAGIC;
230 strcpy(path, args->dir);
231 if (path[len - 1] != '/') {
236 strcpy(path + len, DBHOME);
237 if ((stat(path, &st) < 0) && (ad_mkdir(path, 0777 & ~args->mask) < 0)) {
238 LOG(log_error, logtype_default, "cnid_open: DBHOME mkdir failed for %s", path);
242 if (upgrade_required(path)) {
243 LOG(log_error, logtype_default, "cnid_open: Found version 1 of the CNID database. Please upgrade to version 2");
247 /* Print out the version of BDB we're linked against. */
250 LOG(log_info, logtype_default, "CNID DB initialized using %s", db_version(NULL, NULL, NULL));
253 open_flag = DB_CREATE;
255 /* We need to be able to open the database environment with full
256 * transaction, logging, and locking support if we ever hope to
257 * be a true multi-acess file server. */
258 if ((rc = db_env_create(&db->dbenv, 0)) != 0) {
259 LOG(log_error, logtype_default, "cnid_open: db_env_create: %s", db_strerror(rc));
263 /* Open the database environment. */
264 if ((rc = db->dbenv->open(db->dbenv, path, DBOPTIONS, 0666 & ~args->mask)) != 0) {
265 LOG(log_error, logtype_default, "cnid_open: dbenv->open (rw) of %s failed: %s", path, db_strerror(rc));
266 /* FIXME: This should probably go. Even if it worked, any use for a read-only DB? Didier? */
267 if (rc == DB_RUNRECOVERY) {
268 /* This is the mother of all errors. We _must_ fail here. */
269 LOG(log_error, logtype_default,
270 "cnid_open: CATASTROPHIC ERROR opening database environment %s. Run db_recovery -c immediately", path);
274 /* We can't get a full transactional environment, so multi-access
275 * is out of the question. Let's assume a read-only environment,
276 * and try to at least get a shared memory pool. */
277 if ((rc = db->dbenv->open(db->dbenv, path, DB_INIT_MPOOL, 0666 & ~args->mask)) != 0) {
278 /* Nope, not a MPOOL, either. Last-ditch effort: we'll try to
279 * open the environment with no flags. */
280 if ((rc = db->dbenv->open(db->dbenv, path, 0, 0666 & ~args->mask)) != 0) {
281 LOG(log_error, logtype_default, "cnid_open: dbenv->open of %s failed: %s", path, db_strerror(rc));
285 db->flags |= CNIDFLAG_DB_RO;
286 open_flag = DB_RDONLY;
287 LOG(log_info, logtype_default, "cnid_open: Obtained read-only database environment %s", path);
290 /* ---------------------- */
291 /* Main CNID database. Use a hash for this one. */
293 if ((rc = db_create(&db->db_cnid, db->dbenv, 0)) != 0) {
294 LOG(log_error, logtype_default, "cnid_open: Failed to create cnid database: %s",
299 if ((rc = my_open(db->db_cnid, DBCNID, DBCNID, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
300 LOG(log_error, logtype_default, "cnid_open: Failed to open dev/ino database: %s",
305 /* ---------------------- */
306 /* did/name reverse mapping. We use a BTree for this one. */
308 if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
309 LOG(log_error, logtype_default, "cnid_open: Failed to create did/name database: %s",
314 if ((rc = my_open(db->db_didname, DBCNID, DBDIDNAME, DB_BTREE, open_flag, 0666 & ~args->mask))) {
315 LOG(log_error, logtype_default, "cnid_open: Failed to open did/name database: %s",
320 /* ---------------------- */
321 /* dev/ino reverse mapping. Use a hash for this one. */
323 if ((rc = db_create(&db->db_devino, db->dbenv, 0)) != 0) {
324 LOG(log_error, logtype_default, "cnid_open: Failed to create dev/ino database: %s",
329 if ((rc = my_open(db->db_devino, DBCNID, DBDEVINO, DB_BTREE, open_flag, 0666 & ~args->mask)) != 0) {
330 LOG(log_error, logtype_default, "cnid_open: Failed to open devino database: %s",
335 /* ---------------------- */
336 /* Associate the secondary with the primary. */
337 if ((rc = my_associate(db->db_cnid, db->db_didname, didname, 0)) != 0) {
338 LOG(log_error, logtype_default, "cnid_open: Failed to associate didname database: %s",
343 if ((rc = my_associate(db->db_cnid, db->db_devino, devino, 0)) != 0) {
344 LOG(log_error, logtype_default, "cnid_open: Failed to associate devino database: %s",
349 /* ---------------------- */
350 /* Check for version. "cdb" only supports CNID_VERSION_0, cf cnid_private.h */
355 memset(&key, 0, sizeof(key));
356 memset(&data, 0, sizeof(data));
357 key.data = ROOTINFO_KEY;
358 key.size = ROOTINFO_KEYLEN;
360 if ((rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0)) == 0) {
361 /* If not found, ignore it */
362 memcpy(&version, data.data + CNID_DID_OFS, sizeof(version));
363 version = ntohl(version);
364 LOG(log_debug, logtype_default, "CNID db version %u", version);
365 if (version != CNID_VERSION_0) {
366 LOG(log_error, logtype_default, "Unsupported CNID db version %u, use CNID backend \"dbd\"", version);
371 db_env_set_func_yield(my_yield);
376 db->db_didname->close(db->db_didname, 0);
378 db->db_devino->close(db->db_devino, 0);
380 db->db_cnid->close(db->db_cnid, 0);
381 LOG(log_error, logtype_default, "cnid_open: Failed to setup CNID DB environment");
382 db->dbenv->close(db->dbenv, 0);
391 if (cdb->volpath != NULL)
398 struct _cnid_module cnid_cdb_module = {
402 CNID_FLAG_SETUID | CNID_FLAG_BLOCK
406 #endif /* CNID_BACKEND_CDB */